又肝了3天,整理了80个Python DateTime 例子,必须收藏!

文章很长,高低要忍一下,如果忍不了,那就收藏吧,总会用到的

萝卜哥也贴心的做成了PDF,在文末获取!

使用 time 模块展示当前日期和时间 import time from time import gmtime, strftime t = time.localtime() print (time.asctime(t)) print(strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())) print(strftime("%A", gmtime())) print(strftime("%D", gmtime())) print(strftime("%B", gmtime())) print(strftime("%y", gmtime())) # Convert seconds into GMT date print(strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime(1234567890))) 复制代码

Output:

Sun May 7 09:30:37 2017 Sun, 07 May 2017 04:00:37 +0000 Sunday 05/07/17 May 17 Fri, 13 Feb 2009 23:31:30 +0000 复制代码 将天、小时、分钟转换为秒 SECONDS_PER_MINUTE = 60 SECONDS_PER_HOUR = 3600 SECONDS_PER_DAY = 86400 #Read the inputs from user days = int(input("Enter number of Days: ")) hours = int(input("Enter number of Hours: ")) minutes = int(input("Enter number of Minutes: ")) seconds = int(input("Enter number of Seconds: ")) #Calculate the days, hours, minutes and seconds total_seconds = days * SECONDS_PER_DAY total_seconds = total_seconds + ( hours * SECONDS_PER_HOUR) total_seconds = total_seconds + ( minutes * SECONDS_PER_MINUTE) total_seconds = total_seconds + seconds #Display the result print("Total number of seconds: ","%d"%(total_seconds)) 复制代码

Output:

Enter number of Days: 5 Enter number of Hours: 36 Enter number of Minutes: 24 Enter number of Seconds: 15 Total number of seconds: 563055 复制代码 使用 Pandas 获取当前日期和时间 import pandas as pd print(pd.datetime.now()) print(pd.datetime.now().date()) print(pd.datetime.now().year) print(pd.datetime.now().month) print(pd.datetime.now().day) print(pd.datetime.now().hour) print(pd.datetime.now().minute) print(pd.datetime.now().second) print(pd.datetime.now().microsecond) 复制代码

Output:

2018-01-19 16:08:28.393553 2018-01-19 2018 1 19 16 8 28 394553 复制代码 将字符串转换为日期时间对象 from datetime import datetime from dateutil import parser d1 = "Jan 7 2015 1:15PM" d2 = "2015 Jan 7 1:33PM" # If you know date format date1 = datetime.strptime(d1, \'%b %d %Y %I:%M%p\') print(type(date1)) print(date1) # If you don\'t know date format date2 = parser.parse(d2) print(type(date2)) print(date2) 复制代码

Output:

class \'datetime.datetime\' 2015-01-07 13:15:00 class \'datetime.datetime\' 2015-01-07 13:33:00 复制代码 以毫秒为单位获取当前时间 import time milliseconds = int(round(time.time() * 1000)) print(milliseconds) 复制代码

Output:

1516364270650 复制代码 以 MST、EST、UTC、GMT 和 HST 获取当前日期时间 from datetime import datetime from pytz import timezone mst = timezone(\'MST\') print("Time in MST:", datetime.now(mst)) est = timezone(\'EST\') print("Time in EST:", datetime.now(est)) utc = timezone(\'UTC\') print("Time in UTC:", datetime.now(utc)) gmt = timezone(\'GMT\') print("Time in GMT:", datetime.now(gmt)) hst = timezone(\'HST\') print("Time in HST:", datetime.now(hst)) 复制代码

Output:

Time in MST: 2017-01-19 06:06:14.495605-07:00 Time in EST: 2017-01-19 08:06:14.496606-05:00 Time in UTC: 2017-01-19 13:06:14.496606+00:00 Time in GMT: 2017-01-19 13:06:14.496606+00:00 Time in HST: 2017-01-19 03:06:14.497606-10:00 复制代码 从给定的日期当中获取星期几 import datetime dayofweek = datetime.date(2010, 6, 16).strftime("%A") print(dayofweek) # weekday Monday is 0 and Sunday is 6 print("weekday():", datetime.date(2010, 6, 16).weekday()) # isoweekday() Monday is 1 and Sunday is 7 print("isoweekday()", datetime.date(2010, 6, 16).isoweekday()) dayofweek = datetime.datetime.today().strftime("%A") print(dayofweek) print("weekday():", datetime.datetime.today().weekday()) print("isoweekday()", datetime.datetime.today().isoweekday()) 复制代码

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzppdz.html