I must have the current year and month in datetime.
I use this:
datem = datetime.today().strftime("%Y-%m")
datem = datetime.strptime(datem, "%Y-%m")
Is there maybe another way?
I must have the current year and month in datetime.
I use this:
datem = datetime.today().strftime("%Y-%m")
datem = datetime.strptime(datem, "%Y-%m")
Is there maybe another way?
Use:
I assume you want the first of the month.
Try this solution:
Use:
Reference: 8.1.7. strftime() and strptime() Behavior
Reference: strftime() and strptime() Behavior
The above things are useful for any date parsing, not only now or today. It can be useful for any date parsing.
And so on...
You can always use a sub-string method:
This will get you the current month and year in integer format. If you want them to be strings you simply have to remove the " int " precedence while assigning values to the variables
curr_year
andcurr_month
.You can write the accepted answer as a one-liner using
date.replace
: