根据一天中的不同时间对用户进行不同的问候



我找到了以下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,则问候应该是"0">早上好%username%";

如果时间是12:00到17:59,则问候应该是"12:00">下午好%username%";

如果时间是18:00到19:59,则问候应该是"0">晚安%username%";

如果时间是20:00到05:59,则问候应该是"0">你不应该躺在床上%username%吗";

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

非常感谢,

Bri

无需使用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格式更好处理(
  • Q: ";if语句后面额外的echo Shouldn't you be in bed %username%是用来做什么的?-A: 以处理午夜(00:00(到05:59之间的时间。从逻辑上讲,它将是if "tim" geq "00" ...,但这已经过时了
  • Q: ";为什么使用set "tim=%time: =0%"设置tim变量?-A: 不要乱用%time%变量。设置它会禁用它显示实际时间的能力,所以我们只使用另一个变量名。set命令将字符串中的每个空格替换为零(使用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

最新更新