day of week calculation of any date

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

day of week calculation of any date

#1 Post by MKANET » 13 Apr 2012 22:27

I tried doing searches on google; but all the link I found were based on the current date; which is really easy. I also found an old post on here pointing to a chinese site. Anyway...

I'm trying to figure out a way to get the day of week based on a date with the format: xx/xx/xxxx

So, if %date%=04/27/2012, it will set day=Fri

Whatever the solution, ideally, it should work under Vista, 7, Win2K3, Win2K8

I found an executable that does this; however, I would much prefer to do it without an extra executable.

Thanks as always!

Aacini
Expert
Posts: 1887
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: day of week calculation of any date

#2 Post by Aacini » 14 Apr 2012 00:32

Code: Select all

@ECHO OFF
REM GET MONTH, DAY, YEAR VALUES
FOR /F "TOKENS=1-3 DELIMS=/" %%A IN ("%1") DO SET MM=%%A& SET DD=%%B& SET YY=%%C
REM ELIMINATE LEFT ZEROS
SET /A DD=10%DD% %% 100, MM=10%MM% %% 100
REM CALCULATE JULIAN DAY NUMBER AND DAY OF WEEK
IF %MM% LSS 3 SET /A MM+=12, YY-=1
SET /A A=YY/100, B=A/4, C=2-A+B, E=36525*(YY+4716)/100, F=306*(MM+1)/10, JDN=C+DD+E+F-1523, DOW=JDN %% 7 + 1
ECHO Number of day of week: %DOW%
REM CONVERT DOW NUMBER TO DOW NAME
FOR /F "TOKENS=%DOW%" %%D IN ("Sun Mon Tue Wed Thu Fri Sat") DO ECHO Day of week: %%D

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: day of week calculation of any date

#3 Post by MKANET » 14 Apr 2012 13:34

That worked perfectly! Thanks so much!

Post Reply