Select to view content in your preferred language

Convert date to string

681
3
Jump to solution
09-12-2018 03:25 PM
by Anonymous User
Not applicable

I created a query to pull the current weeks Monday but can't figure out how to convert that to a string and create a variable that prints it in this format ('%Y/%m/%d')

import datetime
today = datetime.date.today()
today + datetime.timedelta(days=-today.weekday())

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

for python 3 don't forget print is different

print(mon.strftime("%Y/%m/%d"))

View solution in original post

3 Replies
RandyBurton
MVP Alum

Try:

mon = today + datetime.timedelta(days=-today.weekday())
print mon.strftime("%Y/%m/%d")‍‍‍‍‍‍‍‍‍‍‍‍‍‍
DanPatterson_Retired
MVP Emeritus

for python 3 don't forget print is different

print(mon.strftime("%Y/%m/%d"))
RandyBurton
MVP Alum

Don't forget that .weekday() for Sunday = 6, so the Monday for Sunday 2018/09/09 is 2018/09/03 and not the following day (the 10th).

0 Kudos