CMD 中的 ISO 周数

ISO week number in CMD

如何确定 Windows 批处理文件中的 (ISO 8601) 周数?

不幸的是 WMIC PATH WIN32_LOCALTIME GET /FORMAT:LIST 只有 WeekInMonth...

我找到了一些 very complex solutions。没有更简单的方法吗?

您可以使用 Ritchie Lawrence 的日期函数。 它维护在 Github 上。 https://ritchielawrence.github.io/batchfunctionlibrary/

这是代码。

 ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
 :DateToWeek %yy% %mm% %dd% yn cw dw
 ::
 :: By:   Ritchie Lawrence, Updated 2002-11-20. Version 1.1
 ::
 :: Func: Returns an ISO 8601 Week date from a calendar date.
 ::       For NT4/2000/XP/2003.
 :: 
 :: Args: %1 year component to be converted, 2 or 4 digits (by val)
 ::       %2 month component to be converted, leading zero ok (by val)
 ::       %3 day of month to be converted, leading zero ok (by val)
 ::       %4 var to receive year, 4 digits (by ref)
 ::       %5 var to receive calendar week, 2 digits, 01 to 53 (by ref)
 ::       %6 var to receive day of week, 1 digit, 1 to 7 (by ref)
 :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 setlocal ENABLEEXTENSIONS
 set yy=%1&set mm=%2&set dd=%3
 if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
 set /a dd=100%dd%%%100,mm=100%mm%%%100
 set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,Jd=153*m+2
 set /a Jd=Jd/5+dd+y*365+y/4-y/100+y/400-32045
 set /a y=yy+4798,Jp=y*365+y/4-y/100+y/400-31738,t=Jp+3,Jp=t-t%%7
 set /a y=yy+4799,Jt=y*365+y/4-y/100+y/400-31738,t=Jt+3,Jt=t-t%%7
 set /a y=yy+4800,Jn=y*365+y/4-y/100+y/400-31738,t=Jn+3,Jn=t-t%%7
 set /a Jr=%Jp%,yn=yy-1,yn+=Jd/Jt,yn+=Jd/Jn
 if %Jd% GEQ %Jn% (set /a Jr=%Jn%) else (if %Jd% GEQ %Jt% set /a Jr=%Jt%)
 set /a diff=Jd-Jr,cw=diff/7+1,wd=diff%%7,wd+=1
 if %cw% LSS 10 set cw=0%cw%
 endlocal&set %4=%yn%&set %5=%cw%&set %6=%wd%&goto :EOF

在批处理文件中试试:

@For /F %%I In ('PowerShell Get-Date -UFormat %%V') Do @Echo(%%I
@Timeout -1

或在命令提示符下 window:

For /F %I In ('PowerShell Get-Date -UFormat %V') Do @Echo(%I

没有可用于 CMD 的本机 Windows 命令可以轻松提供 ISO 8601 周数。事实上,以任何方式处理日期和时间都非常痛苦。

这就是我编写 getTimestamp.bat 的原因 - 一个用于进行 date/time 计算和格式化的通用实用程序。它是纯脚本(混合 batch/JScript),可以在任何 Windows XP 以后的机器上本地运行 - 不需要第 3 方 exe。

完整文档可通过 getTimestamp /?getTimestamp /?? 从命令行获得,如果您需要分页帮助。

以下将使用本地日期和时间打印 ISO 8601 周数:

call getTimeStamp /f {isowk}

您可以轻松地将结果存储在变量中

call getTimeStamp /f {isowk} /r week
echo ISO 8601 week = %week%