python使用datetime模块创造包含时间差计算的特定格式“日期时间”字符串

最近需要生成日期时间的字符串,并且包含时间差计算。
python的time模块能够满足字符串生成的需求,但是时间差计算就不行了。
datetime模块,可以生成所需日期和时间的字符串,还可以进行时间差计算。
导入datetime模块
import datetime
datetime.datetime.now()
生成一个datetime.datetime对象,包含了年、月、日、时、分、秒、毫秒七个参数。
可以直接获取年、月、日等数值:

t = datetime.datetime.now()

print(t.year, t.month, t.day, t.hour, t.minute, t.second, t.microsecond)
print(type(t.year), type(t.month), type(t.day), type(t.hour), type(t.minute), type(t.second), type(t.microsecond))
# output: 
2019 5 16 11 31 20 725790
<class 'int'> <class 'int'> <class 'int'> <class 'int'> <class 'int'> <class 'int'> <class 'int'>

可以分别获得日期,时间:

print(t.date(), type(t.date()))
print(t.time(), type(t.time()))
# output:
2019-05-16 <class 'datetime.date'>
11:34:32.314033 <class 'datetime.time'>

按需格式化输出字符串
datetime.datetime.strftime("format")

指令意义示例
%a当地工作日的缩写。Sun, Mon, …, Sat (美国);So, Mo, …, Sa (德国)
%A当地工作日的全名。Sunday, Monday, …, Saturday (美国);Sonntag, Montag, …, Samstag (德国)
%w以十进制数显示的工作日,其中0表示星期日,6表示星期六。0, 1, …, 6
%d补零后,以十进制数显示的月份中的一天。01, 02, …, 31
%b当地月份的缩写。Jan, Feb, …, Dec (美国);Jan, Feb, …, Dez (德国)
%B当地月份的全名。January, February, …, December (美国);Januar, Februar, …, Dezember (德国)
%m补零后,以十进制数显示的月份。01, 02, …, 12
%y补零后,以十进制数表示的,不带世纪的年份。00, 01, …, 99
%Y十进制数表示的带世纪的年份。0001, 0002, …, 2013, 2014, …, 9998, 9999
%HHour (24-hour clock) as a zero-padded decimal number.00, 01, …, 23
%IHour (12-hour clock) as a zero-padded decimal number.01, 02, …, 12
%p本地化的 AM 或 PM 。AM, PM (美国)am, pm (德国)
%M补零后,以十进制数显示的分钟。00, 01, …, 59
%S补零后,以十进制数显示的秒。00, 01, …, 59
%fMicrosecond as a decimal number, zero-padded on the left.000000, 000001, …, 999999
%zUTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive).(empty), +0000, -0400, +1030, +063415, -030712.345216
%ZTime zone name (empty string if the object is naive).(empty), UTC, EST, CST
%jDay of the year as a zero-padded decimal number.001, 002, …, 366
%UWeek number of the year (Sunday as the first day of the week) as a zero padded decimal number.All days in a new year preceding the first Sunday are considered to be in week 0.00, 01, …, 53
%WWeek number of the year (Monday as the first day of the week) as a decimal number.All days in a new year preceding the first Monday are considered to be in week 0.00, 01, …, 53
%c本地化的适当日期和时间表示。Tue Aug 16 21:30:00 1988 (美国);Di 16 Aug 21:30:00 1988 (德国)
%x本地化的适当日期表示。08/16/88 (None);08/16/1988 (en_US);16.08.1988 (de_DE)
%X本地化的适当时间表示。21:30:00 (en_US);21:30:00 (de_DE)
%%字面的 ‘%’ 字符。%

格式可以用任意连接符组合成你想要的格式,比如2016-05/21_12:35.12

t = datetime.datetime.now()
print(t.strftime("%Y%m%d"),type(t.strftime("%Y%m%d")))
print(t.strftime("%Y-%m_%d:%H-%M:%S"))
# output:
20190516 <class 'str'>
2019-05_16:11-58:30

注意,此时返回的就是字符串了。
datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
通过datetime.timedelta类创建一个时间差,用来进行计算。

创建两个函数,分别返回utc时间和local时间的特定格式的日期时间字符串:

import datetime


def local_time():  # 本地日期时间生成
    # 结束时间
    end_time = datetime.datetime.now()
    end_date, end_hour = end_time.strftime("%Y%m%d"), end_time.strftime("%H")
    # 15分钟间隔
    timespan = datetime.timedelta(minutes=15)
    # 起始时间
    start_time = end_time - timespan
    start_date, start_hour = start_time.strftime("%Y%m%d"), start_time.strftime("%H")
    return start_date, start_hour, end_date, end_hour


def utc_time():   # utc日期时间生成
    # 结束时间
    end_time = datetime.datetime.utcnow()
    end_date, end_hour = end_time.strftime("%Y%m%d"), end_time.strftime("%H")
    # 15分钟间隔
    timespan = datetime.timedelta(minutes=15)
    # 起始时间
    start_time = end_time - timespan
    start_date, start_hour = start_time.strftime("%Y%m%d"), start_time.strftime("%H")
    return start_date, start_hour, end_date, end_hour

以上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值