0

I need to get an ISO 8601 date that only displays the week number and two digit year code for a given date in python 3.0. This needs to be in the following format: YYWW (YY represents a two digit year code and WW represents the week number). I have tried working with the datetime module in python and using %G and %V to get the week number using strftime, but get a value error when running the following code:

from datetime import datetime
now_iso = (datetime.now().strftime('%G%V'))

Any help you can provide would be greatly appreciated. Thanks in advance. Here is the error I get:

Traceback (most recent call last):
  File "C:\Python27\Lib\lib-tk\Tkinter.py", line 1547, in __call__
    return self.func(*args)
  File "C:/Users/ctschantz/Python Project 3/Solenoids Label Program.py", line 881, in close_part
    part_validation()
  File "C:/Users/ctschantz/Python Project 3/Solenoids Label Program.py", line 245, in part_validation
    part_label_create()
  File "C:/Users/ctschantz/Python Project 3/Solenoids Label Program.py", line 58, in part_label_create
    now_bc = (datetime.now().strftime('%G%V'))
ValueError: Invalid format string
  • what should be expected output ? – Rahul Khanna Feb 26 '20 at 20:50
  • The expected output will be YYWW. Same format as if you used the following code:from datetime import datetime now_bc = (datetime.now().strftime('%y%W')) – Cody Tschantz Feb 26 '20 at 20:51
  • Show the actual error (preferrably with complete traceback). To my eye, your code is correct. – Błotosmętek Feb 26 '20 at 21:06
  • 1
    @CodyTschantz ***"get a value error"***: I get `202009`. Can't reproduce this with Python 2.7/3.x, please make sure, code you posts actually behaves as you claim. – stovfl Feb 26 '20 at 21:09
  • What specific version are you using? From [here](https://docs.python.org/3/library/datetime.html) - _New in version 3.6: `%G`, ... and `%V `were added._ And `%G` gives you 4 digits not 2. – andrewJames Feb 26 '20 at 21:13
  • @stovfl see updated post – Cody Tschantz Feb 26 '20 at 21:23
  • @CodyTschantz I see **C:\Python27\..`**. Those format specifiers require python3.6+. – Bakuriu Feb 26 '20 at 21:29
  • @CodyTschantz ***"C:\Python27"***: Double checked, works for me with *2.7.16 (default, Oct 14 2019, 21:26:56) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]* and i wonder because @ Bakuriu is right see [`2.7` strftime-strptime-behavior](https://docs.python.org/2.7/library/datetime.html#strftime-strptime-behavior) – stovfl Feb 26 '20 at 21:35

2 Answers2

1

A concise solution without %G and %V can be like this:

from datetime import datetime

year, week, _ = datetime.now().isocalendar()
print("{0}{1:02}".format(year % 100, week))

{1:02} means that add leading 0s to the argument with index 1 until its width is at least 2. For more information you can check Format Specification Mini-Language.

If the year can be printed with 4 digits then it becomes a one-liner:

print("{0}{1:02}".format(*datetime.now().isocalendar()))
akesfeden
  • 480
  • 2
  • 7
  • 12
0

I found a solution. It may not be the prettiest but:

from datetime import datetime

now_bc = (datetime.now().isocalendar())
now_bc_year = str(now_bc[0])
year_two_digits = now_bc_year[-2:]
now_bc_week = now_bc[1]

if len(str(now_bc_week)) == 1:
    td_week = '0' + str(now_bc_week)
else:
    td_week = now_bc_week

date_code = year_two_digits + td_week

print(date_code)
  • Yes, [datetime.date.isocalendar](https://docs.python.org/2.7/library/datetime.html#datetime.date.isocalendar) is the way to go. – stovfl Feb 26 '20 at 21:37