Python datetime模块的使用示例
# -*- encoding=utf-8 -*-import datetimenow = datetime.datetime.now()print('now:{}'.format(now))year = now.yearprint('year:{}'.format(year))month = now.monthprint('month:{}'.format(month))day = now.dayprint('day:{}'.format(day))hour = now.hourprint('hour:{}'.format(hour))minute = now.minuteprint('minute:{}'.format(minute))second = now.secondprint('second:{}'.format(second))

# -*- encoding=utf-8 -*-import datetimenow = datetime.datetime.now()print(’type:{}’.format(type(now)))print(’now datetime:{}’.format(now))now_string = now.strftime(’%Y-%m-%d %H:%M:%S’)print(’type:{}’.format(type(now_string)))print(’now string:{}’.format(now_string))

# -*- encoding=utf-8 -*-import datetimetime_str = ’2021-01-28 10:51:26’time_date = datetime.datetime.strptime(time_str, ’%Y-%m-%d %H:%M:%S’)print(’type:{}’.format(type(time_date)))print(time_date)

# -*- encoding=utf-8 -*-import datetimetime_str = ’2021-01-28 10:00:00’time_date = datetime.datetime.strptime(time_str, ’%Y-%m-%d %H:%M:%S’)print(’原始时间:tttt{}’.format(time_date))add_info = datetime.timedelta(days=1, hours=2, minutes=3, seconds=4)add_end = time_date + add_infoprint(’加上1天2个小时3分钟4秒后:t{}’.format(add_end))

①两个时间差
# -*- encoding=utf-8 -*-import datetimetime_str = ’2021-01-28 10:00:00’time_date = datetime.datetime.strptime(time_str, ’%Y-%m-%d %H:%M:%S’)print(’原始时间:t{}’.format(time_date))time_str = ’2021-05-29 12:12:12’time_date2 = datetime.datetime.strptime(time_str, ’%Y-%m-%d %H:%M:%S’)print(’原始时间2:t{}’.format(time_date2))time_date3 = time_date2 - time_dateprint(’时间差:{}’.format(time_date3))

②减去1天2个小时3分钟4秒(加负数)
# -*- encoding=utf-8 -*-import datetimetime_str = ’2021-01-28 10:00:00’time_date = datetime.datetime.strptime(time_str, ’%Y-%m-%d %H:%M:%S’)print(’原始时间:tttt{}’.format(time_date))add_info = datetime.timedelta(days=-1, hours=-2, minutes=-3, seconds=-4)add_end = time_date + add_infoprint(’减去1天2个小时3分钟4秒后:t{}’.format(add_end))

以上就是Python datetime模块的使用示例的详细内容,更多关于Python datetime模块的资料请关注好吧啦网其它相关文章!
相关文章:
1. CSS3使用过度动画和缓动效果案例讲解2. HTML DOM setInterval和clearInterval方法案例详解3. ASP.NET Core 5.0中的Host.CreateDefaultBuilder执行过程解析4. PHP使用Swagger生成好看的API文档5. JSP生成九九乘法表的简单实例6. 详细分析css float 属性以及position:absolute 的区别7. CSS 使用Sprites技术实现圆角效果8. jsp 使用request为页面添加静态数据的实例9. IOS苹果AppStore内购付款的服务器端php验证方法(使用thinkphp)10. Xpath语法格式总结

网公网安备