根据一天中的时间以不同方式问候用户

Greet user differently depending on time of day

我有以下 Windows 批处理文件,我发现它可以根据一天中的不同时间用早安或晚安来问候用户:

for /f "delims=:" %%A in ("%time%") do if %%A LSS 12 (echo. Good Morning %username%.) else (echo. Good Afternoon %username%.)

但是,我希望它根据以下规则进行响应:

如果时间是 06:00 到 11:59,问候语应该是“早安 %username%”。 =11=]

如果时间是 12:00 到 17:59,问候语应该是“下午好 %username%”。 =11=]

如果时间是 18:00 到 19:59,问候语应该是“晚上好 %username%”。 =11=]

如果时间是 20:00 到 05:59,问候语应该是“你不应该在床上 %username% 吗?"

有人可以告诉我这是否可行吗?如果可以,请告诉我怎么做?

非常感谢,

布里

无需使用 for 循环来剖析 %time%。只需利用 string 比较的完成方式:

@echo off
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set "tim=%%I"
set "tim=%tim:~8,4%"
if "%tim%" geq "20" echo Shouldn't you be in bed %username%?&goto :done
if "%tim%" geq "18" echo Good Evening %username%&goto :done
if "%tim%" geq "12" echo Good Afternoon %username%&goto :done
if "%tim%" geq "06" echo Good Morning %username%&goto :done
echo Shouldn't you be in bed %username%
:done

(请注意:if "%time%" geq "1730" ... 也有效)

根据您的评论进行编辑。

  • 实现了一种独立于区域设置获取时间的方法(采用 ISO 格式,比 AM/PM 格式更好处理)
  • 问:“if 语句后的额外 echo Shouldn't you be in bed %username% 是做什么用的?- 答:处理午夜 (00:00) 和 05:59 之间的时间。逻辑上应该是 if "tim" geq "00" ... 但那已经过时了。
  • 问:“为什么要使用 set "tim=%time: =0%" 设置 tim 变量?- 答:不要乱用 %time% 变量。设置它会禁用它显示实际时间的能力,所以我们只使用另一个变量名。set 命令将字符串中的每个 space 替换为零(使用 wmic 获取时间时已过时)
  • 在编程中使用 AM/PM 很尴尬。将 24 小时时间格式转换为 12AM/PM 格式以在代码中使用纯属无稽之谈(从我的(欧洲)背景来看,它甚至对“人类可读性”没有意义,但是 ymmv,当你习惯 AM/PM)

为清楚起见,下面是我使用的完全有效的解决方案。

注意:我在问候语中使用 24 小时制,但还将时间转换为 12 小时制并添加 AM/PM 以显示时间。

@echo off &cls
mode con: cols=100 lines=40  &color f0
setLocal

REM Get current system date and time
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set currdatetime=%%I
echo. %currdatetime%
echo. YYYYMMDDhhmmss.(milliseconds)(always 000)+/-(minutes difference to UTC) &echo.

REM Get individual variables for date from %currdatetime%
set _day=%currdatetime:~6,2%
set _month=%currdatetime:~4,2%
set _year=%currdatetime:~-0,4%

REM Display examples
echo. Day: %_day%, Month: %_month% and Year: %_year%
echo.
echo. USA Date: %_month%/%_day%/%_year% (mm-dd-yyyy)
echo. ENG Date: %_day%/%_month%/%_year% (dd-mm-yyyy)
echo.

REM Get individual variables for time from %currdatetime%
set _24hour=%currdatetime:~8,2%
set _min=%currdatetime:~10,2%
set _sec=%currdatetime:~12,2%

REM Display examples
echo.
echo. 24 hour time: %_24hour%:%_min%
echo. Hour: %_24hour%, Min: %_min% and Sec: %_sec%
echo.

REM Convert to 12 hour format
for /F "tokens=1,2,3 delims=:,. " %%A in ('echo %_24time%') do ( set /a "_12hour=100%%A%%100")

REM Add AM/PM
if %_12hour% geq 12 (
set _ampm=PM
set /a "_12hour-=12"
) else set "_ampm=AM"
if %_12hour% equ 0 set "_12hour=12"
if %_12hour% lss 10 set "_12hour=0%_12hour%"

REM Display examples
echo.
echo. 12 hour time: %_12hour%:%_min%%_ampm%
echo. Hour: %_12hour%, Min: %_min% and Sec: %_sec%
echo.

REM Greet user differently depending on the time
if "%_24hour%" geq "20" echo. It's %_12hour%:%_min%%_ampm%^! Bit late to be working isn't it %username%? &goto :done
if "%_24hour%" geq "18" echo. Good Evening %username%, the time is %_12hour%:%_min%%_ampm%. &goto :done
if "%_24hour%" geq "12" echo. Good Afternoon %username%, the time is %_12hour%:%_min%%_ampm%. &goto :done
if "%_24hour%" geq "06" echo. Good Morning %username%, the time is %_12hour%:%_min%%_ampm%. &goto :done
echo. It's %_12hour%:%_min%%_ampm%^! Bit late to be working isn't it %username%?
:done

endlocal
echo. Press any key to exit &>nul timeout /t -1 &exit /B