将格式 ‘year-month-dayT11:04:11.373Z’ 在 Python 中转换为只有 y/m/d。

huangapple go评论51阅读模式
英文:

formatting 'year-month-dayT11:04:11.373Z' into only y/m/d in python

问题

我被困在尝试将日期格式从 '2022-12-10T11:04:11.373Z' 改变为只有年/月/日的格式上。虽然我阅读了其他帖子,但它们没有帮助,还有,我如何将一个年/月/日的输入与格式化后的日期进行比较呢?谢谢阅读,请帮助我!

我尝试过使用 datetime、strip 和一些其他方法,但都没有成功。

英文:

I got stuck trying to change this format of a date, '2022-12-10T11:04:11.373Z' into just the year/month/day.
While I read other posts it didn't help, and also how would I take a y/m/d input and compare the formatted date with the date inputted?
Thanks for reading and please help me!

I've tried using datetime, strip and some other stuff but none of it worked.

答案1

得分: 0

如果我理解你的问题正确,我认为你正在寻找 'datetime 模块'(你已经提到了它),但你还需要知道给定日期的格式字符串以及如何通常使用格式字符串。一旦你知道你要处理的数据的格式,你可以解析任何日期字符串,并且只要它符合正确的格式,你就可以将其转换为 datetime 对象,进行比较和其他操作,而不受每个日期的原始格式的影响。搜索要传递给 datetime 'strftime' / 'strptime' 方法的格式以获取 '% '格式说明符的含义;有相当多的格式说明符。这里有一个使用你提供的两种格式的非常简单的示例:

#!/usr/bin/python

import datetime
import sys
import argparse

# Correctly formatted command line args.
string_date1 = sys.argv[1]
string_date2 = sys.argv[2]

# Put the strings into datetime objects.
datetime_date1 = datetime.datetime.strptime(string_date1, "%Y-%m-%dT%H:%M:%S.%fZ")
datetime_date2 = datetime.datetime.strptime(string_date2, "%Y/%m/%d")

# Here are the dates packaged up into datetime objects.
print("Date 1: ", datetime_date1)
print("Date 2: ", datetime_date2)

# Compare
print("Date 1 > date 2: ", datetime_date1 > datetime_date2)
print("Date 2 > date 1: ", datetime_date2 > datetime_date1)
print("Dates are equal? ", datetime_date1 == datetime_date2)
print("Dates are not equal? ", datetime_date1 != datetime_date2)

使用和输出:

$ ./rudimentary_date_compare.py "2021-02-01T20:42:11.393Z" "2022/01/04"

Date 1:  2021-02-01 20:42:11.393000

Date 2:  2022-01-04 00:00:00

Date 1 > date 2:  False

Date 2 > date 1:  True

Dates are equal?  False

Dates are not equal?  True
英文:

If I follow your question correctly, I think you're looking for the the 'datetime module' (which you've mentioned) but you also need to know the format string of the given date and how to use format strings generally. Once you know the format of the data you're dealing with, you can parse any date string and provided it's in the correct format, you can then turn it into a datetime object and do comparisons and other operations regardless of the original format of each date. Google the formats to pass to the datetime 'strftime' / 'strptime' methods to get the meanings of the '%' format specifiers; there are quite a few of them. Here's a very simple example using the two formats you've supplied:

#!/usr/bin/python

import datetime
import sys
import argparse

# Correctly fomatted command line args.
string_date1=sys.argv[1]
string_date2=sys.argv[2]

# Put the strings into datetime objects.
datetime_date1=datetime.datetime.strptime(string_date1,"%Y-%m-%dT%H:%M:%S.%fZ")
datetime_date2=datetime.datetime.strptime(string_date2,"%Y/%m/%d")

# Here are the dates packaged up into datetime objects.
print("Date 1: ",datetime_date1)
print("Date 2: ",datetime_date2)

# Compare
print ("Date 1 > date 2: ", datetime_date1 > datetime_date2);
print ("Date 2 > date 1: ", datetime_date2 > datetime_date1);
print ("Dates are equal? ", datetime_date1 == datetime_date2);
print ("Dates are not equal? ", datetime_date1 != datetime_date2);

Usage and output:

$ ./rudimentary_date_compare.py "2021-02-01T20:42:11.393Z" "2022/01/04"

Date 1:  2021-02-01 20:42:11.393000

Date 2:  2022-01-04 00:00:00

Date 1 > date 2:  False

Date 2 > date 1:  True

Dates are equal?  False

Dates are not equal?  True

huangapple
  • 本文由 发表于 2023年6月18日 21:58:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定