Python处理日期和时间的基础教程
解析日期字符串
示例代码:
# 01 解析日期字符串
from ztime import dateutil
date_string = '2022-01-01'
date = dateutil.parser.parse(date_string)
print('Parsed Date:', date)
date_string = '2022-01-01 12:13:14'
date = dateutil.parser.parse(date_string)
print('Parsed Date:', date)
date_string = '2022-01-02T12:13:14'
date = dateutil.parser.parse(date_string)
print('Parsed Date:', date)
输出结果:
Parsed Date: 2022-01-01 00:00:00
Parsed Date: 2022-01-01 12:13:14
Parsed Date: 2022-01-02 12:13:14
格式化日期
示例代码:
# 02 格式化日期
from ztime import dateutil
date_string = '2022-01-01 11:12:13'
date = dateutil.parser.parse(date_string)
print('Parsed Date:', date)
formatted_date = date.strftime('%Y-%m-%d %H:%M:%S')
print('Formatted Date:', formatted_date)
输出结果:
Parsed Date: 2022-01-01 11:12:13
Formatted Date: 2022-01-01 11:12:13
日期的加减
示例代码:
# 03 日期的加减
import datetime
from ztime import dateutil
date_string = '2022-01-01 11:12:13'
date = dateutil.parser.parse(date_string)
print('Parsed Date:', date)
# 加上10天
new_date = date + datetime.timedelta(days=10)
formatted_date = new_date.strftime('%Y-%m-%d %H:%M:%S')
print('Formatted Date:', formatted_date)
# 减去10天
new_date = date + datetime.timedelta(days=-10)
formatted_date = new_date.strftime('%Y-%m-%d %H:%M:%S')
print('Formatted Date:', formatted_date)
输出结果:
Parsed Date: 2022-01-01 11:12:13
Formatted Date: 2022-01-11 11:12:13
Formatted Date: 2021-12-22 11:12:13
日期的替换
示例代码:
# 04 日期的替换
from ztime import dateutil
date_string = '2022-01-01 11:12:13'
date = dateutil.parser.parse(date_string)
print('Parsed Date:', date)
# 替换年份为2023
# 其他参数:year,month,day,hour,minute,second,microsecond,tzinfo
new_date = date.replace(year=2023)
formatted_date = new_date.strftime('%Y-%m-%d %H:%M:%S')
print('Formatted Date:', formatted_date)
new_date = date.replace(hour=23)
formatted_date = new_date.strftime('%Y-%m-%d %H:%M:%S')
print('Formatted Date:', formatted_date)
输出结果:
Parsed Date: 2022-01-01 11:12:13
Formatted Date: 2023-01-01 11:12:13
Formatted Date: 2022-01-01 23:12:13
获取当前时间
示例代码:
# 01 获取当前时间
import ztime
# 获取本地时区的当前时间
print(ztime.now())
# 获取UTC时间,然后转为本地时区的时间
utc = ztime.utcnow()
utc = utc.to("local")
print(utc)
输出结果:
2024-02-15T18:48:42.096882+08:00
日期格式化字符串
示例代码:
# 03 日期格式化字符串
import ztime
utc = ztime.utcnow()
utc = utc.to("local")
print(utc)
print(utc.format())
print(utc.format("YYYY-MM-DD HH:mm:ss"))
输出结果:
2024-02-15T18:48:50.218306+08:00
2024-02-15 18:48:50
常用的属性
示例代码:
# 07 常用的属性
import ztime
t = ztime.utcnow()
print(t.naive)
print(t.tzinfo)
print(t.year)
print(t.month)
print(t.day)
print(t.hour)
输出结果:
2024-02-15 10:49:02.023246
tzutc()
2024
2
15
10
日期加减
示例代码:
# 05 日期加减
import ztime
utc = ztime.utcnow()
utc = utc.to("local")
utc = utc.shift(hours=-1)
print(utc.humanize(locale="zh-cn"))
utc = utc.shift(days=-1)
print(utc.humanize(locale="zh-cn"))
utc = utc.shift(months=-1)
print(utc.humanize(locale="zh-cn"))
utc = utc.shift(years=-1)
print(utc.humanize(locale="zh-cn"))
utc = utc.shift(years=2)
print(utc.humanize(locale="zh-cn"))
输出结果:
1小时前
1天前
1个月前
1年前
11个月后
获取日期对象的方式
示例代码:
# 06 获取日期对象的方式
from datetime import datetime
import ztime
t = ztime.get(1367900664)
t = ztime.get(1367900664.152325)
t = ztime.get(datetime.utcnow())
t = ztime.get(datetime(2013, 5, 5), "local")
t = ztime.get('2013-05-05 12:30:45', 'YYYY-MM-DD HH:mm:ss')
t = ztime.get(2013, 5, 5)
print(t)
输出结果:
2013-05-05T00:00:00+00:00
人性化时间
示例代码:
# 04 人性化时间
import ztime
utc = ztime.utcnow()
utc = utc.to("local")
print(utc)
print(utc.humanize())
print(utc.humanize(locale="zh-cn"))
输出结果:
2024-02-15T18:49:35.488135+08:00
刚才
刚才
获取日期和时间
示例代码:
# 08 获取日期和时间
import ztime
t = ztime.utcnow()
print(t.date()) # 获取日期
print(t.time()) # 获取时间
输出结果:
2024-02-15
10:49:52.020618
替换指定时间
示例代码:
# 09 替换指定时间
import ztime
t = ztime.utcnow()
print(t.replace(hour=4, minute=40)) # 替换指定时间
输出结果:
2024-02-15T04:40:02.819664+00:00
解析人性化时间
示例代码:
# 10 解析人性化时间
import ztime
t = ztime.utcnow()
print(t.dehumanize("2 days ago")) # 解析人性化时间
print(t.dehumanize("2天前", locale="zh-cn")) # 解析人性化时间
输出结果:
2024-02-13T10:50:12.944692+00:00
2024-02-13T10:50:12.944692+00:00
小时处理
示例代码:
# 11 小时处理
import ztime
t = ztime.utcnow()
print(t.span('hour')) # 获取到小时
print(t.floor('hour')) # 移除小时后的时间
print(t.ceil('hour')) # 最大化小时后的时间
输出结果:
(<Arrow [2024-02-15T10:00:00+00:00]>, <Arrow [2024-02-15T10:59:59.999999+00:00]>)
2024-02-15T10:00:00+00:00
2024-02-15T10:59:59.999999+00:00
获取指定日期之间的每个小时
示例代码:
# 12 获取指定日期之间的每个小时
import ztime
from datetime import datetime
start = datetime(2013, 5, 5, 12, 30)
end = datetime(2013, 5, 5, 17, 15)
for r in ztime.ZTime.span_range('hour', start, end):
print(r)
输出结果:
(<Arrow [2013-05-05T12:00:00+00:00]>, <Arrow [2013-05-05T12:59:59.999999+00:00]>)
(<Arrow [2013-05-05T13:00:00+00:00]>, <Arrow [2013-05-05T13:59:59.999999+00:00]>)
(<Arrow [2013-05-05T14:00:00+00:00]>, <Arrow [2013-05-05T14:59:59.999999+00:00]>)
(<Arrow [2013-05-05T15:00:00+00:00]>, <Arrow [2013-05-05T15:59:59.999999+00:00]>)
(<Arrow [2013-05-05T16:00:00+00:00]>, <Arrow [2013-05-05T16:59:59.999999+00:00]>)
(<Arrow [2013-05-05T17:00:00+00:00]>, <Arrow [2013-05-05T17:59:59.999999+00:00]>)
获取时间戳
示例代码:
# 02 获取时间戳
import ztime
utc = ztime.utcnow()
utc = utc.to("local")
print(utc)
print(utc.timestamp())
输出结果:
2024-02-15T18:50:47.550633+08:00
1707994247.550633
THE END