Home > Backend Development > Python Tutorial > How to obtain and convert time in Python time module

How to obtain and convert time in Python time module

WBOY
Release: 2023-05-13 12:19:06
forward
1914 people have browsed it

Python time module time acquisition and conversion

Python's Time library can perform time-related processing, such as accessing the current date and time, outputting time in different formats, and waiting for a specified time.

1. Get time

1.1. Timestamp

import time
timestamp = time.time()
# 1682737552.5009851
Copy after login

Starting from 00:00:00 on January 1, 1970, Greenwich Mean Time (GMT) The total number of seconds to date

1.2. Structured time

import time
struct_time = time.localtime()
#time.struct_time(tm_year=2023, tm_mon=4, tm_mday=29, tm_hour=11, tm_min=6, tm_sec=43, tm_wday=6, tm_yday=120, tm_isdst=0)
Copy after login

1.3. Formatted time

import time
format_time = time.strftime('%Y-%m-%d %H:%M:%S %p')
# 2023-04-29 11:07:30 AM
Copy after login

%Y Year with century as a decimal number.(year)
%m Month as a decimal number [01,12].(month)
%d Day of the month as a decimal number [01,31].(日)
% H Hour (24-hour clock) as a decimal number [00,23].(hour-24 hour)
%M Minute as a decimal number [00,59].(minute)
%S Second as a decimal number [00,61].(seconds)
%z Time zone offset from UTC.(The time difference between international standard time and local time (East Eighth District, Beijing time))
%a Locale’s abbreviated weekday name.(Day of the week-abbreviation)
%A Locale’s full weekday name.(Day of the week-full name)
%b Locale’s abbreviated month name.(Month name-abbreviation)
%B Locale&rsquo ;s full month name.(month name-full name)
%c Locale’s appropriate date and time representation.(linux time format)
%I Hour (12-hour clock) as a decimal number [01 ,12].(hour-12 o'clock)
%p Locale’s equivalent of either AM or PM.(morning or afternoon)
%X hour:minute:second, and %H:%M:%S The effect is the same
%x hours/minutes/seconds

2. Time conversion

2.1. Timestamp< > Structured time

import time
timestamp = time.time()
# 1682738174.6577663
# 时间戳 > 结构化时间
struct_time = time.localtime(timestamp)  # 默认time.time()
# time.struct_time(tm_year=2023, tm_mon=4, tm_mday=29, tm_hour=11, tm_min=16, tm_sec=14, tm_wday=6, tm_yday=120, tm_isdst=0)
# 结构化时间 > 时间戳
timestamp = time.mktime(struct_time)
# 1682738174.0
Copy after login

2.2 . structured time< > formatted time

import time
struct_time = time.localtime()
#time.struct_time(tm_year=2023, tm_mon=4, tm_mday=29, tm_hour=11, tm_min=21, tm_sec=43, tm_wday=6, tm_yday=120, tm_isdst=0)
# 结构化时间 > 格式化时间
format_time = time.strftime(&#39;%Y-%m-%d %H:%M:%S&#39;, struct_time)
#2023-04-29 11:21:43
# 格式化时间 > 结构化时间
struct_time = time.strptime(format_time, &#39;%Y-%m-%d %H:%M:%S&#39;)
#time.struct_time(tm_year=2023, tm_mon=4, tm_mday=29, tm_hour=11, tm_min=21, tm_sec=43, tm_wday=6, tm_yday=120, tm_isdst=-1)
Copy after login

The above is the detailed content of How to obtain and convert time in Python time module. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template