#设a为字符串
import time
a = "2011-09-28 10:00:00"
x = time.localtime(1317091800.0)
time.strftime('%Y-%m-%d %H:%M:%S',x)
import time
a = "2011-09-28 10:00:00"
#中间过程,一般都需要将字符串转化为时间数组
time.strptime(a,'%Y-%m-%d %H:%M:%S')
>>time.struct_time(tm_year=2011, tm_mon=9, tm_mday=27, tm_hour=10, tm_min=50, tm_sec=0, tm_wday=1, tm_yday=270, tm_isdst=-1)
#将"2011-09-28 10:00:00"转化为时间戳
time.mktime(time.strptime(a,'%Y-%m-%d %H:%M:%S'))
>>1317091800.0
x = time.localtime(1317091800.0)
time.strftime('%Y-%m-%d %H:%M:%S',x)
>>2011-09-27 10:50:00
import
datetime
d
=
datetime.datetime.now()
def
day_get(d):
oneday
=
datetime.timedelta(days
=
1
)
day
=
d
-
oneday
date_from
=
datetime.datetime(day.year, day.month, day.day,
0
,
0
,
0
)
date_to
=
datetime.datetime(day.year, day.month, day.day,
23
,
59
,
59
)
print
'---'
.join([
str
(date_from),
str
(date_to)])
def
week_get(d):
dayscount
=
datetime.timedelta(days
=
d.isoweekday())
dayto
=
d
-
dayscount
sixdays
=
datetime.timedelta(days
=
6
)
dayfrom
=
dayto
-
sixdays
date_from
=
datetime.datetime(dayfrom.year, dayfrom.month, dayfrom.day,
0
,
0
,
0
)
date_to
=
datetime.datetime(dayto.year, dayto.month, dayto.day,
23
,
59
,
59
)
print
'---'
.join([
str
(date_from),
str
(date_to)])
def
month_get(d):
dayscount
=
datetime.timedelta(days
=
d.day)
dayto
=
d
-
dayscount
date_from
=
datetime.datetime(dayto.year, dayto.month,
1
,
0
,
0
,
0
)
date_to
=
datetime.datetime(dayto.year, dayto.month, dayto.day,
23
,
59
,
59
)
print
'---'
.join([
str
(date_from),
str
(date_to)])