前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python-时间及日期-02-时间转字符串

Python-时间及日期-02-时间转字符串

作者头像
zishendianxia
发布2019-10-23 17:39:07
1.6K0
发布2019-10-23 17:39:07
举报
文章被收录于专栏:Python工程师Python工程师

系统:Windows 7 语言版本:Anaconda3-4.3.0.1-Windows-x86_64 编辑器:pycharm-community-2016.3.2

  • 这个系列讲讲Python对时间及日期的操作
  • 今天讲讲如何将日期格式转化为字符串
  • 涉及模块: datetime

Part 1:代码

代码语言:javascript
复制
import datetime

# 转换成字符串
now_time = datetime.datetime.now()
print(now_time)
print(type(now_time))
print('\n')

str_time = now_time.strftime('%Y-%m-%d %H:%M:%S')
print(str_time)
print(type(str_time))
print('\n')

str_time = now_time.strftime('%y %m %d %I-%M-%S %p')
print(str_time)
print(type(str_time))
print('\n')

str_time = now_time.strftime('%B %d %A %j %w')
print(str_time)
print(type(str_time))
print('\n')

代码截图

运行结果

Part 2:部分代码解读

  1. now_time.strftimestrftime,可以理解为string formattime,即字符串格式的时间,因为后续还会讲一个函数strptime,不要混淆
  2. 格式化符号含义:
    • %Y,4位数表示的年,例如2019
    • %y,2位数表示的年,例如19
    • %m,2位数表示的月,01-12
    • %d,2位数表示的日,01-31
    • %H,2位数表示的时,00-23,24小时制
    • %I,2位数表示的时,01-12,12小时制
    • %M,2位数表示的分,00-59
    • %S,2位数表示的秒,00-59
    • %B,完整的月份表示
    • %A,完整表示的周次
    • %j,年内的第多少天,001-366
    • %w,周内的第几天,0-6,从周日开始
    • %p,表示AM或者PM
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-07-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python工程师 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Part 1:代码
  • Part 2:部分代码解读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档