python datetime strftime_datetime.strftime时间输出转换

datetime.strftime时间输出转换:

注:

1、%Y-%m-%d %H:%M:%S 年-月-日 时:分:秒

2、%A 星期的表示方式,%B 月的英文全写

3、%w周里的第几天,%d月里的第几天,%j年里的第几天,%U年里的第几周

vi a1.py

#!/usr/bin/python

#coding=utf-8

from datetime import datetime

d = datetime.now()

print d.strftime("%Y-%m-%d %H:%M:%S")

print u"今天是{0}".format(d.strftime("%A"))

print u"这月是{0}".format(d.strftime("%B"))

print u"今天是这周的第{0}天".format(d.strftime("%w"))

print u"今天是这月的第{0}天".format(d.strftime("%d"))

print u"今天是这年的第{0}天".format(d.strftime("%j"))

print u"今天是这年的第{0}周".format(d.strftime("%U"))

:wq

python a1.py

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,datetime.strftime()函数用于将日期时间对象转换为指定格式的字符串。通过指定格式化字符串,我们可以以不同的方式显示日期和时间的各个部分。引用中的示例展示了如何使用strftime()函数来格式化年份,示例代码如下: ```python from datetime import datetime def main(): now = datetime.now() formatted_date = now.strftime("%Y") print(formatted_date) main() ``` 这段代码将当前日期时间对象的年份转换为字符串并打印出来。在strftime()函数中,"%Y"代表年份的格式化指令。 除了年份,我们还可以使用其他格式化指令来格式化日期和时间的其他部分。例如,在引用中的示例中,我们可以使用"%I:%M:%S %p"来格式化时间为12小时制,并带有上午/下午的标识,使用"%H:%M"来格式化时间为24小时制,示例代码如下: ```python from datetime import datetime def main(): now = datetime.now() formatted_time_with_ampm = now.strftime("%I:%M:%S %p") formatted_time_24hr = now.strftime("%H:%M") print(formatted_time_with_ampm) print(formatted_time_24hr) main() ``` 这段代码将当前日期时间对象的时间部分转换为12小时制的字符串和24小时制的字符串并打印出来。"%I:%M:%S %p"将时间格式化为12小时制,并带有上午/下午的标识,"%H:%M"将时间格式化为24小时制。 综上所述,datetime.strftime()函数可以将日期时间对象格式化为指定格式的字符串,并且我们可以使用不同的格式化指令来显示日期和时间的各个部分。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Python学习之DateTime、TimeDelta、Strftime(Format)及其示例](https://blog.csdn.net/u014740628/article/details/130267785)[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_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值