python 格式化日期

Time模块提供各种与时间相关的功能,常用的方法如下:


time.time():以秒为单位返回从纪元开始的时间作为浮点数。


time.ctime([secs]):将从epoch开始以秒表示的时间转换为表示本地时间的字符串


time.gmtime[secs]):以秒表示的时间转换为UTC格式的结构时间,其中DST标志始终为零。如果未提供秒或没有秒,则使用time()返回的当前时间。


time.localtime([secs]):类似于gmtime(),但转换为本地时间。如果未提供秒或没有秒,则使用time()返回的当前时间。当DST应用于给定时间时,DST标志设置为1


time.sleep(secs):在给定的秒数内暂停当前线程的执行。


time.strftime(format[, t]):将gmtime()或localtime()返回的时间的元组或结构时间转换为format参数指定的字符串。格式参数如下:

Directive

Meaning

Notes

%a

Locale’s abbreviated weekday name.


%A

Locale’s full weekday name.


%b

Locale’s abbreviated month name.


%B

Locale’s full month name.


%c

Locale’s appropriate date and time representation.


%d

Day of the month as a decimal number [01,31].


%H

Hour (24-hour clock) as a decimal number [00,23].


%I

Hour (12-hour clock) as a decimal number [01,12].


%j

Day of the year as a decimal number [001,366].


%m

Month as a decimal number [01,12].


%M

Minute as a decimal number [00,59].


%p

Locale’s equivalent of either AM or PM.

(1)

%S

Second as a decimal number [00,61].

(2)

%U

Week number of the year (Sunday as the first day of the   week) as a decimal number [00,53]. All days in a new year preceding the first   Sunday are considered to be in week 0.

(3)

%w

Weekday as a decimal number [0(Sunday),6].


%W

Week number of the year (Monday as the first day of the   week) as a decimal number [00,53]. All days in a new year preceding the first   Monday are considered to be in week 0.

(3)

%x

Locale’s appropriate date representation.


%X

Locale’s appropriate time representation.


%y

Year without century as a decimal number [00,99].


%Y

Year with century as a decimal number.


%Z

Time zone name (no characters if no   time zone exists).


%%

A literal '%' character.


# -*- coding: utf-8 -*-
import time

print time.time()
print time.ctime()
print time.gmtime()
print time.localtime()
time.sleep(
3)
print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
print time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
print time.strftime('%Y-%m-%d', time.localtime(time.time()))


 

输出:
1571390853.04
Fri Oct 18 17:27:33 2019
time.struct_time(tm_year=2019, tm_mon=10, tm_mday=18, tm_hour=9, tm_min=27, tm_sec=33, tm_wday=4, tm_yday=291, tm_isdst=0)
time.struct_time(tm_year=2019, tm_mon=10, tm_mday=18, tm_hour=17, tm_min=27, tm_sec=33, tm_wday=4, tm_yday=291, tm_isdst=0)
2019-10-18 17:27:36
2019-10-18 17:27:36

2019-10-18


该文章对你有帮助吗,求分享转发: 分享到QQ空间 分享给QQ好友

 

 

粤ICP备19116230号
友情链接: 码农藏书阁 天天链