Convert date to string

219
3
Jump to solution
09-12-2018 03:25 PM
vonmoosa
MVP

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 Esteemed Contributor

for python 3 don't forget print is different

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

View solution in original post

3 Replies
RandyBurton
MVP Regular Contributor

Try:

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

for python 3 don't forget print is different

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

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