Python date,datetime,time等相关操作总结

datedatetimetime等相关操作总结

 

by:授客 QQ1033553122

测试环境:

Python版本:Python 3.3.2

 

 

代码实践:

__author__ = '授客'

import time
from datetime import date
from datetime import timedelta
from datetime import datetime

#####date#####
# 获取当前本地日期(date对象)
# 方法1
today = date.fromtimestamp(time.time())
print('方法1:当前本地日期(date对象) 类型:', type(today), 'value', today)

# 方法2
today = date.today()
print('方法2:当前本地日期(date对象) 类型:', type(today), 'value', today)

# 获取本地当前日期(字符串,即转date对象为对应字符串)
today_str = today.strftime('%Y-%m-%d')
print('当前本地日期(字符串) 类型:', type(today_str), 'value', today_str)

today_str = today.ctime()
print('当前本地日期(字符串) 类型:', '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对象) 类型:', type(yesterday), 'value', yesterday)

# 获取本地当前日期对应的星期
weekday = today.weekday()
print('当前本地日期对应的星期:', weekday) #0~6 ->周一到周日

# 时间戳()转换为date对象
mydate = date.fromtimestamp(1512144000)
print('时间戳()转换为date对象:', type(mydate), mydate)


print(' ')
#####datetime#####
# 获取本地当前日期时间(datetime对象)
# 方法1
date_time = datetime.today()
print('方法1:当前本地日期时间(datetime对象) 类型:', type(date_time), 'value', date_time)

# 方法2
date_time = datetime.now()
print('方法2:当前本地日期时间(datetime对象) 类型:', 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对象) 类型:', 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对象 类型:', type(mydatetime), 'value', mydatetime)


print(' ')
#####time模块的time#####
# 获取本地当前时间对应的时间戳()
#方法1
second_for_current_time = int(time.time())
print('方法1:本地当前时间对应的时间戳()', second_for_current_time)

#方法2
time_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)

# 时间戳()转换为本地timetuple
time_tuple = time.localtime(1512226650)
print('时间戳转为本地timetuple ', time_tuple)

# 时间字符串表达式转本地timetuple元组
mytimetuple = time.strptime('2017-12-02 22:57:30', '%Y-%d-%m %H:%M:%S')
print('时间字符串表达式转本地timetuple对象: ', mytimetuple)

struct_time = time.strptime('30 Nov 00', '%d %b %y')
print('时间字符串表达式转本地timetuple对象: ', struct_time)

 

# 获取当前日期所在周的周一
weekday1_date = date.today() - timedelta(days=date.today().isoweekday()) + timedelta(days=1)
weekday1 = time.strftime('%Y%m%d', weekday1_date.timetuple())


# 获取当前日期所在周的周七
weekday7_date = date.today() - timedelta(days=date.today().isoweekday()) + timedelta(days=7)
weekday7 = time.strftime('%Y%m%d', weekday7_date.timetuple())

dateRange1 = weekday1 + '-' + weekday7
print(dateRange1)

 

运行结果:

方法1:当前本地日期(date对象)

类型: value 2017-12-04

方法2:当前本地日期(date对象)

类型: value 2017-12-04

当前本地日期(字符串)

类型: value 2017-12-04

当前本地日期(字符串)

类型: value Mon Dec  4 00:00:00 2017

当前本地日期对应的时间戳() 1512316800

当前本地日期对应的时间戳(毫秒) 1512316800000

昨日本地日期(date对象)

类型: value 2017-12-03

当前本地日期对应的星期: 0

时间戳()转换为date对象: 2017-12-02

 

 

 

方法1:当前本地日期时间(datetime对象)

类型: value 2017-12-04 00:09:24.531363

方法2:当前本地日期时间(datetime对象)

类型: value 2017-12-04 00:09:24.531363

当前本地日期时间(字符串)类型: value 2017-12-04 00:09:24

方法2:昨日本地当前时间(datetime对象)

类型: value 2017-12-03 00:09:24.531363

当前本地日期时间对应的时间戳() 1512317364

当前本地日期时间对应的星期: 0

时间戳()转换为datetime对象: 2017-12-02 22:57:30

日期时间字符串表达式转datetime对象

类型: value 2017-02-12 22:57:30

 

 

 

方法1:本地当前时间对应的时间戳() 1512317364

方法2:本地当前时间对应的时间戳() 1512317364

timetuple time.struct_time(tm_year=2017, tm_mon=12, tm_mday=4, tm_hour=0, tm_min=9, tm_sec=24, tm_wday=0, tm_yday=338, tm_isdst=0)

本地当前时间(字符串): 2017-12-04 00:09:24

本地当前时间(字符串): Mon Dec  4 00:09:24 2017

时间戳转为本地timetuple

 time.struct_time(tm_year=2017, tm_mon=12, tm_mday=2, tm_hour=22, tm_min=57, tm_sec=30, tm_wday=5, tm_yday=336, tm_isdst=0)

时间字符串表达式转本地timetuple对象:

 time.struct_time(tm_year=2017, tm_mon=2, tm_mday=12, tm_hour=22, tm_min=57, tm_sec=30, tm_wday=6, tm_yday=43, tm_isdst=-1)

时间字符串表达式转本地timetuple对象:

 time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)

20171204-20171210

 

 

 

原文地址:https://www.cnblogs.com/shouke/p/10157534.html