1. 获取今天日期 %Y-%m-%d
import datetime
today = datetime.datetime.today()
today = today.strftime("%Y-%m-%d")
2. 获取今天%Y-%m-%d %H:%M:%S
import datetime
now = datetime.datetime.now()
todat_time = now.strftime("%Y-%m-%d %H:%M:%S")
>>> now.date()
datetime.date(2017, 3, 22)
>>> now.time()
datetime.time(16, 55, 49, 148233)
today = datetime.date.today()
3. Python获取上个月第一天和最后一天的日期和向后几个小时
>>> today = datetime.date.today()
>>> today
datetime.date(2017, 3, 22)
>>> mlast_day = datetime.date(today.year, today.month, 1) - datetime.timedelta(1)
>>> mlast_day
datetime.date(2017, 2, 28)
>>> mfirst_day = datetime.date(mlast_day.year, mlast_day.month, 1)
>>> mfirst_day
datetime.date(2017, 2, 1)
>>> d1 = datetime.datetime.now()
>>> d2 = d1 + datetime.timedelta(hours = 8)
>>> d2
datetime.datetime(2017, 3, 23, 1, 10, 37, 182240)
4. 获取星期几
import datetime
week = datetime.datetime.weekday(datetime.datetime.now())
print(week)
5. str和时间格式相互转化
a = '2021-05-13 13:00:00'
b = datetime.datetime.strptime(a, "%Y-%m-%d %H:%M:%S")
c = b.strftime("%Y-%m-%d %H:%M:%S")
a1 = '20210513'
b1 = datetime.datetime.strptime(a1, "%Y%m%d")
c1 = b1.strftime("%Y%m%d")
6. 2021-05-13转化成20210513
a = "2021-05-13"
b = a.replace("-","")
print(b)
7. 获得日期时间差
>>> a=datetime.date(2017,3,22)
>>> b=datetime.date(2017,3,15)
>>> a
datetime.date(2017, 3, 22)
>>> b
datetime.date(2017, 3, 15)
>>> a.__sub__(b)
datetime.timedelta(7)
>>> a.__rsub__(b)
datetime.timedelta(-7)
>>> start_time = datetime.datetime.now()
>>> end_time = datetime.datetime.now()
>>> (end_time - start_time).seconds
7
8. 比较常用的方法
>>> d1 = datetime.datetime.now()
>>> d2 = d1 + datetime.timedelta(hours = 8)
>>> d2
datetime.datetime(2017, 3, 23, 1, 10, 37, 182240)
today = datetime.date.today()
>>> today
datetime.date(2017, 3, 23)
>>> today_weekday = today.isoweekday()
>>> last_sunday = today - datetime.timedelta(days=today_weekday)
>>> last_monday = last_sunday - datetime.timedelta(days=6)
>>> last_sunday
datetime.date(2017, 3, 19)
>>> last_monday
datetime.date(2017, 3, 13)
>>> date = datetime.date(2017,12,20)
>>> def eomonth(date_object):
... if date_object.month == 12:
... next_month_first_date = datetime.date(date_object.year+1,1,1)
... else:
... next_month_first_date = datetime.date(date_object.year, date_object.month+1, 1)
... return next_month_first_date - datetime.timedelta(1)
...
>>> eomonth(date)
datetime.date(2017, 12, 31)
>>> eomonth(date).day
31
>>> date = datetime.date(2017,12,20)
>>> def edate(date_object):
... if date_object.month == 12:
... next_month_date = datetime.date(date_object.year+1, 1,date_object.day)
... else:
... next_month_first_day = datetime.date(date_object.year,date_object.month+1,1)
... if date_object.day > eomonth(last_month_first_day).day:
... next_month_date = datetime.date(date_object.year,date_object.month+1,eomonth(last_month_first_day).day)
... else:
... next_month_date = datetime.date(date_object.year, date_object.month+1, date_object.day)
... return next_month_date
...
>>> edate(date)
datetime.date(2018, 1, 20)
>>> today = datetime.date.today()
>>> this_monday = today - datetime.timedelta(today.isoweekday()-1)
>>> last_monday = this_monday - datetime.timedelta(7)
>>> last_weekday = today -datetime.timedelta(7)
>>> this_monday
datetime.date(2017, 3, 20)
>>> today
datetime.date(2017, 3, 23)
>>> last_monday
datetime.date(2017, 3, 13)
>>> last_weekday
datetime.date(2017, 3, 16)
8. ISO标准化日期
>>> a = datetime.date(2017,3,22)
>>> a.isocalendar()
(2017, 12, 3)
>>> a.isocalendar()[0]
2017
>>> a.isocalendar()[1]
12
>>> a.isocalendar()[2]
3
>>> a = datetime.date(2017,3,22)
>>> a.isoformat()
'2017-03-22'
>>> a = datetime.date(2017,3,22)
>>> a.isoweekday()
3
>>> a = datetime.date(2017,3,22)
>>> a.weekday()
2
>>> a = datetime.date(2017,3,22)
>>> a.timetuple()
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=22, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=81, tm_isdst=-1)
>>> a.timetuple().tm_year
2017
>>> a.timetuple().tm_mon
3
>>> a.timetuple().tm_mday
22
>>> time.time()
1490165087.2242179
>>> datetime.date.fromtimestamp(time.time())
datetime.date(2017, 3, 22)
9. 日期的字符串输出
>>> a = datetime.date(2017,3,22)
>>> a.__format__('%Y-%m-%d')
'2017-03-22'
>>> a.__format__('%Y/%m/%d')
'2017/03/22'
>>> a.__format__('%y/%m/%d')
'17/03/22'
>>> a.__format__('%D')
'03/22/17'
>>> a.strftime("%Y%m%d")
'20170322'
>>> a.__str__()
'2017-03-22'
>>> a.ctime()
'Wed Mar 22 00:00:00 2017'