python日期时间格式化

日期时间格式化为字符串:datetime.datetime.strftime()
字符串转为datetime类型:datetime.datetime.strptime()

%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
%z, %Z Time-zone name or abbreviation; no characters if time zone is unknown
%% Percent sign

Python日期格式是通过使用time和datetime模块进行操作的。可以使用time模块中的strftime函数来将日期格式为字符串。具体来说,strftime函数接受一个格式字符串作为参数,并返回一个格式后的日期字符串。日期格式字符串中使用特定的占位符来表示不同的日期和时间部分。例如,"%Y"表示四位数的年份,"%m"表示两位数的月份,"%d"表示两位数的日期。通过组合不同的占位符,可以得到所需的日期格式。此外,Pythondatetime模块也提供了format函数,可以用于将日期格式为字符串。format函数接受一个格式字符串作为参数,并返回一个格式后的日期字符串。与strftime函数类似,格式字符串中的占位符用于表示不同的日期和时间部分。所以,通过使用time和datetime模块中的相应函数,可以很方便地对日期进行格式。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [你想要知道的Python日期格式知识都在这!](https://blog.csdn.net/weixin_34223655/article/details/91445316)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Python :格式日期](https://blog.csdn.net/sehun_sx/article/details/123553822)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值