您的位置:首页 > 编程语言 > Python开发

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

2019-05-16 12:15 866 查看

最近需要生成日期时间的字符串,并且包含时间差计算。
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
%H Hour (24-hour clock) as a zero-padded decimal number. 00, 01, …, 23
%I Hour (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
%f Microsecond as a decimal number, zero-padded on the left. 000000, 000001, …, 999999
%z UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive). (empty), +0000, -0400, +1030, +063415, -030712.345216
%Z Time zone name (empty string if the object is naive). (empty), UTC, EST, CST
%j Day of the year as a zero-padded decimal number. 001, 002, …, 366
%U Week 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
%W Week 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

以上。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐