Datetime模块常用方法(年、季度、月、周、日)

目录

  • Datetime模块常用方法
    • 1、年的加减
    • 2、月的加减
    • 3、日的加减
    • 4、小时的加减
    • 5、分钟的加减
    • 6、时间与字符串的转换
    • 7、两个日期计算相差多少天
    • 8、当前小时:00:00--00:00
    • 9、当前天:00:00:00--00:00:00
    • 10、当前周:00:00:00--00:00:00
    • 11、上一周:00:00:00--00:00:00
    • 12、当前月:00:00:00--00:00:00
    • 13、上一月:00:00:00--00:00:00
    • 14、当前季度:00:00:00--00:00:00
    • 15、上一季度:00:00:00--00:00:00
    • 16、当前年:00:00:00--00:00:00
    • 17、上一年:00:00:00--00:00:00
    • 18、计算当前日期是当年的第几天

Datetime模块常用方法

1、年的加减

import datetime
from dateutil.relativedelta import relativedelta
# 当前时间+3年
date_time = datetime.datetime.date(datetime.datetime.now()) + relativedelta(years=3)
print(date_time)
# 当前时间-3年
date_time = datetime.datetime.date(datetime.datetime.now()) + relativedelta(years=-3)
print(date_time)

2、月的加减

import datetime
from dateutil.relativedelta import relativedelta
# 当前时间+3月
date_time = datetime.datetime.now() + relativedelta(months=3)
print(date_time)
# 当前时间-3月
date_time = datetime.datetime.now() + relativedelta(months=-3)
print(date_time)

3、日的加减

import datetime
# 当前时间+3天
date_time = datetime.datetime.now() + datetime.timedelta(days=3)
print(date_time)
# 当前时间-3天
date_time = datetime.datetime.now() + datetime.timedelta(days=-3)
print(date_time)

4、小时的加减

import datetime
# 当前时间+3小时
date_time = datetime.datetime.now() + datetime.timedelta(hours=3)
print(date_time)
# 当前时间-3小时
date_time = datetime.datetime.now() + datetime.timedelta(hours=-3)
print(date_time)

5、分钟的加减

import datetime
# 当前时间+3分钟
date_time = datetime.datetime.now() + datetime.timedelta(minutes=3)
print(date_time)
# 当前时间-3分钟
date_time = datetime.datetime.now() + datetime.timedelta(minutes=-3)
print(date_time)

6、时间与字符串的转换

6.1、特殊的字符串类型

import datetime
date_time = '2012-05-29T19:30:03.283Z'
date_time = datetime.datetime.strptime(date_time, "%Y-%m-%dT%H:%M:%S.%fZ")
print(date_time)

6.2、常见的字符串类型

import datetime
date_time = '2012-05-29 19:30:03'
date_time = datetime.datetime.strptime(date_time, "%Y-%m-%d %H:%M:%S")

6.3、时间转换成字符串

import datetime
date_time = str(datetime.datetime.now())
print(date_time)

7、两个日期计算相差多少天

import datetime
d1 = datetime.datetime(2005, 2, 16)
d2 = datetime.datetime(2004, 12, 31)
print((d1 - d2).days)

8、当前小时:00:00–00:00

import datetime
time_str = "%Y-%m-%d %H"
gt_time = datetime.datetime.strptime(current_time.strftime(time_str), time_str)
lt_time = gt_time + datetime.timedelta(hours= 1)
print(gt_time)
print(lt_time)

9、当前天:00:00:00–00:00:00

import datetime
time_str = '%Y-%m-%d'
gt_time = datetime.datetime.strptime(current_time.strftime(time_str), time_str)
lt_time = gt_time + datetime.timedelta(days= 1)
print(gt_time)
print(lt_time)

10、当前周:00:00:00–00:00:00

import datetime
gt_time = current_time - datetime.timedelta(days=current_time.weekday())
lt_time = current_time + datetime.timedelta(days=7-current_time.weekday())
print(gt_time)
print(lt_time)
或
time_str = '%Y-%m-%d'
gt_time = datetime.datetime.strptime((current_time - datetime.timedelta(days=current_time.weekday())).strftime(time_str), time_str)
lt_time = gt_time + datetime.timedelta(days=7)
print(gt_time)
print(lt_time)

11、上一周:00:00:00–00:00:00

gt_time = current_time - datetime.timedelta(days=current_time.weekday()+7)
lt_time = current_time - datetime.timedelta(days=current_time.weekday()+1)
print(gt_time)
print(lt_time)
或
days = datetime.date.weekday(current_time)
gt_time = current_time + datetime.timedelta(days=-days-7,hours=-current_time.hour,minutes=-current_time.minute,seconds=-current_time.second)
lt_time = gt_time + datetime.timedelta(days=6)
print(gt_time)
print(lt_time)

12、当前月:00:00:00–00:00:00

gt_time = datetime.datetime(current_time.year, current_time.month, 1)
lt_time = datetime.datetime(current_time.year, current_time.month + 1, 1)
print(gt_time)
print(lt_time)

13、上一月:00:00:00–00:00:00

gt_time = datetime.datetime(current_time.year, current_time.month, 1) - datetime.timedelta(days=1)
gte_time = datetime.datetime(gt_time.year, gt_time.month, 1)
lt_time = datetime.datetime(gt_time.year, gt_time.month + 1, 1)
print(gt_time)
print(lt_time)

14、当前季度:00:00:00–00:00:00

month = (current_time.month - 1) - (current_time.month - 1) % 3 + 1
gt_time = datetime.datetime(current_time.year, month, 1)
lt_time = gt_time + relativedelta(months=3)
print(gt_time)
print(lt_time)

15、上一季度:00:00:00–00:00:00

month = (current_time.month - 1) - (current_time.month - 1) % 3 + 1
lt_time = datetime.datetime(current_time.year, month, 1)
gt_time = lt_time - datetime.timedelta(days=1)
gt_time = datetime.datetime(gt_time.year, gt_time.month - 2, 1)
print(gt_time)
print(lt_time)

16、当前年:00:00:00–00:00:00

gt_time = datetime.datetime(current_time.year, 1, 1)
lt_time = datetime.datetime(current_time.year + 1, 1, 1)
print(gt_time)
print(lt_time)

17、上一年:00:00:00–00:00:00

gt_time = datetime.datetime(datetime.datetime(current_time.year - 1, 1, 1).year, 1, 1)
lt_time = datetime.datetime(current_time.year, 1, 1)
print(gt_time)
print(lt_time)

18、计算当前日期是当年的第几天

import time
import datetime
cur = datetime.datetime.now()
cur = datetime.datetime.strftime(cur, "%Y-%m-%d")
cur = time.strptime(cur, "%Y-%m-%d")
print(cur.tm_year)
print(cur.tm_yday)

你可能感兴趣的:(Python,Datetime,python)