简体   繁体   English

从字符串转换为 ISO 时间格式不起作用

[英]Converting from String into ISO time format not working

Been trying to set a time range of 15 min between now and 15 minutes before.一直在尝试将时间范围设置为从现在到 15 分钟之前的 15 分钟。 So far no problem.到目前为止没问题。 Here is the code:这是代码:

from datetime import datetime 

now = datetime.now()
past15min = (now - timedelta(minutes=15)).isoformat()[:-3] + 'Z'

print(type(now),now)
print(type(past15min),past15min)

Output:输出:

<class 'datetime.datetime'> 2022-12-21 17:51:18.233462
<class 'str'> 2022-12-21T17:36:18.233Z

Now, the issue is that I need it to be strictly in ISO format that ends with 3 digits + the letter Z at the end.现在,问题是我需要它严格采用 ISO 格式,以 3 位数字 + 最后的字母 Z 结尾。 I'm using this as part of another language Elastic DSL.我将其用作另一种语言 Elastic DSL 的一部分。 There, I'm using the variable now in the beginning timerange, and the variable past15min in the end timerange.在那里,我在开始时间范围内使用变量now ,在结束时间范围内使用变量past15min What happens is that now is in datetime format and end in 6 digits.发生的情况是现在是日期时间格式并以 6 位数字结尾。 If I try to type cast it into a string, then the calculation in the past15min variable won't work since you can't subtract strings.如果我尝试将其类型转换为字符串,那么 past15min 变量中的计算将不起作用,因为您不能减去字符串。

now = str(datetime.now())
past15min = (now - timedelta(minutes=15)).isoformat()[:-3] + 'Z'

print(type(now),now)
print(type(past15min),past15min)

Output:输出:

    past15min = (now - timedelta(minutes=15)).isoformat()[:-3] + 'Z'
TypeError: unsupported operand type(s) for -: 'str' and 'datetime.timedelta'

I also tried to first edit the time into its correct format, which turns it into a string automatically, and then convert it into iso format again to I can do the calculation, but I get an error still.我还尝试先将时间编辑成正确的格式,自动将其转换为字符串,然后再次将其转换为iso格式以进行计算,但仍然出现错误。

now = datetime.now().isoformat()[:-3] + 'Z'
now_to_datetime = datetime(now)
past15min = (now_to_datetime - timedelta(minutes=15)).isoformat()[:-3] + 'Z'

print(type(now),now)
print(now_to_datetime)
print(type(past15min),past15min)

Output:输出:

    now_to_datetime = datetime(now)
TypeError: 'str' object cannot be interpreted as an integer

Any help is appreciated.任何帮助表示赞赏。

Only format the datetimes into a string before printing.仅在打印前将日期时间格式化为字符串。

from datetime import datetime, timedelta
now = datetime.now()
past15min = (now - timedelta(minutes=15))

print(type(now),now.isoformat(timespec='milliseconds') + 'Z')
print(type(past15min),past15min.isoformat(timespec='milliseconds') + 'Z')

Why don't you do the string formatting after you perform the calculations?为什么不在执行计算后进行字符串格式化?


now = datetime.utcnow()
past15min = (now - timedelta(minutes=15))

formatted_now = now.isoformat()[:-3] + 'Z' # '2022-12-21T09:22:34.213Z'
formatted_past15min = past15min.isoformat()[:-3] + 'Z' # '2022-12-21T09:07:34.213Z'

You can now send the formatted data to whatever external system you want.您现在可以将格式化的数据发送到您想要的任何外部系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM