Python日期格式化

简介:

Python日期格式化常用的一些格式化占位符



%a Abbreviated weekday name
%A Full weekday name
%b Abbreviated month name
%B Full month name
%c Date and time representation appropriate for locale
%d Day of month as decimal number (01 - 31)
%H Hour in 24-hour format (00 - 23)
%I Hour in 12-hour format (01 - 12)
%j Day of year as decimal number (001 - 366)
%m Month as decimal number (01 - 12)
%M Minute as decimal number (00 - 59)
%p Current locale's A.M./P.M. indicator for 12-hour clock
%S Second as decimal number (00 - 59)
%U Week of year as decimal number, with Sunday as first day of week (00 - 51)
%w Weekday as decimal number (0 - 6; Sunday is 0)
%W Week of year as decimal number, with Monday as first day of week (00 - 51)
%x Date representation for current locale
%X Time representation for current locale
%y Year without century, as decimal number (00 - 99)
%Y Year with century, as decimal number
%% Percent sign
%f Millisecond


注:Python不支持%z, %Z等时区的格式占位符


字符串->日期格式


from datetime import datetime

tString = '1990-7-18-23-11-20'
format = '%Y-%m-%d-%H-%M-%S'
t = datetime.strptime(tString, format)

日期格式->字符串


from datetime import datetime

t = datetime.now()
format = '%Y-%m-%d-%H-%M-%S'
tString = t.strftime(format)





目录
相关文章
|
14天前
|
Python
「Python系列」Python 日期和时间
Python 提供了多个内置模块来处理日期和时间,其中最常用的是 `datetime` 模块。这个模块提供了类来操作日期、时间、日期和时间间隔。
26 0
|
1月前
|
Unix 数据处理 Python
Python中日期时间的处理
Python中日期时间的处理
24 0
|
3月前
|
编译器 Python
Python关于日期的记录
Python关于日期的记录
18 0
|
3月前
|
Python
python获取当前日期
python获取当前日期
31 1
|
6月前
|
存储 Linux C语言
Python标准库分享之时间与日期 (time, datetime包)
Python标准库分享之时间与日期 (time, datetime包)
|
1月前
|
安全 Python
Python如何使用datetime模块进行日期和时间的操作
Python如何使用datetime模块进行日期和时间的操作
26 1
|
1月前
|
开发者 Python
Python生成日期和时间
Python生成日期和时间
16 0
|
2月前
|
存储 Python
用Python提取长时间序列遥感文件中缺失文件所对应的日期
【2月更文挑战第1天】本文介绍批量下载大量多时相的遥感影像文件后,基于Python语言与每一景遥感影像文件的文件名,对这些已下载的影像文件加以缺失情况的核对,并自动统计、列出未下载影像所对应的时相的方法~
用Python提取长时间序列遥感文件中缺失文件所对应的日期
|
2月前
|
BI Python
Python获取上个月最后一天的日期
Python获取上个月最后一天的日期
36 0
Python获取上个月最后一天的日期
|
3月前
|
Python
Python 时间日期处理库函数
Python 时间日期处理库函数
55 0
Python 时间日期处理库函数