import datetime # 能调整的字段:weeks days hours minutes seconds print(datetime.datetime.now() + datetime.timedelta(weeks=3)) # 三周后 print(datetime.datetime.now() + datetime.timedelta(weeks=-3)) # 三周前 print(datetime.datetime.now() + datetime.timedelta(days=-3)) # 三天前 print(datetime.datetime.now() + datetime.timedelta(days=3)) # 三天后 print(datetime.datetime.now() + datetime.timedelta(hours=5)) # 5小时后 print(datetime.datetime.now() + datetime.timedelta(hours=-5)) # 5小时前 print(datetime.datetime.now() + datetime.timedelta(minutes=-15)) # 15分钟前 print(datetime.datetime.now() + datetime.timedelta(minutes=15)) # 15分钟后 print(datetime.datetime.now() + datetime.timedelta(seconds=-70)) # 70秒前 print(datetime.datetime.now() + datetime.timedelta(seconds=70)) # 70秒后 current_time = datetime.datetime.now() # 现在的时间 print(current_time.replace(year=1977)) # 直接调整到1977年 print(current_time.replace(month=1)) # 直接调整到1月份 print(current_time.replace(year=1989,month=4,day=25)) # 将时间戳转化成时间 print(datetime.date.fromtimestamp(1232132131)) # 2009-01-17 # 输出结果 ''' 2019-10-14 21:43:51.831900 2019-09-02 21:43:51.831900 2019-09-20 21:43:51.831900 2019-09-26 21:43:51.831900 2019-09-24 02:43:51.831900 2019-09-23 16:43:51.831900 2019-09-23 21:28:51.831900 2019-09-23 21:58:51.831900 2019-09-23 21:42:41.831900 2019-09-23 21:45:01.831900 1977-09-23 21:43:51.831900 2019-01-23 21:43:51.831900 1989-04-25 21:43:51.831900 2009-01-17 '''
datetime时区转换
Python标准库中的datetime模块提供了各种对日期和时间的处理方法。从本文的主题着手,首先利用datetime中提供的utcnow()方法获取到当前UTC时间:
>>> import datetime >>> utc_now = datetime.datetime.utcnow() >>> utc_now datetime.datetime(2013, 12, 4, 15, 43, 21, 872000)
而此时其tzinfo为none:
>>> utc_now.tzinfo
>>>
当涉及国际时区时,时区转换则会经常使用到,比如来尝试将刚才的UTC时间转换为本地时间。对于Python3.3+版本,可以这么做:
>>> utc_now.replace(tzinfo=datetime.timezone.utc).astimezone(tz=None) datetime.datetime(2013, 12, 4, 23, 43, 21, 872000, tzinfo=datetime.timezone(datetime.timedelta(0, 28800), '中国标准时间'))
不过该方法貌似并不方便,特别是在转换其他时区的时候。而对于更低版本的Python,则datetime.timezone可能压根就还没有。因此更便捷的方法是借助第三方包来实现 —— pyzt,下面就借助于它来实现时区转换:
>>> from pytz import timezone >>> utc_now.tzinfo >>> tzchina = timezone('Asia/Chongqing') >>> tzchina'Asia/Chongqing' LMT+7:06:00 STD> >>> utc = timezone('UTC') >>> utc_now.replace(tzinfo=utc).astimezone(tzchina) datetime.datetime(2013, 12, 4, 23, 43, 21, 872000, tzinfo= 'Asia/Chongqing' CST+8:00:00 STD>)
要转换为其他时区,则以此类推。
对于我自己来说,时区的转换主要出现在Django中,会经常需要将UTC时间转换为本地时间,而Django本身也已经为我们考虑到了这一点,因此实际操作起来更为方便:
>>> from django.utils.timezone import utc >>> from django.utils.timezone import localtime >>> now = datetime.datetime.utcnow().replace(tzinfo=utc) >>> localtime(now) datetime.datetime(2013, 12, 5, 0, 3, 13, 122000, tzinfo='Asia/Shanghai' CST+8:00:00 STD>)
在Python中转换时区的方法还有很多,通过探索也许还能找到更好的方法。
from pytz import timezone def datetime_as_timezone(date_time, time_zone): tz = timezone(time_zone) utc = timezone('UTC') return date_time.replace(tzinfo=utc).astimezone(tz) def datetime_to_str(date_time): date_time_tzone = datetime_as_timezone(date_time, 'Asia/Shanghai') return '{0:%Y-%m-%d %H:%M}'.format(date_time_tzone)
from datetime import datetime from django.utils.timezone import utc time1 = datetime.now() time2 = datetime.utcnow() time3 = datetime.utcnow().replace(tzinfo=utc) # 进行时区转换 print(time1) print(time2) print(time3) ''' 结果输出: 2019-11-19 12:27:13.752947 2019-11-19 04:27:13.752947 2019-11-19 04:27:13.752947+00:00 '''