Datetime current year and month in Python

PythonDatetime

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

Try this solution:

from datetime import datetime

currentSecond= datetime.now().second
currentMinute = datetime.now().minute
currentHour = datetime.now().hour

currentDay = datetime.now().day
currentMonth = datetime.now().month
currentYear = datetime.now().year

Solution 2 - Python

Use:

from datetime import datetime
today = datetime.today()
datem = datetime(today.year, today.month, 1)

I assume you want the first of the month.

Solution 3 - Python

Use:

from datetime import datetime

current_month = datetime.now().strftime('%m') // 02 //This is 0 padded
current_month_text = datetime.now().strftime('%h') // Feb
current_month_text = datetime.now().strftime('%B') // February

current_day = datetime.now().strftime('%d')   // 23 //This is also padded
current_day_text = datetime.now().strftime('%a')  // Fri
current_day_full_text = datetime.now().strftime('%A')  // Friday

current_weekday_day_of_today = datetime.now().strftime('%w') //5  Where 0 is Sunday and 6 is Saturday.

current_year_full = datetime.now().strftime('%Y')  // 2018
current_year_short = datetime.now().strftime('%y')  // 18 without century

current_second= datetime.now().strftime('%S') //53
current_minute = datetime.now().strftime('%M') //38
current_hour = datetime.now().strftime('%H') //16 like 4pm
current_hour = datetime.now().strftime('%I') // 04 pm

current_hour_am_pm = datetime.now().strftime('%p') // 4 pm

current_microseconds = datetime.now().strftime('%f') // 623596 Rarely we need.

current_timzone = datetime.now().strftime('%Z') // UTC, EST, CST etc. (empty string if the object is naive).

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.

e.g.
my_date = "23-02-2018 00:00:00"

datetime.strptime(str(my_date),'%d-%m-%Y %H:%M:%S').strftime('%Y-%m-%d %H:%M:%S+00:00')

datetime.strptime(str(my_date),'%d-%m-%Y %H:%M:%S').strftime('%m')

And so on...

Solution 4 - Python

Late answer, but you can also use:

import time
ym = time.strftime("%Y-%m")

Solution 5 - Python

>>> from datetime import date
>>> date.today().month
2
>>> date.today().year
2020
>>> date.today().day
13

Solution 6 - Python

You can write the accepted answer as a one-liner using date.replace:

datem = datetime.today().replace(day=1)

Solution 7 - Python

The question asks to use datetime specifically.

This is a way that uses datetime only:

from datetime import datetime
year = datetime.now().year
month = datetime.now().month

Solution 8 - Python

You can always use a sub-string method:

import datetime;

today = str(datetime.date.today());
curr_year = int(today[:4]);
curr_month = int(today[5:7]);

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 and curr_month.

Solution 9 - Python

using the regular expression extract the date and time from the string then convert the string date to a datetimeindex using strptime

import re

s = "Audio was recorded at 21:50:00 02/07/2019 (UTC) by device 243B1F05 at gain setting 2 while battery state was 3.6V."

t = re.search(' (\d{2}:\d{2}:\d{2} \d{2}\/\d{2}\/\d{4}) ', s).group(1)

date=datetime.datetime.strptime(t,'%H:%M:%S %m/%d/%Y')
print(type(date))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3778327View Question on Stackoverflow
Solution 1 - PythonGaurav DaveView Answer on Stackoverflow
Solution 2 - PythonllogiqView Answer on Stackoverflow
Solution 3 - PythonAnup YadavView Answer on Stackoverflow
Solution 4 - PythonPedro LobitoView Answer on Stackoverflow
Solution 5 - PythonsanfiratView Answer on Stackoverflow
Solution 6 - PythonMad PhysicistView Answer on Stackoverflow
Solution 7 - PythonMattia RasuloView Answer on Stackoverflow
Solution 8 - PythonJaymeen_JKView Answer on Stackoverflow
Solution 9 - PythonGolden LionView Answer on Stackoverflow