【python-日期和时间处理】datetime模块基本使用

1. 获取datetime对象

  1. 获取当前datetime对象
  • 方法:datetime.now(cls, tz=None) -> datetime

  • 参数说明:

    tz:时区信息,不传该参数时,默认使用当地时区

  • 示例代码:

now = datetime.now()
print(now)  # 2023-01-05 09:38:43.084062
print(type(now))  # datetime.datetime

如果需要指定tz时区,可以通过pytz.timezone来指定,需要提供时区标识符,时区标识符可在"\Lib\site-packages\pytz\zoneinfo"文件夹下查看

  • 示例代码:
import pytz
from datetime import datetime

dt_mexico = datetime.now(pytz.timezone('America/Mexico_City'))
print(dt_mexico)  # 2023-01-04 20:18:46.943553-06:00
dt_shanghai = datetime.now(pytz.timezone('Asia/Shanghai'))  # 上海和北京同属于东八区,因此可以用“Asia/Shanghai”来作为东八区的标识符
print(dt_shanghai)  # 2023-01-05 10:18:46.943553+08:00
  1. 获取当前utc datetime对象
  • 方法:datetime.utcnow(cls) -> datetime

  • 示例代码:

from datetime import datetime

china_time = datetime.now()
utc_time = datetime.utcnow()

print(china_time)       # 2023-01-05 10:30:26.716563
print(utc_time)         # 2023-01-05 02:30:26.716563

北京属于东八区,比utc时间早8小时,即我们早上十点半的时候,正是utc时区中的凌晨两点半。

  1. 自定义datetime对象

2. datetime,str,timestamp相互转换

2.1 str 转 datetime 和 timestamp

  1. str转datetime
  • 方法:datetime.strptime(_date_string, _format) -> datetime

  • 参数说明:

    • _date_string:时间字符串
    • _format:时间解析格式
  • 示例代码:

from datetime import datetime

beijing_time_str = "2023-01-05 11:45:33"
datetime_beijing = datetime.strptime(beijing_time_str, "%Y-%m-%d %H:%M:%S")
print(type(datetime_beijing))  # <class 'datetime.datetime'>
print(datetime_beijing)  # 2023-01-05 11:45:33

  1. str转timestamp

。。。。。。

2.2 datetime 转 str 和 timestamp

  1. datetime对象 转 str
  • 方法:datetime.strftime(format) -> str
  • 示例代码:
from datetime import datetime

now = datetime.now()
now_str = now.strftime("%Y-%m-%d %H:%M:%S")
print(type(now_str))  # <class 'str'>
print(now_str)  # 2023-01-05 12:09:04
  1. datetime对象 转 timestamp
  • 方法:datetime.timestampe() -> float
  • 示例代码
from datetime import datetime

now = datetime.now()
timestamp = now.timestamp()
print(type(timestamp))  # <class 'float'>
print(timestamp)  # 1672900754.798144

2.3 timestamp 转 datetime对象 和 str

  1. timestamp 转 datetime
  • 方法
datetime.fromstamp(timestamp, tz=None) -> datetime
  • 示例代码
from datetime import datetime

timestamp = 1672899623
now = datetime.fromtimestamp(timestamp)
print(now)  # 2023-01-05 14:20:23
print(type(now))  # <class 'datetime.datetime'>
  1. timestamp 转 utc datetime对象
  • 方法
datetime.utcfromstamp(timestamp, tz=None) -> datetime
  • 示例代码
from datetime import datetime

timestamp = 1672899623
now = datetime.utcfromtimestamp(timestamp)
print(now)  # 2023-01-05 06:20:23
print(type(now))  # <class 'datetime.datetime'>

3. datetime实例方法

  1. datetime.timestampe() -> float
  • 示例代码
from datetime import datetime

now = datetime.now()
timestamp = now.timestamp()
print(type(timestamp))  # <class 'float'>
print(timestamp)  # 1672900754.798144
  1. datetime.timetuple() -> time.struct_time
  • 示例代码
from datetime import datetime

now = datetime.now()
timetuple = now.timetuple()
print(type(timetuple))  # <class 'time.struct_time'>
print(timetuple)  # time.struct_time(tm_year=2023, tm_mon=1, tm_mday=5, tm_hour=14, tm_min=44, tm_sec=3, tm_wday=3, tm_yday=5, tm_isdst=-1)

  1. datetime.replace(year=None, month=None, day=None, hour=None, minute=None, second=None, microsecond=None, tzinfo=True, *, fold=None) -> datetime
  • 示例代码
>>> from datetime import datetime
>>> now = datetime.now()
>>> now
datetime.datetime(2023, 1, 5, 14, 56, 10, 892657)
>>> now.replace(year=2022)
datetime.datetime(2022, 1, 5, 14, 56, 10, 892657)

4. datetime类方法

1.datetime.fromisocalendar(year, week, day) -> datetime

  • 功能说明

    从国际标准化日历中获得相应的datetime对象

  • 示例代码

>>> datetime.fromisocalendar(2023, 1, 1)
datetime.datetime(2023, 1, 2, 0, 0)  # 2023年中第1个星期的第1天是1月2号
  1. datetime.fromisoformat(date_string) -> datetime
  • 功能说明

    将符合isoformat时间格式的字符串转为datetime对象。

    isoformat指的是符合国际标准ISO8601的日期和时间表示方式。该标准旨在提供一套没有歧义的表示格式,并具有以下规则:

    • Year first, followed by month, then the day, each separated by a hyphen ("-")(按年月日的顺序先后排列,并以'-'进行分隔)
    • Numbers less than 10 preceded by a leading zero(小于10的数字前面加0)
  • 示例代码

>>> from datetime import datetime
>>> datetime.fromisoformat("2022-01-05")
datetime.datetime(2022, 1, 5, 0, 0)
>>> datetime.fromisoformat("2022-01-05 15:24:23")
datetime.datetime(2022, 1, 5, 15, 24, 23)
>>> datetime.fromisoformat("2022-01-05T15:24:23")
datetime.datetime(2022, 1, 5, 15, 24, 23)
>>> datetime.fromisoformat("2022-01-05T15:24:23.434")
datetime.datetime(2022, 1, 5, 15, 24, 23, 434000)
posted @ 2024-10-12 17:11  berlin-fly  阅读(11)  评论(0编辑  收藏  举报