Python 官方文档:入门教程 => 点击学习
目录1、时间戳转换为指定格式日期2、将字符串的时间转换为时间戳3、Datetime详细介绍4、获得三天前的时间的方法5、使用datetime模块来获取当前的日期和时间1、时间戳转换为
import timet = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())print(t)timestamp = time.time()tuple_time = time.localtime(timestamp)print(tuple_time)print(time.strftime("%Y-%m-%d %H:%M:%S", tuple_time))
import timeimport datetimetime_str = "2023-02-19 23:07:21"time_struct = time.strptime(time_str, "%Y-%m-%d %H:%M:%S")print(time_struct)print(time.mktime(time_struct))print(int(time.mktime(time_struct)))
python提供了多个内置模块用于操作日期时间,像calendar,time,datetime。time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致。相比于time模块,datetime模块的接口则更直观、更容易调用。今天就来讲讲datetime模块。
datetime模块定义了两个常量:datetime.MINYEAR和datetime.MAXYEAR,分别表示datetime所能表示的最 小、最大年份。其中,MINYEAR = 1,MAXYEAR = 9999。(对于偶等玩家,这个范围已经足够用矣~~)
注 :上面这些类型的对象都是不可变(immutable)的。
下面详细介绍这些类的使用方式。
date类
date类表示一个日期。日期由年、月、日组成(地球人都知道~~)。date类的构造函数如下:
class datetime.date(year, month, day):参数的意义就不多作解释了,只是有几点要注意一下:
date类定义了一些常用的类方法与类属性,方便我们操作:
import time
import datetime
time_str = "2023-02-19 23:07:21"
time_struct = time.strptime(time_str, "%Y-%m-%d %H:%M:%S")
print(datetime.datetime.now())
computed_time = datetime.datetime.now() - datetime.timedelta(days=3)
print(computed_time)
timestamp = time.mktime(computed_time.timetuple())
print(timestamp)
time_str = time.strftime("%Y-%m-%d %H:%M:%S", computed_time.timetuple())
print(time_str)
import time
import datetime
time_str = "2023-02-19 23:07:21"
time_struct = time.strptime(time_str, "%Y-%m-%d %H:%M:%S")
currentDate = datetime.datetime.now()
print(time.strftime("%Y-%m-%d %H:%M:%S", currentDate.timetuple()))
print(currentDate.year)
print(currentDate.month)
print(currentDate.day)
print(currentDate.hour)
print(currentDate.minute)
print(currentDate.second)
date提供的实例方法和属性:
import time
from datetime import datetime, date
time_str = "2023-02-19 23:07:21"
time_struct = time.strptime(time_str, "%Y-%m-%d %H:%M:%S")
now = date(2023, 1, 18)
print(time.strftime("%Y-%m-%d %H:%M:%S", now.timetuple()))
print(now.weekday())
print(now.isoformat())
date还对某些操作进行了重载,它允许我们对日期进行如下一些操作:
注: 对日期进行操作时,要防止日期超出它所能表示的范围。
import time
from datetime import datetime, date, timedelta
time_str = "2023-02-19 23:07:21"
time_struct = time.strptime(time_str, "%Y-%m-%d %H:%M:%S")
currentDate = date.today()
print(time.strftime("%Y-%m-%d %H:%M:%S", currentDate.timetuple()))
time_delta = currentDate - timedelta(days=1)
print(time.strftime("%Y-%m-%d %H:%M:%S", time_delta.timetuple()))
print(currentDate > time_delta)
print(currentDate - time_delta)
print(time_delta + timedelta(days=3))
datetime类提供的实例方法与属性(很多属性或方法在date和time中已经出现过,在此有类似的意义,这里只罗列这些方法名,具体含义不再逐个展开介绍,可以参考上文对date与time类的讲解。):
像date一样,也可以对两个datetime对象进行比较,或者相减返回一个时间间隔对象,或者日期时间加上一个间隔返回一个新的日期时间对象。
到此这篇关于Python time时间格式化和设置时区实现代码详解的文章就介绍到这了,更多相关Python time时间格式化内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Pythontime时间格式化和设置时区实现代码详解
本文链接: https://www.lsjlt.com/news/197379.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0