赞
踩
__author__ = '授客'import
timefrom
datetime import datefrom
datetime import timedeltafrom
datetime import datetime#####date######
获取当前本地日期(date对象)#
方法1today
= date.fromtimestamp(time.time())print('方法1:当前本地日期(date对象)\n类型:',
type(today), 'value:',
today)#
方法2today
= date.today()print('方法2:当前本地日期(date对象)\n类型:',
type(today), 'value:',
today)#
获取本地当前日期(字符串,即转date对象为对应字符串)today_str
= today.strftime('%Y-%m-%d')print('当前本地日期(字符串)\n类型:',
type(today_str), 'value:',
today_str)today_str
= today.ctime()print('当前本地日期(字符串)\n类型:',
'value:',today_str)#
转换本地当前日期为时间戳(秒)second_for_today
= int(time.mktime(today.timetuple()))print('当前本地日期对应的时间戳(秒):',
second_for_today)#
转换本地当前日期为时间戳(毫秒)millisecond_for_today
= int(time.mktime(today.timetuple())*1000)print('当前本地日期对应的时间戳(毫秒):',
millisecond_for_today)#
获取本地昨日日期yesterday
= today - timedelta(days=1)print('昨日本地日期(date对象)\n类型:',
type(yesterday), 'value:',
yesterday)#
获取本地当前日期对应的星期weekday
= today.weekday()print('当前本地日期对应的星期:',
weekday) #0~6 ->周一到周日#
时间戳(秒)转换为date对象mydate
= date.fromtimestamp(1512144000)print('时间戳(秒)转换为date对象:',
type(mydate), mydate)print('\n\n')#####datetime######
获取本地当前日期时间(datetime对象)#
方法1:date_time
= datetime.today()print('方法1:当前本地日期时间(datetime对象)\n类型:',
type(date_time), 'value:',
date_time)#
方法2:date_time
= datetime.now()print('方法2:当前本地日期时间(datetime对象)\n类型:',
type(date_time), 'value:',
date_time)#
获取本地当前日期时间(字符串,即转datetime对象为对应字符串)date_time_str
= date_time.strftime('%Y-%m-%d
%H:%M:%S')print('当前本地日期时间(字符串)类型:',
'value:',
date_time_str)#
获取本地昨日当前时间(datetime对象)yesterday_date_time
= date_time - timedelta(days=1)print('方法2:昨日本地当前时间(datetime对象)\n类型:',
type(yesterday_date_time), 'value:',
yesterday_date_time)#
转换本地当前日期时间为时间戳(秒)millisecond_for_date_time
= int(time.mktime(date_time.timetuple()))print('当前本地日期时间对应的时间戳(秒):',
millisecond_for_date_time)#
获取本地日期对应的星期weekday
= date_time.weekday()print('当前本地日期时间对应的星期:',
weekday) #0~6 ->周一到周日#
时间戳(秒)转换为datetime对象mydatetime
= datetime.fromtimestamp(1512226650)print('时间戳(秒)转换为datetime对象:',
type(mydatetime), mydatetime)#
日期时间字符串表达式转datetime对象mydatetime
= datetime.strptime('2017-12-02
22:57:30',
'%Y-%d-%m %H:%M:%S')print('日期时间字符串表达式转datetime对象\n类型:',
type(mydatetime), 'value:',
mydatetime)print('\n\n')#####time模块的time######
获取本地当前时间对应的时间戳(秒)#方法1second_for_current_time
= int(time.time())print('方法1:本地当前时间对应的时间戳(秒):',
second_for_current_time)#方法2time_tuple
= time.localtime()second_for_current_time
= int(time.mktime(time_tuple))print('方法2:本地当前时间对应的时间戳(秒):',
second_for_current_time)#
获取本地当前时间(字符串)time_tuple
= time.localtime()print('timetuple:',
time_tuple)time_str
= time.strftime('%Y-%m-%d
%H:%M:%S',time_tuple)print('本地当前时间(字符串):',
time_str)time_str
= time.asctime(time_tuple)print('本地当前时间(字符串):',
time_str)#
时间戳(秒)转换为本地timetupletime_tuple
= time.localtime(1512226650)print('时间戳转为本地timetuple:\n',
time_tuple)#
时间字符串表达式转本地timetuple元组mytimetuple
= time.strptime('2017-12-02
22:57:30',
'%Y-%d-%m %H:%M:%S')print('时间字符串表达式转本地timetuple对象:\n',
mytimetuple)struct_time
= time.strptime('30
Nov 00',
'%d %b %y')print('时间字符串表达式转本地timetuple对象:\n',
struct_time)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。