python datetine 获取时间例子

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获取上个月第一天和最后一天的日期和向后几个小时

#  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)


# Python计算当前时间向后8个小时的时间
>>> 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'
# str 转化为时间格式
b = datetime.datetime.strptime(a, "%Y-%m-%d %H:%M:%S")	
# 时间格式转化为str
c = b.strftime("%Y-%m-%d %H:%M:%S")

a1 = '20210513'
# str转时间格式:
b1 = datetime.datetime.strptime(a1, "%Y%m%d")
# 时间格式转str:
c1 = b1.strftime("%Y%m%d")

6. 2021-05-13转化成20210513

a = "2021-05-13"
b = a.replace("-","")
print(b)

7. 获得日期时间差

# 1. 相差多少天
>>> 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)

# 2. Python获取时间差,时间差单位为秒
>>> start_time = datetime.datetime.now()
>>> end_time = datetime.datetime.now()
>>> (end_time - start_time).seconds
7

8. 比较常用的方法

# 1. Python计算当前时间向后8个小时的时间
>>> d1 = datetime.datetime.now()
>>> d2 = d1 + datetime.timedelta(hours = 8)
>>> d2
datetime.datetime(2017, 3, 23, 1, 10, 37, 182240)


# 2. Python计算上周一和周日的日期
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)

# 3. Python计算指定日期当月最后一天的日期和本月天数
>>> 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

# 4. Python计算指定日期下个月当天的日期
>>> 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)

# 5. Python获得本周一至今天的时间段并获得上周对应同一时间段
>>> 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标准化日期

# isocalendar(...)*:返回一个包含三个值的元组,三个值依次为:year年份,week number周数,weekday星期数(周一为1…周日为7):
>>> a = datetime.date(2017,3,22)
>>> a.isocalendar()
(2017, 12, 3)
>>> a.isocalendar()[0]
2017
>>> a.isocalendar()[1]
12
>>> a.isocalendar()[2]
3

# isoformat(...): 返回符合ISO 8601标准 (YYYY-MM-DD) 的日期字符串;
>>> a = datetime.date(2017,3,22)
>>> a.isoformat()
'2017-03-22'


# isoweekday(...): 返回符合ISO标准的指定日期所在的星期数(周一为1…周日为7)
>>> a = datetime.date(2017,3,22)
>>> a.isoweekday()
3

# 与isoweekday(...)相似的还有一个weekday(...)方法,只不过是weekday(...)方法返回的周一为 0, 周日为 6
>>> a = datetime.date(2017,3,22)
>>> a.weekday()
2

# timetuple(...):该方法为了兼容time.localtime(...)返回一个类型为time.struct_time的数组,但有关时间的部分元素值为0:
>>> 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


# fromtimestamp(...):根据给定的时间戮,返回一个date对象
>>> 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'
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值