在建立现实生活项目时,与时代和日期合作是不可避免的,并且有很多用例。值得庆幸的是,Python有几个模块,可以轻松地与不同时区的日期和时代一起工作。
> 可以在github上找到本教程的代码。>。 内容:
时间模块
<span>import time as time_module </span> time_in_seconds <span>= time_module.time() </span><span>print("Time in sceconds from epoch", time_in_seconds)</span>
gmtime()函数自从时代开始以来,从秒内以秒的时间表示UTC中的struct_time。 struct_time是一种时间值序列,其命名元组接口由GMTime(),Localtime()和Strptime()返回
Time <span>in sceconds from epoch 1680712853.0801558</span>这是以上代码的输出:
localtime()函数
<span>import time as time_module </span> utc_time_in_seconds <span>= time_module.gmtime() </span><span>print("Time struct in UTC", utc_time_in_seconds)</span>
这是以上代码的输出:
Time struct <span>in UTC: time.struct_time(tm_year=2023, tm_mon=3, tm_mday=16, tm_hour=14, tm_min=47, tm_sec=28, tm_wday=3, tm_yday=75, tm_isdst=0)</span>> ctime()函数
> ctime()方法将从时期开头的几秒钟转换为字符串格式。如果没有参数传递给该函数,它将在秒内返回当前时间的时间字符串:
<span>import time as time_module </span> local_time <span>= time_module.localtime() </span><span>print("Time struct in local time:", local_time)</span>
strftime()函数
Time struct <span>in local time: time.struct_time(tm_year=2023, tm_mon=4, tm_mday=20, tm_hour=15, tm_min=46, tm_sec=15, tm_wday=3, tm_yday=75, tm_isdst=0)</span>strftime()方法将struct_time转换为给定格式参数指定的时间字符串:
这是上述代码的输出:
<span>import time as time_module </span> time_in_secs <span>= 1678671984.939945 </span> time_string <span>= time_module.ctime(time_in_secs) </span><span>print("Time string: ",time_string)</span>
sleep()函数延迟了指定数量的线程的执行
Time string: Thu Apr <span>20 01:46:24 2023</span>这是上述代码的输出:
>在上面的代码中,数字2作为睡眠()函数的参数传递,这会导致循环在执行前延迟两秒钟。输出的数字验证我们的代码。
<span>import time as time_module </span> time_tuple <span>= time_module.gmtime() </span>time_format <span>= "%y/%m/%d %I:%M:%S %p" </span> time_in_string <span>= time_module.strftime(time_format, time_tuple) </span> <span>print("Time expressed as formatted string:", time_in_string)</span>
DateTime模块提供用于操纵日期和时间的类
这些类对于时间间隔,时间和日期的轻松操纵,提取和输出格式至关重要。通常,日期和时间在Python中不被视为数据类型,但它们是DateTime模块类的日期和时间对象。 DateTime类还具有可用于处理日期和时间对象的不同方法。Time expressed as formatted string: <span>23/04/20 04:40:04 PM</span>
在Python中获取当前日期和时间
获得当前日期和时间,请从DateTime模块导入DateTime类。 DateTime类具有一个方法,即现在(),该类返回当前日期和时间:<span>import time as time_module </span> <span>for i in range(5): </span> local_time <span>= time_module.localtime() </span> seconds <span>= local_time.tm_sec </span> <span>print(seconds) </span> time_module<span>.sleep(2)</span>
这是上述代码的输出:
获得当前日期,请从DateTime模块导入日期类。日期类有一个方法,今天(),该方法返回当前日期:
>这是上述代码的输出:
> DateTime模块当前有六个类,每个类都有不同的方法来操纵日期和时间对象。这些课程列出如下:
<span>49 </span><span>51 </span><span>53 </span><span>55 </span><span>57</span>
日期对象在理想化的日历中代表日期(年,月和日) - 当前的Gregorian日历无限期地扩展了两个方向。
>可以将日期对象实例化如下:
<span>import time as time_module </span> time_in_seconds <span>= time_module.time() </span><span>print("Time in sceconds from epoch", time_in_seconds)</span>
日期对象构造函数采用三个整数参数,应在指定的范围内:
或
>示例:获取当前日期
Time <span>in sceconds from epoch 1680712853.0801558</span>要获取当前的本地日期,请使用今天的日期类()和ctime()方法:
> today()方法将返回本地日期,而ctime()方法将日期呈现为字符串。 这是以上代码的输出:
<span>import time as time_module </span> utc_time_in_seconds <span>= time_module.gmtime() </span><span>print("Time struct in UTC", utc_time_in_seconds)</span>>
可以从ISO 8601格式的日期字符串创建日期对象。使用日期类的fromisOformat()方法来执行此操作:
Time struct <span>in UTC: time.struct_time(tm_year=2023, tm_mon=3, tm_mday=16, tm_hour=14, tm_min=47, tm_sec=28, tm_wday=3, tm_yday=75, tm_isdst=0)</span>>
<span>import time as time_module </span> local_time <span>= time_module.localtime() </span><span>print("Time struct in local time:", local_time)</span>注意:ISO 8601是一种标准化格式,用于呈现日期和时间,而无需在不同区域或时区造成混淆。 ISO 8601采用格式yyyy-mm-dd。
>示例:从字符串创建日期对象
Time struct <span>in local time: time.struct_time(tm_year=2023, tm_mon=4, tm_mday=20, tm_hour=15, tm_min=46, tm_sec=15, tm_wday=3, tm_yday=75, tm_isdst=0)</span>为创建一个日期对象,将日期字符串和相应格式传递给Strptime()方法。使用返回的dateTime对象的日期()方法提取日期:
这是上述代码的输出:
>要从日期对象提取年,月和一天
<span>import time as time_module </span> time_in_secs <span>= 1678671984.939945 </span> time_string <span>= time_module.ctime(time_in_secs) </span><span>print("Time string: ",time_string)</span>这是以上代码的输出:
一个时间对象代表一天中的(本地)时间,独立于任何特定的一天,并通过tzinfo对象进行调整。
Time string: Thu Apr <span>20 01:46:24 2023</span>
可以将日期对象实例化如下:
<span>import time as time_module </span> time_in_seconds <span>= time_module.time() </span><span>print("Time in sceconds from epoch", time_in_seconds)</span>
可以在没有任何参数的情况下实例化时间对象。所有参数都是可选的,默认值为0,除了tzinfo,这不是。所有参数必须是指定范围内的整数,而tzinfo参数应为tzinfo子类的一个实例:
>当不超出范围的参数传递给构造函数时,它会提高价值。
创建一个时间对象,请从DateTime模块导入时间类。通过几个小时,分钟,秒,微秒和tzinfo的争论。请记住,所有参数都是可选的,因此,当没有参数传递给构造函数时,时间对象返回00:00:00:
Time <span>in sceconds from epoch 1680712853.0801558</span>
这是上述代码的输出:
<span>import time as time_module </span> utc_time_in_seconds <span>= time_module.gmtime() </span><span>print("Time struct in UTC", utc_time_in_seconds)</span>>示例:从ISO格式创建时间
>
Time struct <span>in UTC: time.struct_time(tm_year=2023, tm_mon=3, tm_mday=16, tm_hour=14, tm_min=47, tm_sec=28, tm_wday=3, tm_yday=75, tm_isdst=0)</span>这是上述代码的输出:
>示例:从字符串
<span>import time as time_module </span> local_time <span>= time_module.localtime() </span><span>print("Time struct in local time:", local_time)</span>创建时间对象
这是上述代码的输出:
Time struct <span>in local time: time.struct_time(tm_year=2023, tm_mon=4, tm_mday=20, tm_hour=15, tm_min=46, tm_sec=15, tm_wday=3, tm_yday=75, tm_isdst=0)</span>>示例:从时间对象
获取小时,分钟,秒和微秒
>提取数小时,分钟,秒和微秒的值,使用时间对象的小时,分钟,第二和微秒属性:<span>import time as time_module </span> time_in_secs <span>= 1678671984.939945 </span> time_string <span>= time_module.ctime(time_in_secs) </span><span>print("Time string: ",time_string)</span>
dateTime类
Time string: Thu Apr <span>20 01:46:24 2023</span>
dateTime对象是一个单个对象,其中包含来自日期对象和时间对象的所有信息。
<span>import time as time_module </span> time_tuple <span>= time_module.gmtime() </span>time_format <span>= "%y/%m/%d %I:%M:%S %p" </span> time_in_string <span>= time_module.strftime(time_format, time_tuple) </span> <span>print("Time expressed as formatted string:", time_in_string)</span>>
minyear 1 Time expressed as formatted string: <span>23/04/20 04:40:04 PM</span>
1 0
<span>import time as time_module </span> time_in_seconds <span>= time_module.time() </span><span>print("Time in sceconds from epoch", time_in_seconds)</span>
要获取当前的本地日期和时间,请使用DateTime类的NOW()方法:
Time <span>in sceconds from epoch 1680712853.0801558</span>这是上述代码的输出:
>示例:创建从ISO格式创建日期时间
<span>import time as time_module </span> utc_time_in_seconds <span>= time_module.gmtime() </span><span>print("Time struct in UTC", utc_time_in_seconds)</span>
>
Time struct <span>in UTC: time.struct_time(tm_year=2023, tm_mon=3, tm_mday=16, tm_hour=14, tm_min=47, tm_sec=28, tm_wday=3, tm_yday=75, tm_isdst=0)</span>>注意:如果将日期字符串参数传递到FromisOformat()方法不是有效的ISO格式字符串中,则提高了ValueError异常。此处的日期输出与从dateTime.now()。
>获得的结果非常相似
这是上述代码的输出:
>示例:从DateTime对象获取日期和时间属性
<span>import time as time_module </span> local_time <span>= time_module.localtime() </span><span>print("Time struct in local time:", local_time)</span>> DateTime对象提供以下属性:年,月,每日,小时,分钟,第二,微秒,tzinfo和fold。属性可以访问如下:
这是上述代码的输出:
Time struct <span>in local time: time.struct_time(tm_year=2023, tm_mon=4, tm_mday=20, tm_hour=15, tm_min=46, tm_sec=15, tm_wday=3, tm_yday=75, tm_isdst=0)</span>
注意:tzinfo的默认属性值无了,因为没有传递对象参数,并且fold将默认情况下返回0。有关折叠属性的更多信息(在Python版本3.6中引入),请参见文档。
the timedelta class<span>import time as time_module </span> time_in_secs <span>= 1678671984.939945 </span> time_string <span>= time_module.ctime(time_in_secs) </span><span>print("Time string: ",time_string)</span>
序列对象代表持续时间,两个日期或时间之间的差异。
毫秒转换为1000微秒。
一分钟转换为60秒。Time string: Thu Apr <span>20 01:46:24 2023</span>
一个小时转换为3600秒。
一周转换为7天。
>示例:创建一个timedelta对象
日期和时间格式因地区到国家和国家 /地区而异。这是因为在日期和时间格式方面存在这些差异,因此引入了ISO 8601格式,作为标准化日期和时间的一种方式。 但是,可能需要根据一个国家或地区以特定方式格式化日期和时间。
使用Strftime()方法方法签名看起来像这样:
通常,将字符串格式代码作为参数传递给strftime()方法以格式日期。某些格式代码如下:
<span>import time as time_module </span> time_in_seconds <span>= time_module.time() </span><span>print("Time in sceconds from epoch", time_in_seconds)</span>>
>%a:工作日缩写名称 - 例如太阳,蒙上等
这是上述代码的输出:
方法签名看起来像这样:
Time <span>in sceconds from epoch 1680712853.0801558</span>
将字符串格式代码作为参数传递给strptime()方法以格式日期。
><span>import time as time_module </span> utc_time_in_seconds <span>= time_module.gmtime() </span><span>print("Time struct in UTC", utc_time_in_seconds)</span>>示例:dateTime对象的字符串
>
这是上述代码的输出:
Time struct <span>in UTC: time.struct_time(tm_year=2023, tm_mon=3, tm_mday=16, tm_hour=14, tm_min=47, tm_sec=28, tm_wday=3, tm_yday=75, tm_isdst=0)</span>与TimeDelta
一起工作
> python中的时间介绍类用于计算日期之间的差异,计算特定日期之间的时间差,并使用特定的时间单位(例如周或小时)进行其他计算。>
<span>import time as time_module </span> local_time <span>= time_module.localtime() </span><span>print("Time struct in local time:", local_time)</span>这是上述代码的输出:
在上面的示例中,我们首先获得当前的本地日期和时间,以及7天的时间码对象。由于TimeDERTA支持诸如添加之类的操作,因此我们添加了DateTime对象和TimeDelta对象,以在7天内获得未来的一天。如果我们的当前日期是2023-04-20,则日期将在2023-04-27之内。
Time struct <span>in local time: time.struct_time(tm_year=2023, tm_mon=4, tm_mday=20, tm_hour=15, tm_min=46, tm_sec=15, tm_wday=3, tm_yday=75, tm_isdst=0)</span>>示例:计算两个序列对象之间的差异
这是上述代码的输出:
使用时区
>示例:将DateTime对象从一个时区转换为另一个时区
结论 >
可以在github >。
<span>import time as time_module
</span>
time_in_seconds <span>= time_module.time()
</span><span>print("Time in sceconds from epoch", time_in_seconds)</span>
在上面的代码段中,我们创建了两个TimeDelta对象,即time_delta1和time_delta2,并计算了它们之间的差异。
Time <span>in sceconds from epoch 1680712853.0801558</span>
这是上述代码的输出:<span>import time as time_module
</span>
utc_time_in_seconds <span>= time_module.gmtime()
</span><span>print("Time struct in UTC", utc_time_in_seconds)</span>
>
ZoneInfo是一个内置的Python模块,用于使用时区。 这是以上代码的输出:
Time struct <span>in UTC: time.struct_time(tm_year=2023, tm_mon=3, tm_mday=16, tm_hour=14, tm_min=47, tm_sec=28, tm_wday=3, tm_yday=75, tm_isdst=0)</span>
首先,我们从DateTime模块和Zoneinfo模块中导入DateTime类。我们创建一个ZoneInfo对象,然后创建一个DateTime对象,但是这次我们将TimeZone对象TZ传递给NOW()方法。 <span>import time as time_module
</span>
local_time <span>= time_module.localtime()
</span><span>print("Time struct in local time:", local_time)</span>
>
>要在时区之间转换,我们使用DateTime对象的AstimeZone()方法,以新的时区对象传递。 AstimeZone()返回一个带有更新的时区信息的新的DateTime对象。 Time struct <span>in local time: time.struct_time(tm_year=2023, tm_mon=4, tm_mday=20, tm_hour=15, tm_min=46, tm_sec=15, tm_wday=3, tm_yday=75, tm_isdst=0)</span>
<span>import time as time_module
</span>
time_in_secs <span>= 1678671984.939945
</span>
time_string <span>= time_module.ctime(time_in_secs)
</span><span>print("Time string: ",time_string)</span>
跟踪时间是我们日常生活的重要方面,这也转化为编程。当我们构建现实世界项目时,总是需要保留登录和登录等用户活动的时间日志,以及其他用例。在在线生成的内容上放置时间戳并根据用户的区域或时区显示时间和日期也很重要。
经常询问有关Python日期和时间的问题(常见问题解答)
>如何将字符串转换为python中的日期?>在Python中,您可以使用DateTime模块的Strptime()函数将字符串转换为日期。此功能需要两个参数:要转换的字符串和与字符串的日期格式匹配的格式代码。例如,如果您的格式“ yyyy-mm-dd”中有一个日期字符串“ 2022-03-01”,则可以将其转换为这样的日期:datetime import import import
date_object = dateTime.strptime(date_string,“%y-%m-%d”)
>
我如何获得python的当前日期和时间?从dateTime import import dateTime
>我如何在python中格式化一个日期? fort_date_date = currated_date = current_dateTime。 strftime(“%y-%m-%d”)
print(formatted_date)
>我如何从python的日期中添加或减去天数? TIMEDERTA类,您可以用来添加或减去日期的一定天数。以下是您可以使用它的方法:
print> print(new_date)
如何比较python中的两个日期?以下是一个示例:
>来自dateTime import import dateTime
dateTime(2022,3,1)
>我如何在Python中获得一周的一天?作为整数的一周(周一为0,星期日为6)。以下是您可以使用它的方法:
>来自dateTime import dateTime
current_date = dateTime.now()
current_date = dateTime.now()
timestamp = current_date.timestamp()
从dateutil import import parser
date_string =“ 2022-03-01”
date_object = parser.parser.parse.parse(date_string)(date_string)
print(date_object)
>如何在Python中获得当前时间?如果您只需要时间,则可以使用Time()函数这样:
>如何将时间戳转换为python中的日期?
>来自DateTime Import DateTime
以上是了解python日期和时间,示例的详细内容。更多信息请关注PHP中文网其他相关文章!