python 日期和时间操作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 日期和时间操作相关的知识,希望对你有一定的参考价值。

#########################################
### GET CURRENT DATETIME ###
#########################################

from datetime import datetime, date, time

datetime.now()

#########################################
### DATE AND TIME PARTS ###
#########################################

from datetime import datetime, date, time

dt = datetime(2020, 10, 29, 20, 30, 21, 235335)

dt # result is datetime.datetime(2020, 10, 29, 20, 30, 21)
dt.year # result is 2020
dt.day # result is 29
dt.hour # result is 20
dt.minute # result is 30
dt.second # result is 21
dt.microsecond # result is 235335
dt.year, dt.month, dt.day # result is tuple (2020, 10, 29)

dt.date() # result is datetime.date(2020, 10, 29)
dt.time() # result is datetime.time(20, 30, 21, 235335)

#########################################
### USING strptime TO CONVERT STRING TO DATETIME, AND DATETIME TO FORMATTED STRING ###
#########################################

from datetime import datetime, date, time

# get datetime from a string
datetime.strptime('20190701', '%Y%m%d') # result is datetime.datetime(2019, 7, 1, 0, 0)
datetime.strptime('190701', '%y%m%d') # result is datetime.datetime(2019, 7, 1, 0, 0)

# format datetime into various string formats
dt.strftime('%m/%d/%Y %H:%M') # result is '10/29/2020 20:30'
dt.strftime('%m/%d/%y %H:%M:%f') # result is '10/29/20 20:30:235335'
dt.strftime('%Y-%m-%d %H:%M') # result is '2020-10-29 20:30'
dt.strftime('%A, %B %d, %Y') # result is 'Thursday, October 29, 2020'
dt.strftime('Year: %Y, Minute: %m, Day:%d, Hour: %H, Minute: %M, Microsecond: %f') # result is 'Year: 2020, Minute: 10, Day:29, Hour: 20, Minute: 30, Microsecond: 235335'

'''
The full list of date character abbreviations:


    %a: Returns the first three characters of the weekday, e.g. Wed.
    %A: Returns the full name of the weekday, e.g. Wednesday.
    %B: Returns the full name of the month, e.g. September.
    %w: Returns the weekday as a number, from 0 to 6, with Sunday being 0.
    %m: Returns the month as a number, from 01 to 12.
    %p: Returns AM/PM for time.
    %y: Returns the year in two-digit format, that is, without the century. For example, "18" instead of "2018".
    %f: Returns microsecond from 000000 to 999999.
    %H: Returns the hour (24 hour clock)
    %h: Returns the hour (12 hour clock)
    %F: Returns shortcut for %Y-%m-%d (2019-4-18)
    %D: Returns shortcut for %m/%d/%Y (04/18/19)
    %Z: Returns the timezone.
    %z: Returns UTC offset.
    %j: Returns the number of the day in the year, from 001 to 366.
    %W: Returns the week number of the year, from 00 to 53, with Monday being counted as the first day of the week.
    %U: Returns the week number of the year, from 00 to 53, with Sunday counted as the first day of each week.
    %c: Returns the local date and time version.
    %x: Returns the local version of date.
    %X: Returns the local version of time.

'''

#########################################
### REPLACING DATE AND TIME PARTS ###
#########################################

from datetime import datetime, date, time

date1 = datetime(2019, 10, 29, 20, 30, 21, 352355) # result is date1 = datetime(2019, 10, 29, 20, 30, 21, 352355)

date1.replace(minute = 8, second = 0) # result is datetime.datetime(2019, 10, 29, 20, 8, 0, 352355)


#########################################
### DATE AND TIME ARITHEMTIC ###
#########################################

from datetime import datetime, date, time, timedelta

date1 = datetime(2020, 10, 29, 20, 30, 21, 235335)
date2 = datetime(2022, 12, 1, 20, 15, 21, 1214)

# timedelta gives you the difference between two date objects showing the number of days and the number of seconds 
date2 - date1 # result is datetime.timedelta(762, 85499, 765879)

# you can get the difference in time in different units
(date2 - date1).days # result is 762
(date2 - date1).days / 365 # result is 2.0876712328767124, or just over 2 years
(date2 - date1).days // 365 # result is 2 years
(date2 - date1).total_seconds() # result is 65922299.765879, the total time as seconds
(date2 - date1).total_seconds() / 60 # result is 1098704.9960979833, the total time as minutes
((date2 - date1).total_seconds() / 60) / 60 # result is 18311.74993496639, the total time as hours
date2.year - date1.year # result is 2

# timedelta can be used to add various units of time to a date object
date1 + timedelta(days = 1) # result is datetime.datetime(2020, 10, 30, 20, 30, 21, 235335)
date1 + timedelta(hours = 2) # result is datetime.datetime(2020, 10, 29, 22, 30, 21, 235335)
date1 - timedelta(minutes= 32432) # result is datetime.datetime(2020, 10, 7, 7, 58, 21, 235335)
date1 + timedelta(seconds = 239) # result is datetime.datetime(2020, 10, 29, 20, 34, 20, 235335)
date1 - timedelta(microseconds = 3243240) # result is datetime.datetime(2020, 10, 29, 20, 30, 17, 992095)
date1 - timedelta(hours = 4, microseconds = 3243240) # result is datetime.datetime(2020, 10, 29, 16, 30, 17, 992095)


以上是关于python 日期和时间操作的主要内容,如果未能解决你的问题,请参考以下文章

python 日期和时间操作

python 日期时间和xlwt结合的相关操作

Python标准库:datetime 时间和日期模块 —— 时间的获取和操作详解

全网最全深度盘点 Python 日期时间处理操作

捋一捋操作python日期时间处理(上)

捋一捋操作python日期时间处理(上)