仅包含年份和月份的日期对象 [英] Date object with year and month only

查看:104
本文介绍了仅包含年份和月份的日期对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建具有年份和年份的 date 对象仅一个月?我不需要一天.

Is it possible to create date object with year and month only? I don't need day.

In [5]: from datetime import date

In [6]: date(year=2013, month=1)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-a84d4034b10c> in <module>()
----> 1 date(year=2013, month=1)

TypeError: Required argument 'day' (pos 3) not found

我在字典中使用date对象作为键,并且1月20日必须与1月21日具有相同的键,因为它们在同一年和同一月.

I'm using the date object as key in my dictionary and January 20 must have the same key as January 21, because they are in same month and year.

在此之前,我使用了一个简单的整数作为月份数字.不幸的是,我也需要知道这一年!

I used a simple integer before that as a month number. Unfortunately I need to know the year too!

推荐答案

不,您不能这样做.对于您的用例,请改用元组:

No, you can't do that. For your usecase, use a tuple instead:

key = (2013, 1)

由于您不需要对元组的值进行日期操作就足够了.

Since you don't need to do date manipulations on the value a tuple more than suffices.

这篇关于仅包含年份和月份的日期对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆