如果日期为python

发布于 2025-01-23 17:23:12 字数 315 浏览 3 评论 0原文

我有2个日期的日期,其中一种包含日期,月和年,第二个仅包含一个月和日期。即----> 2019年12月21日和7月3日。

现在,我想检查日期是否包含年。我的代码如下:

import datetime

x = datetime.datetime(2020, 5, 17)

if '%Y' in x:
   print("contains year")
else:
   print("doesn't contain year")

但是我遇到了一个错误:typeError:类型'dateTime.dateTime'的参数是不可能的

I have 2 dates in datetime format, one of them contains date, month and year and the second contains only month and date. ie----> 21 December 2019 and July 3.

Now I want to check if a date contains year or not. My code is as follows:

import datetime

x = datetime.datetime(2020, 5, 17)

if '%Y' in x:
   print("contains year")
else:
   print("doesn't contain year")

But I'm getting an error: TypeError: argument of type 'datetime.datetime' is not iterable

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳公公是暖光 2025-01-30 17:23:12

如果x是日期时间,那一定是一年。但是,随着12月3日的结果,我认为您实际上是指字符串。

在这种情况下,您可以使用的最佳工具是max。获取X中的更大数字,然后检查这是一年。

x = (2020, 5, 17)
max_record = max(x)
if max_record > 32: #Since we know that the month will be 12 and the day will be 31 at the most, numbers greater than 32 should be considered years.
    print("X have a year.: " + str(max_record))
else:
    print("X don't have year.")

If x is a datetime, it must be the year. But with the result of 3 December that I assume you actually mean string.

The best tool you can use in this situation is max. Get the bigger number in x and check this is a year.

x = (2020, 5, 17)
max_record = max(x)
if max_record > 32: #Since we know that the month will be 12 and the day will be 31 at the most, numbers greater than 32 should be considered years.
    print("X have a year.: " + str(max_record))
else:
    print("X don't have year.")
浪荡不羁 2025-01-30 17:23:12

迭代基本上是重复某些内容,在CS中通常适用于您可以算出的列表,阵列或其他项目。错误是由于您像列表一样对待DateTime对象的事实引起的,而Python没有看到值重复的部分。

您坚持认为自己有一个以上的DateTime对象,并且其中一个人丢失了这一年。很难看到这是如何可能的,因为您需要提供所有三个:年,每月和每天;当您创建该对象时。

话虽如此,我们可以从两个方向攻击问题。首先,我们可以假设所有有效的工作都可以检查DateTime对象是否有效的日期值。如果日期时间对象可能出现问题,我们可以将对象转换为字符串,并以多种不同方式检查字符串以获取所需的年度信息。以下检查以查看长度是否小于全日期字符串,是否有预期位置的破折号,以及日期是否是意外的默认值。

import datetime
x = datetime.datetime(1900,12,2)

if x.year == 1900:
    print("invalid date")
    
#assuming something might be wrong with the datetime object
#converting it to a string will let you examine the parts:
#string format of object: "1900-12-02 00:00:00"
d = str(x)
if len(d) < 19 or d[4:5] != "-" or d[0:4] == "1900":
    print("invalid date")

Iteration is basically repeating something and in CS usually applies to lists, arrays or other items you can count through. The error is caused by the fact that you are treating a datetime object like a list, and python isn't seeing a repeatable part of the value.

You maintain that you have more than one datetime object, and that one of them is missing the year. It is hard to see how that is possible, as you are required to supply all three: year, month and day; when you are creating that object.

With that said, we can attack the problem from two directions. First we can assume that everything works that we can check the datetime object for a valid date value. If something might be wrong with the date time object we can convert the object to a string and check the string for the required year info in multiple different ways. The following checks to see if the length is less than a full date time string, if there is a dash in the expected position, and if the date is an unexpected default value.

import datetime
x = datetime.datetime(1900,12,2)

if x.year == 1900:
    print("invalid date")
    
#assuming something might be wrong with the datetime object
#converting it to a string will let you examine the parts:
#string format of object: "1900-12-02 00:00:00"
d = str(x)
if len(d) < 19 or d[4:5] != "-" or d[0:4] == "1900":
    print("invalid date")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文