使用 Python 在 ISO 8601 日历中获取周数年数日期代码
Get a week number year number date code in the ISO 8601 calendar using Python
我需要获取一个 ISO 8601 日期,该日期仅显示 python 3.0 中给定日期的周数和两位数年份代码。这需要采用以下格式:YYWW(YY 代表两位数的年份代码,WW 代表周数)。我尝试在 python 中使用 datetime 模块并使用 %G 和 %V 使用 strftime 获取周数,但是当 运行 以下代码时出现值错误:
from datetime import datetime
now_iso = (datetime.now().strftime('%G%V'))
如果您能提供任何帮助,我们将不胜感激。提前致谢。这是我得到的错误:
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
我找到了解决办法。它可能不是最漂亮的,但是:
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)
没有%G
和%V
的简洁解决方案可以是这样的:
from datetime import datetime
year, week, _ = datetime.now().isocalendar()
print("{0}{1:02}".format(year % 100, week))
{1:02}
表示将前导 0
添加到索引为 1
的参数中,直到其宽度至少为 2
。有关详细信息,您可以查看 Format Specification Mini-Language.
如果年份可以用4位数字打印就变成一行:
print("{0}{1:02}".format(*datetime.now().isocalendar()))
我需要获取一个 ISO 8601 日期,该日期仅显示 python 3.0 中给定日期的周数和两位数年份代码。这需要采用以下格式:YYWW(YY 代表两位数的年份代码,WW 代表周数)。我尝试在 python 中使用 datetime 模块并使用 %G 和 %V 使用 strftime 获取周数,但是当 运行 以下代码时出现值错误:
from datetime import datetime
now_iso = (datetime.now().strftime('%G%V'))
如果您能提供任何帮助,我们将不胜感激。提前致谢。这是我得到的错误:
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
我找到了解决办法。它可能不是最漂亮的,但是:
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)
没有%G
和%V
的简洁解决方案可以是这样的:
from datetime import datetime
year, week, _ = datetime.now().isocalendar()
print("{0}{1:02}".format(year % 100, week))
{1:02}
表示将前导 0
添加到索引为 1
的参数中,直到其宽度至少为 2
。有关详细信息,您可以查看 Format Specification Mini-Language.
如果年份可以用4位数字打印就变成一行:
print("{0}{1:02}".format(*datetime.now().isocalendar()))