datetime – Getting today’s date in YYYY-MM-DD in Python?

The Question :

432 people think this question is useful

I’m using:


to return today’s date in the YYYY-MM-DD format.

Is there a less crude way to achieve this?

The Question Comments :

The Answer 1

752 people think this answer is useful

You can use strftime:

from datetime import datetime'%Y-%m-%d')

Additionally, for anyone also looking for a zero-padded Hour, Minute, and Second at the end: (Comment by Gabriel Staples)'%Y-%m-%d-%H:%M:%S')

The Answer 2

145 people think this answer is useful

You can use and convert the resulting object to a string:

from datetime import date
today = str(
print(today)   # '2017-12-26'

The Answer 3

41 people think this answer is useful

Datetime is just lovely if you like remembering funny codes. Wouldn’t you prefer simplicity?

>>> import arrow

This module is clever enough to understand what you mean.

Just do pip install arrow.

Addendum: In answer to those who become exercised over this answer let me just say that arrow represents one of the alternative approaches to dealing with dates in Python. That’s mostly what I meant to suggest.

The Answer 4

37 people think this answer is useful

I always use the isoformat() function for this.

from datetime import date    
today =
print(today) # '2018-12-05'

Note that this also works on datetime objects if you need the time in standard format as well.

from datetime import datetime
now =
print(now) # '2018-12-05T11:15:55.126382'

The Answer 5

15 people think this answer is useful

Other answers suggest the use of python’s datetime.datetime, but as @Bill Bell said, there are other libraries that offer simpler datetime interfaces either as a service or as part of a larger ecosystem of APIs. Here are two such libraries that make working with datetimes very simple.


You can use pd.to_datetime from the pandas library. Here are various options, depending on what you want returned.

import pandas as pd

pd.to_datetime('today')  # pd.to_datetime('now')
# Timestamp('2019-03-27 00:00:10.958567')

As a python datetime object,

# datetime.datetime(2019, 4, 18, 3, 50, 42, 587629)

As a formatted date string,

# '2019-04-18T04:03:32.493337'

# Or, `strftime` for custom formats.
# '2019-03-27'

To get just the date from the timestamp, call

#, 3, 27)

Aside from to_datetime, you can directly instantiate a Timestamp object using,

pd.Timestamp('today')  # pd.Timestamp('now')
# Timestamp('2019-04-18 03:43:33.233093')

# datetime.datetime(2019, 4, 18, 3, 53, 46, 220068)

If you want to make your Timestamp timezone aware, pass a timezone to the tz argument.

pd.Timestamp('now', tz='America/Los_Angeles')
# Timestamp('2019-04-18 03:59:02.647819-0700', tz='America/Los_Angeles')


If you’re working with pendulum, there are some interesting choices. You can get the current timestamp using now() or today’s date using today().

import pendulum
# DateTime(2019, 3, 27, 0, 2, 41, 452264, tzinfo=Timezone('America/Los_Angeles'))
# DateTime(2019, 3, 27, 0, 0, 0, tzinfo=Timezone('America/Los_Angeles'))

Additionally, you can also get tomorrow() or yesterday()‘s date directly without having to do any additional timedelta arithmetic.

# DateTime(2019, 3, 26, 0, 0, 0, tzinfo=Timezone('America/Los_Angeles'))

# DateTime(2019, 3, 28, 0, 0, 0, tzinfo=Timezone('America/Los_Angeles'))

There are various formatting options available.
# '2019-03-27'
# 'Mar 27, 2019'
# 'Wed, Mar 27, 2019 12:04 AM'

The Answer 6

12 people think this answer is useful

Very late answer, but you can use:

import time
today = time.strftime("%Y-%m-%d")
# 2020-02-14

The Answer 7

3 people think this answer is useful

You can use,

>>> from datetime import date

The Answer 8

0 people think this answer is useful

my code is a little complicated but I use it a lot

strftime("%y_%m_%d", localtime(time.time()))


you can look at the reference to make anything you want for you what YYYY-MM-DD just change my code to:

strftime("%Y-%m-%d", localtime(time.time()))

The Answer 9

0 people think this answer is useful

This works:

from datetime import date

Output in this time: 2020-08-29


this_year =
this_month =
this_day =

The Answer 10

-1 people think this answer is useful

To get day number from date is in python

for example:19-12-2020(dd-mm-yyy)order_date we need 19 as output

order['day'] = order['Order_Date'].apply(lambda x:

The Answer 11

-5 people think this answer is useful

I prefer this, because this is simple, but maybe somehow inefficient and buggy. You must check the exit code of shell command if you want a strongly error-proof program.

os.system('date +%Y-%m-%d')

Add a Comment