China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-31 13:26
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Determine the day of the week from the date View 2,411 Replies 21
Original Poster Posted 2007-02-06 03:11 ·  中国 湖南 长沙 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
For example, 20060110
How to determine what day of the week it is based on the above date?
Floor 2 Posted 2007-02-06 03:24 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Here's an idea: First, determine a date for which you already know what day of the week it is. Then calculate the difference in days between 20060110 and that date. Then divide the difference by 7 and take the remainder - the key point is calculating the difference between the two dates, which is transformed back to the classic old topic: What day of the week was N days before a certain date?
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 3 Posted 2007-02-06 05:22 ·  中国 湖南 长沙 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
I can calculate the date n days ago, but just can't calculate the day of the week.
For example, today is 20070205 Monday, the date n days ago is 20061210.
How to calculate it?
How to subtract 20061210 from 20070205?
Floor 4 Posted 2007-02-06 08:26 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
The moderator's statement is quite right. First of all, you need to have a reference object. The rest is to convert the date interval into decimal days, subtract 7 minus the reference week, and then divide by 7 to find the remainder.
Floor 5 Posted 2007-02-06 09:23 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Another alternative day-of-the-week query: )



  1. @ echo off && setlocal
  2. :: Redtek.2006 bbs.cn-dos.net Query what day of the week a certain year/month/day is



  3. set /p setDate=Please enter the date to query: (Format: year-month-day)


  4. :: Backup current date and set new date

  5. for /f %%i in ('date/t') do set "Current date backup=%%i"
  6. echo %setDate%|date >nul


  7. :: Display the queried date and restore the current date to the backup date

  8. echo. && echo The detailed information of the date you want to query is: %date% ] && echo.
  9. echo %Current date backup%|date >nul




  10. Pause
Redtek@sweetmeet.comPosted on: 2007-02-05 20:26



Note: Do not use this code on computers such as servers that are closely connected to the date.

  Because the implementation principle is to first back up the current date, then set the specified date, use the system %date% to get the day of the week, and then restore the date before the calculation.

  If some date-sensitive programs are installed on the computer, such as: JCreator pro for Java development and other sensitive software, if the date is changed to the past, even if it is changed back, it has become invalid.
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 6 Posted 2007-02-06 09:38 ·  中国 吉林 四平 联通
高级用户
★★★
Credits 859
Posts 413
Joined 2006-08-14 21:55
19-year member
UID 60532
Status Offline
Brother upstairs, just one word, "Like"
Floor 7 Posted 2007-02-06 14:39 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Using the newly learned VBS

@echo off
for %%n in (week1-日 week2-一 week3-二 week4-三 week5-四 week6-五 week7-六) do set Tmp$=%%n&&call set %%Tmp$:-==%%

:loop
cls
echo.
set input=
set /p input= Please enter the date (format: 2007/1/1):
if not defined input exit
echo.
>tmp.vbs echo wscript.echo weekday(#%input%#)
for /f "tokens=* delims=-" %%i in ('"cscript //nologo tmp.vbs"') do call echo Week %%week%%i%%
del tmp.vbs
echo.
pause
goto loop


Add one modified according to floor 9:

@echo off
for %%n in (week1-日 week2-一 week3-二 week4-三 week5-四 week6-五 week7-六) do set Tmp$=%%n&&call set %%Tmp$:-==%%

:loop
cls
echo.
set input=
set /p input= Please enter the date (format: 20070101):
if not defined input exit
echo.
>tmp.vbs echo wscript.echo Weekday(CDate("%input:~0,4%-%input:~4,2%-%input:~6,2%"))
for /f "tokens=* delims=-" %%i in ('"cscript //nologo tmp.vbs"') do call echo Week %%week%%i%%
del tmp.vbs
echo.
pause
goto loop


[ Last edited by zh159 on 2007-2-6 at 03:07 PM ]
Floor 8 Posted 2007-02-06 16:29 ·  中国 湖南 长沙 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
The method on the 5th floor is indeed good, but it seems a bit less perfect to change the system time. Do you all have any other good ideas?
Floor 9 Posted 2007-02-06 17:55 ·  中国 浙江 杭州 电信
初级用户
Credits 128
Posts 31
Joined 2006-10-23 08:05
19-year member
UID 67855
Gender Male
Status Offline
Why not use VBS?

<script language="vbscript">
<!--
MsgBox Weekday(CDate("2006-01-10")-1)
-->
</script>
Floor 10 Posted 2007-02-06 21:16 ·  中国 江苏 苏州 联通
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
I remember there was a formula in the Olympiad in Mathematics when I was a child, and I can check it again.
Floor 11 Posted 2007-02-08 16:40 ·  中国 湖南 长沙 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
The problem of getting the difference between two dates has been solved. Now the problem is how to determine the day of the week?
//For example, given Thursday, February 8, 2007, how to determine the day of the week 18 days ago?
That is, how to subtract 1 or 2 from the current day of the week...?

[ Last edited by 26933062 on 2007-2-8 at 03:47 AM ]
Floor 12 Posted 2007-02-08 22:01 ·  中国 吉林 四平 联通
高级用户
★★★
Credits 859
Posts 413
Joined 2006-08-14 21:55
19-year member
UID 60532
Status Offline
Give you a boost, it's getting more challenging
Floor 13 Posted 2007-02-09 05:23 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Originally posted by 26933062 at 2007-2-8 03:40:
The difference between two dates has been solved. Now the problem is how to determine the day of the week?
// For example, given 2007/02/08/Thursday, how to determine the day of the week 18 days ago?
That is, how to subtract 1 or 2 from the current day of the week..........?

...

@echo off
for %%n in (week1-日 week2-一 week3-二 week4-三 week5-四 week6-五 week7-六) do set Tmp$=%%n&&call set %%Tmp$:-==%%

:loop
cls
echo.
set input=
set week=+0
set /p input= Please enter the date (format: 20070101):
if not defined input exit
set /p week= Please enter the number of days interval (format: +8(-8), default is 0):
echo.
>tmp.vbs echo wscript.echo Weekday(CDate("%input:~0,4%-%input:~4,2%-%input:~6,2%")%week%)
for /f "tokens=* delims=-" %%i in ('"cscript //nologo tmp.vbs"') do call echo Day of the week%%week%%i%%
del tmp.vbs
echo.
pause
goto loop
Floor 14 Posted 2007-02-09 06:00 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
MsgBox WeekdayName(Weekday(CDate(Now)))
Floor 15 Posted 2007-02-09 06:05 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Borrowing from the above:
MsgBox WeekdayName(Weekday(CDate(Now)-18))

+ - number of days

@echo off
:loop
cls
echo.
set input=
set week=+0
set /p input= Please enter the date (format: 20070101):
if not defined input exit
set /p week= Please enter the number of days to interval (format: +8(-8), default is 0):
echo.
>tmp.vbs echo wscript.echo WeekdayName(Weekday(CDate("%input:~0,4%-%input:~4,2%-%input:~6,2%")%week%))
for /f %%i in ('"cscript //nologo tmp.vbs"') do call echo %%i
del tmp.vbs
echo.
pause
goto loop


[ Last edited by zh159 on 2007-2-8 at 06:42 PM ]
Forum Jump: