python之time和datetime模块 python time和datetime
# -*- coding: UTF-8 -*-
import time
import datetime
#打印时间戳:1650041030.05
print(time.time())
#格式化字符串时间:Sat Apr 16 00:44:44 2022
print(time.ctime())
#time.struct_time(tm_year=2022, tm_mon=4, tm_mday=16, tm_hour=0, tm_min=46, tm_sec=40, tm_wday=5, tm_yday=106, tm_isdst=0)
print(time.localtime())
#Sat Apr 16 00:46:40 2022
print(time.asctime(time.localtime()))
#元组时间转换为时间戳: 1650041341.0
print(time.mktime(time.localtime()))
#元组时间转换为字符串时间: 16-04
print(time.strftime("%d-%m", time.localtime()))
#年月日:2022/04/16
print(time.strftime("%Y/%m/%d", time.localtime()))
#将时间戳转换为字符串时间: Sat Apr 16 00:55:42 2022
print(time.ctime(time.time()))
#将时间戳转换为时间元组:time.struct_time(tm_year=2022, tm_mon=4, tm_mday=16, tm_hour=0, tm_min=57, tm_sec=22, tm_wday=5, tm_yday=106, tm_isdst=0)
print(time.localtime(time.time()))
#datetime模块
#date对象所能表示的最大、最小日期 9999-12-31
print(datetime.date.max)
#date对象所能表示的最大、最小日期 0001-01-01
print(datetime.date.min)
#返回一个表示当前本地日期的date对象:2022-04-16
print(datetime.date.today())
#将时间戳转换为字符串时间:2022-04-16
print(datetime.date.fromtimestamp(time.time()))
#time模块
序号 | 函数及描述 |
1 | time.altzone |
2 | time.asctime([tupletime]) |
3 | time.clock( ) |
4 | time.ctime([secs]) |
5 | time.gmtime([secs]) |
6 | time.localtime([secs]) |
7 | time.mktime(tupletime) |
8 | time.sleep(secs) |
9 | time.strftime(fmt[,tupletime]) |
10 | time.strptime(str,fmt='%a %b %d %H:%M:%S %Y') |
11 | time.time( ) |
12 | time.tzset() |