Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date object with year and month only

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 

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!

like image 642
daGrevis Avatar asked Jan 20 '13 13:01

daGrevis


People also ask

How do you create a Date object with day month and year?

Use the Date() constructor to create a date from the day, month and year values, e.g. const date = new Date(2022, 0, 24) . The Date() constructor takes the year, a zero-based value for the month and the day as parameters and returns a Date object.

How do I get the current month and year in HTML?

innerHTML = months[date. getMonth()] + ' ' + date. getFullYear(); };


1 Answers

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.

like image 50
Martijn Pieters Avatar answered Sep 23 '22 13:09

Martijn Pieters