datetime模块用于是date和time模块的合集,datetime有两个常量,MAXYEAR和MINYEAR,分别是9999和1.
datetime模块定义了5个类,分别是
1.datetime.date:表示日期的类
2.datetime.datetime:表示日期时间的类
3.datetime.time:表示时间的类
4.datetime.timedelta:表示时间间隔,即两个时间点的间隔
5.datetime.tzinfo:时区的相关信息
str转date:dt = datetime.date(2019,2,12)
date转str:dt_str = date_date.strftime('%Y-%m-%d')
date类有三个参数,datetime.date(year,month,day),返回year-month-day
方法:
time.struct_time(tm_year=2017, tm_mon=4, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=105, tm_isdst=-1)
python中时间日期格式化符号:
str转datetime:dt = datetime.datetime.strptime(sign_day_str, "%Y-%m-%d")
datetime转str:dt_str = date_time.strftime('%Y-%m-%d')
datetime转date:dt = datetime.datetime.strptime(sign_day_str, "%Y-%m-%d").date()
date转datetime:dt = datetime(d.year, d.month, d.day)
datetime类有很多参数,datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]]),返回年月日,时分秒
datetime.datetime.now().strftime('%b-%d-%Y %H:%M:%S')
datetime.datetime.strptime('Apr-16-2017 21:01:35', '%b-%d-%Y %H:%M:%S')
datetime.datetime.timedelta用于计算两个日期之间的差值,例如:
>>> a=datetime.datetime.now()
>>> b=datetime.datetime.now()
>>> a
datetime.datetime(2017, 4, 16, 21, 21, 20, 871000)
>>> b
datetime.datetime(2017, 4, 16, 21, 21, 29, 603000)
>>> b-a
datetime.timedelta(0, 8, 732000)
>>> (b-a).seconds
8
或者
time1 = datetime.datetime(2016, 10, 20)
time2 = datetime.datetime(2015, 11, 2)
"""计算天数差值"""
print(time1-time2).days
"""计算两个日期之间相隔的秒数"""
print (time1-time2).total_seconds()
time类有5个参数,datetime.time(hour,minute,second,microsecond,tzoninfo),返回08:29:30
1.datetime.time.replace()
2.datetime.time.strftime(format):按照format格式返回时间
3.datetime.time.tzname():返回时区名字
4.datetime.time.utcoffset():返回时区的时间偏移量
# -*- coding: UTF-8 -*-
import datetime
import calendar
dt = datetime.date(2018,4,19)
yes_time = datetime.datetime.now()+ datetime.timedelta(days=-1)
week_ago = yes_time + datetime.timedelta(days=-7)
yes_time_str = yes_time.strftime('%Y-%m-%d')
print yes_time_str
firstday = yes_time.replace(day=1)
firstday_str = yes_time.replace(day=1).strftime('%Y-%m-%d')
days_num = calendar.monthrange(firstday.year, firstday.month)[1]
lastday = datetime.date(firstday.year, firstday.month, days_num).strftime('%Y-%m-%d')
print lastday
pre_month_lastday = yes_time.replace(day=1) - datetime.timedelta(1)
pre_month_lastday_str = pre_month_lastday.strftime('%Y-%m-%d')
pre_month_firstday = datetime.date(pre_month_lastday.year, pre_month_lastday.month, 1)
print pre_month_firstday
thisMonday = yes_time
oneday = datetime.timedelta(days = 1)
while thisMonday.weekday() != calendar.MONDAY:
thisMonday -= oneday
thisMonday_str = thisMonday.strftime('%Y-%m-%d')
print thisMonday_str
year = 2019
first_day = datetime.date(int(year),int(1),int(1)).strftime('%Y-%m-%d')
last_day = datetime.date(int(year),int(12),int(31)).strftime('%Y-%m-%d')
first_day_lst = datetime.date(int(year)-1,int(1),int(1)).strftime('%Y-%m-%d')
print first_day_lst
# 上月日期:不能直接月份-1,考虑1月份的情况:
dt = datetime.datetime.now()
now_Month = dt.strftime('%m')
now_Year = dt.strftime('%Y')
if int(now_Month) == 1 :
m1 = datetime.datetime(int(now_Year) - 1, 12, 1).strftime('%Y-%m-%d')
else:
m1 = datetime.datetime(int(now_Year), int(now_Month) - 1, 1).strftime('%Y-%m-%d')
print m1