获取批处理文件中工作日的简便方法

huangapple go评论68阅读模式
英文:

Easy way to get the weekday in a batch file

问题

我在寻找一个从%date%获取星期几的好简单答案,看到了很多答案,但没有一个非常流畅。

英文:

I was searching for a good easy answer to getting the day of the week from %date% and saw a lot of answers but none very eloquent.

答案1

得分: 1

以下是翻译好的内容:

你要的,就在这里!

@echo off
setlocal

for /F "tokens=1-3 delims=/" %%a in ("%date%") do set /A "M=1%%a-100,D=1%%b-100,Y=%%c"
set /A "DOW=( D + 30*(M-1)+M/2+(M>>3)*(M&1) - !(((2-M)>>31)+1)*(2-!(Y%%4)) +6)%7+1"
for /F "tokens=%DOW%" %%a in ("Sunday Monday Tuesday Wednesday Thursday Friday Saturday") do echo %date% is %%a

这是一个从%date%获取星期几的“好易用答案”。然而,我恐怕不理解“非常雄辩”是什么意思... **`;)`**

这个公式假定`%date%`以MM/DD/YYYY格式显示日期。如果顺序不同,只需更改公式中的`M`和`D`变量。

这种方法每到新年需要进行小的调整:只需将`+6`的调整值更改为在新年的1月1日得到正确星期几的值(该值必须在0和6之间)。这是为了保持该方法“简单易用”...
英文:

You ask for it, so here it is!

@echo off
setlocal

for /F "tokens=1-3 delims=/" %%a in ("%date%") do set /A "M=1%%a-100,D=1%%b-100,Y=%%c"
set /A "DOW=( D + 30*(M-1)+M/2+(M>>3)*(M&1) - !(((2-M)>>31)+1)*(2-!(Y%%4)) +6)%%7+1"
for /F "tokens=%DOW%" %%a in ("Sunday Monday Tuesday Wednesday Thursday Friday Saturday") do echo %date% is %%a

This is a "good easy answer to getting the day of the week from %date%". However, I'm afraid I don't understand what "very eloquent" means... ;)

This formula assumes that %date% show the date in MM/DD/YYYY format. If the order is a different one, just change the M and D variables in the formula.

This method requires a small adjustment every new year: just change the +6 adjustment to a value that get the right day of week on Jan/1st of the new year (the value must be between 0 and 6). This is done in order to keep the method "simple and easy"...

答案2

得分: 1

Here is the translated content:

另一种使用 Powershell 命令的解决方案:

@echo off
Title 获取并显示星期几
@for /f "delims=" %%a in ('Powershell -C "(Get-Date).ToString('dddd')"' ) do Set "DayOfWeek=%%a"
echo( 星期几是 %DayOfWeek%
Pause
英文:

Another solution using Powershell command :

@echo off
Title Get and Show The day of the week
@for /f "delims=" %%a in ('Powershell -C "(Get-Date).ToString('dddd')"') do Set "DayOfWeek=%%a"
echo( The day of the Week is %DayOfWeek%
Pause

huangapple
  • 本文由 发表于 2023年7月18日 09:10:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定