python的time和datetime
来源:互联网 发布:运放芯片淘宝有真的吗 编辑:程序博客网 时间:2024/06/03 05:14
time在 Python 文档里,time是归类在Generic Operating System Services中,换句话说, 它提供的功能是更加接近于操作系统层面的。通读文档可知,time 模块是围绕着 UnixTimestamp 进行的。该模块主要包括一个类 struct_time,另外其他几个函数及相关常量。 需要注意的是在该模块中的大多数函数是调用了所在平台C library的同名函数,
所以要特别注意有些函数是平台相关的,可能会在不同的平台有不同的效果。另外一点是,由于是基于Unix Timestamp,所以其所能表述的日期范围被限定在 1970 - 2038 之间,如果你写的代码需要处理在前面所述范围之外的日期,那可能需要考虑使用datetime模块更好。文档解释比较费劲,
datetime 基于 time 进行了封装,提供了更多实用的函数
def faith_time(): """ There is time of intruduce, 6 parts such as: 1. The three format of time convert 2. now time: 3. time stamp: 4. formating time: 5. convert the origin time to time stamp: 6. %y 两位数的年份表示(00-99) %Y 四位数的年份表示(000-9999) %m 月份(01-12) %d 月内中的一天(0-31) %H 24小时制小时数(0-23) %I 12小时制小时数(01-12) %M 分钟数(00=59) %S 秒(00-59) %a 本地简化星期名称 %A 本地完整星期名称 %b 本地简化的月份名称 %B 本地完整的月份名称 %c 本地相应的日期表示和时间表示 %j 年内的一天(001-366) %p 本地A.M.或P.M.的等价符 %U 一年中的星期数(00-53)星期天为星期的开始 %w 星期(0-6),星期天为星期的开始 %W 一年中的星期数(00-53)星期一为星期的开始 %x 本地相应的日期表示 %X 本地相应的时间表示 %Z 当前时区的名称 %% %号本身 """ print('time.ctime()', time.ctime(), '\n') print('time.gmtime()', time.gmtime(), '\n') # The three format of time convert tn1 = time.time()# float tn2 = time.localtime(tn1) # float --> tuple tuple tn3 = time.strftime('%Y-%m-%d %H:%M:%S', tn2) # tuple --> str tn4 = time.strptime(tn3, '%Y-%m-%d %H:%M:%S') # str --> tuple tn5 = time.mktime(tn4)# tuple --> float # tn6 = datetime(*tn4[0:6]) print( 'the type of tn1:', type(tn1), '\n', 'the value of tn1:', tn1, '\n', 'the type of tn2:', type(tn2), '\n', 'the value of tn2:', tn2, '\n', 'the type of tn3:', type(tn3), '\n', 'the value of tn3:', tn3, '\n', 'the type of tn4:', type(tn4), '\n', 'the value of tn4:', tn4, '\n', 'the type of tn5:', type(tn5), '\n', 'the value of tn5:', tn5, '\n', ) # now time t = time.time() print('All of origin time infomation:', '\n', 'The t type is:', type(t), '\n', 'The origin:', t, '\n', 'Turn int is:', int(t), '\n', 'Turn localtime is:', time.localtime(t), '\n', 'Turn new type is:', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(t)), '\n', ) # time stamp ts = 1234567890 print('Turn is: ', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(ts)), '\n') # formating time print('The first type is:', time.strftime('%Y-%m-%d', time.localtime(t)), '\n', 'The second type is:', time.strftime('%Y%m%d', time.localtime(t)), '\n', 'The second type is:', time.strftime('%Y%m%d%H%M%S', time.localtime(t)), '\n', 'The second type is:', time.strftime('%Y/%m/%d/%H/%M/%S', time.localtime(t)), '\n', ) # convert the origin time to time stamp or_time = '2016-03-15 13:32:01' array_time = time.strptime(or_time, '%Y-%m-%d %H:%M:%S') print('The result is: ', time.mktime(array_time), '\n') """ There is datetime of intruduce, """ # now datetime dn = datetime.datetime.now() print('Origin datetime: ', dn, '\n', 'strf is: ', dn.strftime('%Y-%m-%d %H:%M:%S %A'), '\n', 'isoformat:', dn.isoformat(), '\n', 'year:', dn.year, '\n', 'hour:', dn.hour, '\n', 'microsecond', dn.microsecond, '\n', 'weekday (0-6)=(Mon-Sun): ', dn.weekday(), '\n', ) # get time of 3 days ago: three_day_ago = (datetime.datetime.now() - datetime.timedelta(days=3)) print('time is', three_day_ago.strftime('%Y-%m-%d %H:%M:%S'), '\n',) # 给定时间戳, 计算该时间的几天前时间 time_stamp = 1234567890 pt = time.localtime(time_stamp) pl = time.strftime('%Y-%m-%d %H:%M:%S', pt) dateArray = datetime.datetime.utcfromtimestamp(time_stamp)# turn datetime three_day = dateArray - datetime.timedelta(days=2) print('time_stamp is: ', pl, '\n', 'three_day_ago is: ', three_day, '\n', 'the type of time.location is:', type(pt), '\n', ) # yesterday today tomorrow today = datetime.date.today() yesterday = today - datetime.timedelta(days=1) tomorrow = today + datetime.timedelta(days=1) print('today is:', today, '\n', 'yesterday is: ', yesterday, '\n', 'tomorrow is: ', tomorrow, '\n', )
阅读全文
0 0
- python的time和datetime
- python---time和datetime
- Python datetime 和time 模块
- python time和datetime区别
- time,datetime------python操作日期和时间的方法
- Python中time和datetime模块对时间的操作
- python time和datetime的常用转换处理
- python之time和datetime模块
- python datetime 和time模块方法汇总
- time和datetime时间戳---python
- Python:时间模块:time和datetime
- python time模块和datetime模块详解
- python time模块和datetime模块详解
- Python的time模块datetime模块
- python---time,datetime模块
- python datetime time
- python datetime、time、date
- Python datetime / time 转换
- 送你6份最新开源代码!含NLP、ML、计算机视觉方向(附代码&论文)
- 【技术干货】Python之模拟http请求测试
- js 立即执行函数 问题
- 数组元素循环右移问题 (20)
- 多级继承(继承关系)中构造方法是的调用规则
- python的time和datetime
- Jedis的使用
- 利用jquery给指定的table动态添加一行、删除一行
- 566. Reshape the Matrix
- Centos7vsftp虚拟用户
- java:if (i == 1)和if (1 == i)有区别吗?
- 成绩转换
- 6经典卷积神经网络模型之AlexNet-6.1用于ImageNet的AlexNet网络计算图设计
- 高性能JavaScript模板引擎template.js原理解析