python datetime 时间处理
  TEZNKK3IfmPf 2023年11月14日 25 0

python 模块 datetime时间处理

 

文章目录

  • python 模块 datetime时间处理
    • 1. 定义的类
      • 1.1 date类
    • 2. 扩展

 


1. 定义的类

datetime.date    --表示日期的类。常用的属性有year, month, day
datetime.time    --表示时间的类。常用的属性有hour, minute, second, microsecond
datetime.datetime  --表示日期时间
datetime.timedelta   --表示时间间隔,即两个时间点之间的长度

1.1 date类

date类表示日期,构造函数如下 :

datetime.date(year, month, day);
year (1-9999)
month (1-12)
day (1-31)
date.today()  --返回一个表示当前本地日期的date对象
date.fromtimestamp(timestamp) --根据给定的时间戮,返回一个date对象
#根据给定的时间戮,返回一个date对象
> print(datetime.date.fromtimestamp(time.time()))
2020-10-10
#返回一个表示当前本地日期的date对象
> print(datetime.datetime.today())
2020-10-10 15:31:14.152035
date.year()   --取给定时间的年
date.month()  --取时间对象的月
date.day()  --取给定时间的日
date.replace()  --生成一个新的日期对象,用参数指定的年,月,日代替原有对象中的属性
date.timetuple()  --返回日期对应的time.struct_time对象
date.weekday()  --返回weekday,Monday == 0 ... Sunday == 6
date.isoweekday() --返回weekday,Monday == 1 ... Sunday == 7
date.ctime()    --返回给定时间的字符串格式
#取时间对象的年
print(datetime.date.today().year)
#取时间对象的月
print(datetime.date.today().month)
#取时间对象的日
print(datetime.date.today().day)
#生成一个新的日期对象,用参数指定的年,月,日代替原有对象中的属性
print(datetime.date.today().replace(2010,6,12))
#返回给定时间的时间元组/对象
print(datetime.date.today().timetuple())
#返回weekday,从0开始
print(datetime.date.today().weekday())
#返回weekday,从1开始
print(datetime.date.today().isoweekday())
#返回给定时间的字符串格式
print(datetime.date.today().ctime())

2. 扩展

import datetime
mtime = path.stat().st_mtime
timestamp_str = datetime.datetime.fromtimestamp(mtime).strftime('%Y-%m-%d-%H:%M')

示例:添加链接描述

#!/usr/bin/python

from datetime import datetime, timezone
from pathlib import Path

path = Path('foo')
path.touch()
stat_result = path.stat()
modified = datetime.fromtimestamp(stat_result.st_mtime, tz=timezone.utc)

输出:

$ python3 test.py 
modified 2021-01-17 09:04:37.146791+00:00

参考:

  • geeksforgeeks Python datetime module
  • datetime — Basic date and time types
  • w3schools Python Datetime
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月31日   38   0   0 python开发语言
  TEZNKK3IfmPf   2024年05月31日   28   0   0 python
  TEZNKK3IfmPf   2024年05月31日   35   0   0 excelpython
  TEZNKK3IfmPf   2024年05月31日   30   0   0 python
TEZNKK3IfmPf