How does python get time and format time and date?

Table of contents

1.time.time()

(1) Usage: used to get the current timestamp.

(2) Examples

2.time.localtime()

(1) Syntax: time.localtime([secs])

(2) Usage: Get the current time and date and convert to local time tuple.

(3) Examples

3.time.asctime()

(1) Syntax: time.asctime([tuple])

(2) Usage: Get the formatted time and convert the tuple time into a string time.

(3) Examples

4.time.strftime()

(1) Syntax: strftime(format[, tuple]) 

(2) Usage: used to get the time or date in the target time format.

(3) Examples


1.time.time()

(1) Usage: used to get the current timestamp.

Explanation: Timestamp refers to the total number of seconds from January 1, 1970 (00:00:00 GMT) to the current time in Greenwich Mean Time.

(2) Examples

import time #导入时间time模块
print(time.time())

#输出结果为:1668931308.366759

2.time.localtime()

(1) Syntax: time.localtime([secs])

Parameter Description:

secs: seconds, the number of seconds of the incoming time. Defaults to converting the current time when a null value is passed in. The return value is in struct_time format (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst). The timestamp returned by time.time() can be directly passed in.

(2) Usage: Get the current time and date and convert to local time tuple.

(3) Examples

import time  #导入time模块
print(time.localtime(time.time()))

#输出结果为:time.struct_time(tm_year=2022, tm_mon=11, tm_mday=20, tm_hour=16, tm_min=37, tm_sec=6, tm_wday=6, tm_yday=324, tm_isdst=0)

3.time.asctime()

(1) Syntax: time.asctime([tuple])

Parameter Description

tuple: optional, incoming time tuple or time.struct_time object representing time. When the value passed in is null, the current time returned by localtime() is used by default.

(2) Usage: Get the formatted time and convert the tuple time into a string time.

eg 'Sat Jun 06 16:26:11 1998'。

(3) Examples

import time  #导入time模块
print(time.asctime(time.localtime(time.time()))) #传入time.struct_time格式时间

#输出结果为:  Sun Nov 20 20:01:38 2022



import time  #导入time模块
print(time.asctime()) #传入空值,默认

#输出结果为:'Sun Nov 20 20:04:26 2022'

4.time.strftime()

(1) Syntax: strftime(format[, tuple]) 

Parameter Description:

format: Use English quotation marks to introduce the target time format, such as "'%Y-%m-%d %H:%M:%S'"

tuple: Incoming time tuple or time.struct_time object representing time. When the value passed in is null, the current time returned by localtime() is used by default.

The return value is a string.

(2) Usage: used to get the time or date in the target time format.

(3) Examples

import time  #导入time模块
date1 = time.strftime('%Y-%m-%d %H:%M:%S')  #默认传入当前时间
print('当前时间date1为',date1)

date2 = time.strftime('%Y-%m-%d',time.localtime())  #传入time.localtime本地目前时间
print('当前日期date2为',date2)

date3 = time.strftime('%Y%m%d')
print('当前日期date3为',date3)

time1 = time.strftime('%H:%M:%S')
print('当前时间time1为',time1)

The output is:

当前时间date1为 2022-11-20 20:31:53
当前日期date2为 2022-11-20
当前日期date3为 20221120
当前时间time1为 20:31:53

Reference article:

Python Date and Time | Rookie Tutorial (runoob.com)

Guess you like

Origin blog.csdn.net/weixin_50853979/article/details/127941237