I am trying to get the current date in ISO Calendar format as follows alongwith the zero padding on the week?
2019/W06
I tried the following, but prefer something using strftime as it is much easier to read.
print(str(datetime.datetime.today().isocalendar()[0]) + '/W' + str(datetime.datetime.today().isocalendar()[1]))
2019/W6
Use following code:
print(datetime.now().strftime('%Y/W%V'))
%Y Year with century as a decimal number.
%V - The ISO 8601 week number of the current year (01 to 53), where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week.
https://docs.python.org/3.7/library/datetime.html#strftime-and-strptime-behavior
Solution with strftime:
If you want the zero padding:
datetime.date.today().strftime("%Y/W%V")
Output:
2019/W06
If you don't want it:
datetime.date.today().strftime("%Y/W%-V")
Output:
2019/W6
Note that "%V" returns the week number, and the "-" is what removes the leading zero.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With