JSI Tip 7047. How do I convert a date to the day number and day name?

I have scripted Day.bat to calculate the day number and day name.

The syntax for using Day.bat is:

call day yyyy mm dd daynumb daytext

where yyyy is the 4 digit year, mm is a 2 digit month, and dd is a 2 digit day of the month. Daynumb is a call directed environment variable that will contain the day number, where Monday is 1 and Sunday is 7. Daytext is a call directed environment variable that will contain the day name (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, or Sunday).

NOTE: This script was modified on 24-Aug-2003 to fix a century problem.

Daybat contains:

@echo off
if \{%5\}==\{\} @echo Syntax: Call day yyyy mm dd daynumb daytext&goto :EOF
set montabl=0033614625035
set monleap=0623614625035
set daytab=7Sunday###1Monday###2Tuesday##3Wednesday4Thursday#5Friday###6Saturday#
set /a century=1
set /a century4=1
set /a yyyy=%1
set /a mm=100%2%%100
set /a dd=100%3%%100
set /a leap=%yyyy%%%4
if %leap% EQU 0  set /a century=%yyyy%%%100
if %century% EQU 0 set /a century4=%yyyy%%%400
if %leap% EQU 0 set montabl=%monleap%
set /a day=%yyyy% + (%yyyy% / 4)
set /a day=%day% - (%yyyy% / 100)
set /a day=%day% + (%yyyy% / 400)
set /a day=%day% + %dd%
call set /a mon=%%montabl:~%mm%^,1%%
set /a day=%day% + %mon% - 1
set /a day=%day%%%7
set /a day=%day% * 10
if %century% EQU 0 if %century4% NEQ 0 if %mm% LSS 3 set daytab=1Monday###2Tuesday##3Wednesday4Thursday#5Friday###6Saturday#7Sunday###
call set w5=%%daytab:~%day%^,10%%
set /a daynumb=%w5:~0,1%
set daytext=%w5:~1,9%
set daytext=%daytext:#=%
endlocal&set %4=%daynumb%&set %5=%daytext%
NOTE: See What is the ISO week number and day for a given date?

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.