程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Python日期和时间随记

发布于2020-03-18 10:59     阅读(1508)     评论(0)     点赞(12)     收藏(1)


时间格式:

python中时间日期格式化符号:

  • %y 两位数的年份表示(00-99)
  • %Y 四位数的年份表示(000-9999)
  • %m 月份(01-12)
  • %d 月内中的一天(0-31)
  • %H 24小时制小时数(0-23)
  • %I 12小时制小时数(01-12)
  • %M 分钟数(00=59)
  • %S 秒(00-59)
  • %a 本地简化星期名称
  • %A 本地完整星期名称
  • %b 本地简化的月份名称
  • %B 本地完整的月份名称
  • %c 本地相应的日期表示和时间表示
  • %j 年内的一天(001-366)
  • %p 本地A.M.或P.M.的等价符
  • %U 一年中的星期数(00-53)星期天为星期的开始
  • %w 星期(0-6),星期天为星期的开始
  • %W 一年中的星期数(00-53)星期一为星期的开始
  • %x 本地相应的日期表示
  • %X 本地相应的时间表示
  • %Z 当前时区的名称
  • %% %号本身

时间time:

  • 本地时间获取,及对时间进行格式化:
  1. import time
  2. times = time.time()
  3. print("现在时间戳: %0.2f" %times)
  4. localtimes = time.localtime(times)
  5. print("本地时间是:",localtimes)
  6. # 将时间戳格式化
  7. asctime1=time.asctime(localtimes)
  8. print("格式化时间1:",asctime1)
  9. print(time.ctime())
  10. formatTime=time.strftime('%Y-%m-%d %H-%M-%S',localtimes)
  11. print("格式化时间2:",formatTime)
  12. # 将格式字符串转换为时间戳
  13. print(time.mktime(time.strptime(asctime1,"%a %b %d %H:%M:%S %Y")))
  1. 现在时间戳: 1584411264.85
  2. 本地时间是: time.struct_time(tm_year=2020, tm_mon=3, tm_mday=17, tm_hour=10, tm_min=14, tm_sec=24, tm_wday=1, tm_yday=77, tm_isdst=0)
  3. 格式化时间1: Tue Mar 17 10:14:24 2020
  4. Tue Mar 17 10:14:24 2020
  5. 格式化时间22020-03-17 10-14-24

日期datetime:

  • 获取今天、昨天、明天,时间差1天,即可根据间隔天数获取日期:
  1. import datetime
  2. today=datetime.date.today()
  3. oneday=datetime.timedelta(days=1)
  4. yesterday=today-oneday
  5. tomorrow=today+oneday
  6. print(today)
  7. print(oneday)
  8. print(yesterday)
  9. print(tomorrow)
  1. 2020-03-17
  2. 1 day, 0:00:00
  3. 2020-03-16
  4. 2020-03-18

 

日历calendar

  • 获取某月日历
  1. import calendar
  2. cal = calendar.month(2020, 3)
  3. print("以下输出2020年3月份的日历:")
  4. print(cal)
  1. 以下输出2020年3月份的日历:
  2. March 2020
  3. Mo Tu We Th Fr Sa Su
  4. 1
  5. 2 3 4 5 6 7 8
  6. 9 10 11 12 13 14 15
  7. 16 17 18 19 20 21 22
  8. 23 24 25 26 27 28 29
  9. 30 31
  • 判断是否为闰年calendar.isleap(year)
  1. import calendar
  2. cal = calendar.isleap(2020)
  3. print("判断2020是否是闰年:",cal)
判断2020是否是闰年: True

 

原文链接:https://blog.csdn.net/jt_121217/article/details/104915835



所属网站分类: 技术文章 > 博客

作者:dfd323

链接:https://www.pythonheidong.com/blog/article/265218/c384e913fa3c22f5c7ce/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

12 0
收藏该文
已收藏

评论内容:(最多支持255个字符)