참고사항
http://docs.python.org/library/datetime.html
http://timebird.egloos.com/3362817
from datetime import date, timedelta
def date_add(number, base_date, sep="-"):
yy=int(base_date.split(sep)[0])
mm=int(base_date.split(sep)[1])
dd=int(base_date.split(sep)[2])
d=date(yy,mm,dd)
return d+timedelta(days=number)
http://codejob.co.kr/docs/page/67/
import datetime
def elapsed_time(sdate):
e = datetime.datetime.now()
if not sdate or len(sdate) < 14: return 0,0,0,0
s = datetime.datetime(int(sdate[:4]), int(sdate[4:6]), int(sdate[6:8]),
int(sdate[8:10]), int(sdate[10:12]), int(sdate[12:14]))
days = (e-s).days
sec = (e-s).seconds
hour, sec = divmod(sec, 3600)
minute, sec = divmod(sec, 60)
return days, hour, minute, sec
#!/usr/bin/python
# -*- coding: cp949 -*-
from datetime import *
import os
import sys
import time
def timestamp(timestamp): def timedeltaEx(prev_day): print mktime();
def mktime():
# 년, 월, 일, 시, 분, 초, 0, 0, 0
return time.mktime( (10, 8, 20, 10, 20, 30, 0, 0, 0) )
lt = time.localtime(float(timestamp)) #튜플 형태로 반환됨
return "%02d.%02d.%04d %02d:%02d:%02d" % (lt[2], lt[1], lt[0], lt[3], lt[4], lt[5])
timestampVar = time.localtime(time.time()); #현재 시각 가져옴
year = time.strftime('%Y', timestampVar);
month = time.strftime('%m', timestampVar);
day = time.strftime('%d', timestampVar); #년 월 일 분리
#현재 시각 에서 날짜 계산할 날을 더하거나 뺄수 잇다.
dt = datetime( int(year), int(month), int(day) ) - timedelta( prev_day );
return dt.strftime('%Y-%m-%d');
mktime_test = mktime(); #타임스템프 만들기
print "time now: %s" % ( timestamp(mktime_test) ) #타임스템프 출력
print timedeltaEx(1) # 하루전의 날짜를 가져오기
#결과
#1282267230.0
#time now: 20.08.2010 10:20:30
#2010-08-19
내가 만든 것
# -*- coding: utf-8 -*-
from datetime import date, datetime, timedelta
def date_delta(base_date):
yy=int(base_date[:4])
mm=int(base_date[5:7])
dd=int(base_date[8:10])
d=datetime.date(yy,mm,dd)
return (datetime.date.today() - d).days
base_date = "2012-07-02"
print date_delta(base_date)
if date_delta(base_date) < 7 :
print "Yes! it works"
else :
print "No, it doesn't work"