Python 日期和时间用法超强总结

发表于:2022-10-18 09:19

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:萝卜大杂烩    来源:萝卜大杂烩

  时间无疑是生活各个方面中最关键的因素之一,因此,记录和跟踪时间变得非常重要。在 Python 中,可以通过其内置库跟踪日期和时间。今天我们来介绍关于 Python 中的日期和时间,一起来了解如何使用time和datetime模块查找和修改日期和时间。
  Python 中处理日期和时间的模块
  Python 提供了time和datetime模块,可以帮助我们轻松获取和修改日期和时间,下面让我们来逐一了解一下。
  time 模块
  该模块包括使用时间执行各种操作所需的所有与时间相关的功能,它还允许我们访问多种用途所需的时钟类型。
  内置函数:
  请看下表,它描述了时间模块的一些重要内置功能。
  代码格式化:
  在用示例解释每个函数之前,先看一下所有合法的格式化代码的方式:
  struct_time 类具有以下属性:
  现在让我们看几个 time 模块的例子。
  使用time模块查找日期和时间
  使用上表中描述的内置函数和格式化代码,可以在 Python 中轻松获取日期和时间。
  import time
  #time
  a=time.time()           #total seconds since epoch
  print("Seconds since epoch :",a,end='n----------n')
  #ctime
  print("Current date and time:")
  print(time.ctime(a),end='n----------n') 
  #sleep
  time.sleep(1)     #execution will be delayed by one second
  #localtime
  print("Local time :")
  print(time.localtime(a),end='n----------n')
  #gmtime
  print("Local time in UTC format :")
  print(time.gmtime(a),end='n-----------n')
  #mktime
  b=(2019,8,6,10,40,34,1,218,0)
  print("Current Time in seconds :")
  print( time.mktime(b),end='n----------n')
  #asctime
  print("Current Time in local format :")
  print( time.asctime(b),end='n----------n')
  #strftime
  c = time.localtime() # get struct_time
  d = time.strftime("%m/%d/%Y, %H:%M:%S", c)
  print("String representing date and time:")
  print(d,end='n----------n')
  #strptime
  print("time.strptime parses string and returns it in struct_time format :n")
  e = "06 AUGUST, 2019"
  f = time.strptime(e, "%d %B, %Y")
  print(f)
  Output:
  Seconds since epoch : 1565070251.7134922
  ———-
  Current date and time:
  Tue Aug 6 11:14:11 2019
  ———-
  Local time :
  time.struct_time(tm_year=2019, tm_mon=8, tm_mday=6, tm_hour=11, tm_min=14, tm_sec=11, tm_wday=1, tm_yday=218, tm_isdst=0)
  ———-
  Local time in UTC format :
  time.struct_time(tm_year=2019, tm_mon=8, tm_mday=6, tm_hour=5, tm_min=44, tm_sec=11, tm_wday=1, tm_yday=218, tm_isdst=0)
  ———–
  Current Time in seconds :
  1565068234.0
  ———-
  Current Time in local format :
  Tue Aug 6 10:40:34 2019
  ———-
  String representing date and time:
  08/06/2019, 11:14:12
  ———-
  time.strptime parses string and returns it in struct_time format :
  time.struct_time(tm_year=2019, tm_mon=8, tm_mday=6, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=218, tm_isdst=-1)
  datetime 模块
  与time模块类似,datetime模块包含处理日期和时间所必需的所有方法。
  内置功能:
  下表介绍了本模块中的一些重要功能:
  使用 datetime 查找日期和时间
  现在,让我们尝试实现这些函数,以使用datetime模块在 Python 中查找日期和时间。
  import datetime
  #datetime constructor
  print("Datetime constructor:n")
  print(datetime.datetime(2019,5,3,8,45,30,234),end='n----------n')
   
  #today
  print("The current date and time using today :n")
  print(datetime.datetime.today(),end='n----------n')
   
  #now
  print("The current date and time using today :n")
  print(datetime.datetime.now(),end='n----------n')
   
  #date
  print("Setting date :n")
  print(datetime.date(2019,11,7),end='n----------n')
    
  #time
  print("Setting time :n")
  print(datetime.time(6,30,23),end='n----------n')
   
  #date.fromtimestamp
  print("Converting seconds to date and time:n")
  print(datetime.date.fromtimestamp(23456789),end='n----------n')
   
  #timedelta
  b1=datetime.timedelta(days=30, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=4, weeks=8)
  b2=datetime.timedelta(days=3, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=4, weeks=8)
  b3=b2-b1
  print(type(b3))
  print("The resultant duration = ",b3,end='n----------n')
   
  #attributes
  a=datetime.datetime.now()   #1
  print(a)
  print("The year is :",a.year)
   
  print("Hours :",a.hour)
  Output:
  Datetime constructor:
  2019-05-03 08:45:30.000234
  ———-
  The current date and time using today :
  2019-08-06 13:09:56.651691
  ———-
  The current date and time using today :
  2019-08-06 13:09:56.651691
  ———-
  Setting date :
  2019-11-07
  ———-
  Setting time :
  06:30:23
  ———-
  Converting seconds to date and time:
  1970-09-29
  ———-
  <class ‘datetime.timedelta’>
  The resultant duration = -27 days, 0:00:00
  ———-
  2019-08-06 13:09:56.653694
  The year is : 2019
  Hours : 13
  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号