datetime module of Python time module

Table of contents

Introduction

Function introduction and application

date: date class

1. Get the current time

 2. Properties of date objects

3. Conversion of time and timestamp in the date class:

4. Modify the date using the replace method 

time: time class

 time class operations

datetime: date time class

timedelta: time interval, that is, the length of time between two time points


Introduction

Doing functions in project development often uses operations about time. For example, scheduled tasks for membership expiration, and delayed execution of some codes. The three commonly used time modules in python are: time , calendar , and datetime modules. Today is the datetime module.

There are three formats for representing time in python: timestamp, tuple, and formatted time. Similar to the time module, the datetime module can also convert the datetime type into these three types.

Function introduction and application

date: date class

Common attributes: year/month/day

1. Get the current time

import datetime
today = datetime.datetime.today()
today1 = datetime.datetime.now()

 result:

 2. Properties of date objects

import datetime

# 这两种都可以
today = datetime.datetime.today()
# today1 = datetime.datetime.now()

print("当前日期:", today)  # 当前日期
print("当前日期(字符串):", today.ctime())  # 返回日期的字符串
print("时间元组信息:", today.timetuple())  # 当前日期的时间元组信息
print("年:", today.year)  # 返回today对象的年份
print("月:", today.month)  # 返回today对象的月份
print("日:", today.day)  # 返回today对象的日
print("星期:", today.weekday())  # 0代表星期一,类推
print("公历序数:", today.toordinal())  # 返回公历日期的序数
print("年/周数/星期:", today.isocalendar())  # 返回一个元组:一年中的第几周,星期几

 result:

3. Conversion of time and timestamp in the date class:

(1) The Gregorian ordinal number returned by the toordinal method is converted into a date

import datetime

today = datetime.datetime.now()
# 此方法的返回类型是一个数字,它是该日期在公历中的序数。
num = today.toordinal()
print(num)
print(today.fromordinal(num))

result:

(2) time module timestamp conversion date

import datetime
import time

nowtime = time.time()
print(nowtime)
nowdate = datetime.date.fromtimestamp(nowtime)
print(nowdate)

result:

(3) Format the time, the format refers to the strftime method in the time module

import datetime

today = datetime.date.today()
print(today)
print(today.strftime("%Y.%m.%d"))
print(today.strftime("%Y:%m:%d"))
print(today.strftime("%Y.%m.%d %H:%M:%S"))

result:

4. Modify the date using the replace method 

import datetime

# 当前日期
date1 = datetime.date.today()
print(date1)

# 指定日期
date2 = datetime.date(2022, 10, 7)
print(date2)

# 不带参数修改日期
date3 = date2.replace(2022, 10, 8)
print(date3)

# 带参数修改日期
date4 = date2.replace(month=12, day=9)
print(date4)

 result:

time: time class

Common attributes: hour/minute/second/microsecond

The time class generates time objects, including hour, minute, second, microsecond attributes

 time class operations

import datetime

# time对象
print(datetime.time)
# 格式化time
time1 = datetime.time(18, 30, 59, 59)
print(time1)
print(time1.hour)
print(time1.minute)
print(time1.second)
print(time1.microsecond)  # 微秒

result:

datetime: date time class

The datetime class contains all the information of the date class and the time class

import datetime

print(datetime.datetime.today())
print(datetime.datetime.now())
print(datetime.datetime.utcnow())  # 返回当前UTC日期和时间的datetime对象
print(datetime.datetime.fromtimestamp(1670582201))  # 时间戳的datetime对象
print(datetime.datetime.fromordinal(738498))
print(datetime.datetime.strptime("2020-12-25", "%Y-%m-%d"))

result:

timedelta: time interval, that is, the length of time between two time points

The timedelta object represents a period of time, that is, the difference between two dates or datetimes; support parameters: weeks, days, hours, minutes, seconds, milliseconds, microseconds

timedelta operation

import datetime
day = datetime.date.today()
# 当前日期
print(day)
# 增加7天后日期
print(day+datetime.timedelta(days=7))

# 时间操作
now = datetime.datetime.now()
# 当前日期时间
print(now)
# 增加8小时
print(now+datetime.timedelta(hours=8))
# 增加30分钟
print(now+datetime.timedelta(minutes=30))
# 增加30秒钟
print(now+datetime.timedelta(seconds=30))
# 减去一星期
print(now-datetime.timedelta(weeks=1))

result:

This article introduces the use of the datetime module. Friends who don’t know how to use time or are unfamiliar with time operations can refer to the blog of the time module ( time , calendar , datetime) to realize the conversion with the other three time formats and the operation with the date. 

Guess you like

Origin blog.csdn.net/json_ligege/article/details/128284233