python3 datetime和time获取当前日期和时间
2017-01-01 23:54
671 查看
最经使用到了一些时间相关的包和函数,以后使用到更多,再补充
import datetime import time # 获取当前时间, 其中中包含了year, month, hour, 需要import datetime today = datetime.date.today() print(today) print(today.year) print(today.month) print(today.day) ''' >>>2017-01-01 >>>2017 >>>1 >>>1 ''' # 获得明天, 其他依次类推 tomorrow = today + datetime.timedelta(days=1) print(tomorrow) ''' >>>2017-01-02 ''' # 时间相减,相加同理 now = datetime.timedelta(days=0, hours=0, minutes=3, seconds=50); pre = datetime.timedelta(days=0, hours=0, minutes=1, seconds=10); duration_sec = (now - pre).seconds duration_day = (now - pre).days print(type(duration_sec)) print(type(now - pre)) print(duration_sec) print(duration_day) ''' >>><class 'int'> >>><class 'datetime.timedelta'> >>>160 >>>0 ''' # 使用time.strftime(format, p_tuple)获取当前时间,需要import time now = time.strftime("%H:%M:%S") print(now) ''' >>>23:49:34 ''' # 使用datetime.now() now = datetime.datetime.now() print(now) print(now.year) print(now.month) print(now.day) print(now.hour) print(now.minute) print(now.second) print(now.microsecond) ''' >>>2017-01-01 23:49:34.789292 >>>2017 >>>1 >>>1 >>>23 >>>49 >>>34 >>>789292 '''文档地址:datetime — Basic date and time types
相关文章推荐
- python 利用time模块获取当前 日期时间
- Python标准库02 时间与日期 (time, datetime包)
- python 获取当前时间 日期 时间戳格式化 参数
- python获取当前日期和时间的方法
- C# 根据当前时间获取,本周,本月,本季,DateTime 日期加1天 减一天 加一月 减一月 等方法
- Python获取并输出当前日期时间
- VC++获取当前系统时间日期 GetSystemTime()函数的用法
- Python标准库02 时间与日期 (time, datetime包)
- Python标准库02 时间与日期 (time, datetime包)
- VC++获取当前系统时间日期 GetSystemTime()函数的用法
- python--标准库 时间与日期 (time, datetime包)
- Python标准库02 时间与日期 (time, datetime包)
- Python标准库02 时间与日期 (time, datetime包)
- time,datetime------python操作日期和时间的方法
- python获取当前日期和时间的方法
- VC++获取当前系统时间日期 GetSystemTime()函数的用法
- Python标准库02 时间与日期 (time, datetime包)
- Python学习笔记9:标准库之日期时间(time包,datetime包)
- Python:如何获取当前的日期和时间
- VC++获取当前系统时间日期 GetSystemTime()函数的用法