Thanks, but I need this script to work on any version of Windows no matter how the %date% is formatted. Hence the point of the first section of code where it analyises %date% and calculates the day name.
Main Topics
Browse All TopicsHi,
I got the following script from SteveGTR here. I believe since 2006 it hasn't been working out the day name right, like for today it outputs it as Monday. I really need this script to be future proof and work for years to come! Please help!
==========================
@echo off
cls
setlocal ENABLEDELAYEDEXPANSION
set yyyy=
set $tok=1-3
for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u
if "%$d1:~0,1%" GTR "9" set $tok=2-4
for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (
for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (
set %%x=%%u
set %%y=%%v
set %%z=%%w
set $d1=
set $tok=))
if "%yyyy%"=="" set yyyy=%yy%
if /I %yyyy% LSS 100 set /A yyyy=2000 + 1%yyyy% - 100
set /A dd=1%dd% - 100
set /A mm=1%mm% - 100
set /A dayNum=%dd% + (2 * %mm%) + (3 * (%mm% + 1) / 5) + %yyyy% + (%yyyy% / 4) - (%yyyy% / 100) + (%yyyy% / 400) + 1
set /A dayNum=%dayNum% %% 7
if /i %dd%==01 goto skipdd
if /i %dd%==02 goto skipdd
if /i %dd%==03 goto skipdd
if /i %dd%==04 goto skipdd
if /i %dd%==05 goto skipdd
if /i %dd%==06 goto skipdd
if /i %dd%==07 goto skipdd
if /i %dd%==08 goto skipdd
if /i %dd%==09 goto skipdd
if /i %dd% LSS 10 set dd=0%dd%
:skipdd
if /i %mm%==01 goto skipmm
if /i %mm%==02 goto skipmm
if /i %mm%==03 goto skipmm
if /i %mm%==04 goto skipmm
if /i %mm%==05 goto skipmm
if /i %mm%==06 goto skipmm
if /i %mm%==07 goto skipmm
if /i %mm%==08 goto skipmm
if /i %mm%==09 goto skipmm
if /i %mm% LSS 10 set mm=0%mm%
:skipmm
set day=%dd%
set month=%mm%
set year=%yyyy%
set yy=%yyyy:~2,2%
if %daynum%==1 set daynamefull=Monday& set dayname=Mon
if %daynum%==2 set daynamefull=Tuesday& set dayname=Tue
if %daynum%==3 set daynamefull=Wednesday& set dayname=Wed
if %daynum%==4 set daynamefull=Thursday& set dayname=Thu
if %daynum%==5 set daynamefull=Friday& set dayname=Fri
if %daynum%==6 set daynamefull=Saturday& set dayname=Sat
if %daynum%==0 set daynamefull=Sunday& set dayname=Sun
if %month%==01 set monthname=January& set shortmonthname=Jan
if %month%==02 set monthname=February& set shortmonthname=Feb
if %month%==03 set monthname=March& set shortmonthname=Mar
if %month%==04 set monthname=April& set shortmonthname=Apr
if %month%==05 set monthname=May& set shortmonthname=May
if %month%==06 set monthname=June& set shortmonthname=Jun
if %month%==07 set monthname=July& set shortmonthname=Jul
if %month%==08 set monthname=August& set shortmonthname=Aug
if %month%==09 set monthname=September& set shortmonthname=Sep
if %month%==10 set monthname=October& set shortmonthname=Oct
if %month%==11 set monthname=November& set shortmonthname=Nov
if %month%==12 set monthname=December& set shortmonthname=Dec
echo %daynamefull% %day% %monthname% %year% %time:~0,8%
pause
==========================
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Did you implement my update correctly? Here's the first part of your code with the change:
@echo off
REM cls
REM setlocal ENABLEDELAYEDEXPANSION
set yyyy=
set $tok=1-3
for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u
if "%$d1:~0,1%" GTR "9" set $tok=2-4
for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (
for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (
set %%x=%%u
set %%y=%%v
set %%z=%%w
set $d1=
set $tok=))
if "%yyyy%"=="" set yyyy=%yy%
if /I %yyyy% LSS 100 set /A yyyy=2000 + 1%yyyy% - 100
set /A dd=1%dd% - 100
set /A mm=1%mm% - 100
if /I %mm% LSS 3 set /a mm+=12&set /a yyyy-=1
set /A dayNum=%dd% + (2 * %mm%) + (3 * (%mm% + 1) / 5) + %yyyy% + (%yyyy% / 4) - (%yyyy% / 100) + (%yyyy% / 400) + 1
set /A dayNum=%dayNum% %% 7
echo dayNum=%dayNum%
Today, it displays the correct number 4.
Well that doesn't work here...
I get:
-------------
dayNum=5
03/02/2006
2005
3
14
-------------
From running with the variables echo'ed:
--------------------------
@echo off
REM cls
REM setlocal ENABLEDELAYEDEXPANSION
set yyyy=
set $tok=1-3
for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u
if "%$d1:~0,1%" GTR "9" set $tok=2-4
for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (
for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (
set %%x=%%u
set %%y=%%v
set %%z=%%w
set $d1=
set $tok=))
if "%yyyy%"=="" set yyyy=%yy%
if /I %yyyy% LSS 100 set /A yyyy=2000 + 1%yyyy% - 100
set /A dd=1%dd% - 100
set /A mm=1%mm% - 100
if /I %mm% LSS 3 set /a mm+=12&set /a yyyy-=1
set /A dayNum=%dd% + (2 * %mm%) + (3 * (%mm% + 1) / 5) + %yyyy% + (%yyyy% / 4) - (%yyyy% / 100) + (%yyyy% / 400) + 1
set /A dayNum=%dayNum% %% 7
echo dayNum=%dayNum%
echo %date%
echo %yyyy%
echo %dd%
echo %mm%
--------------------------
Yes, I see. We'll have to preserve the mm and yyyy variable so you can use them later.
@echo off
set yyyy=
set $tok=1-3
for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u
if "%$d1:~0,1%" GTR "9" set $tok=2-4
for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (
for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (
set %%x=%%u
set %%y=%%v
set %%z=%%w
set $d1=
set $tok=))
if "%yyyy%"=="" set yyyy=%yy%
if /I %yyyy% LSS 100 set /A yyyy=2000 + 1%yyyy% - 100
set /A dd=1%dd% - 100
set /A mm=1%mm% - 100
set wmm=%mm%
set wyyyy=%yyyy%
if /I %wmm% LSS 3 set /a wmm+=12&set /a wyyyy-=1
set /A dayNum=%dd% + (2 * %wmm%) + (3 * (%wmm% + 1) / 5) + %wyyyy% + (%wyyyy% / 4) - (%wyyyy% / 100) + (%wyyyy% / 400) + 1
set /A dayNum=%dayNum% %% 7
echo dayNum=%dayNum%
echo Date: %date%
echo Year: %yyyy%
echo Day: %dd%
echo Month: %mm%
Remember that we do this here for free... You don't pay me anything to do your work. So stop complaining and take some responsibility for the code. I see you've answered some MS-DOS questions so you are no novice. Time to start learning as we all did here and stop whinning. Sorry for being blunt, but my hope is that you'll become a better DOS programmer if you just do a little of the work.
"future proof" for days on "every year possible" indicates knowing the code for leap years ( i don't have it memorized but something like every four years except not every 100 except not every 400 except . . . ) - the bottom line is a true year is not nothing near a neat and pretty division of days, and some group of people long ago thought this type of adjustment sequence was better than setting the clocks back 6 hours X minutes and X seconds every year. That would require adjustingworking hours every year but never worry about this being a leap year again. having handled that little piece we could also re-lable Jan 1st as Monday each year as well.
oops, I digress again, sry, kinda.
My point is, several programs have solved this already, including vbscript, and the vbscript version is not too dificult to pull into DOS batch with a small learning curve.
(For learning DOS batch, you're better off doing the whole thing with addition and subtraction in set commands and understanding the logic in each line, never forgetting numbers that start with 0 are NOT decimal, etc. this wheele rewriting helps keep a valuable skill honed for other tasks that have not been pre-solved.)
Anywho, lemme stop babbleing, poke around a few of my scripts that use vb to get date, and mod one to help a bit . . .
This was easy, and the vb portion is minimal and understandable, but i don't like this version.
It's kludgy and puts the conversion job in batch instead of VB
I didn't even finish this, you have to copy the two lines from the weekday conversion to the month setting and change "1 Monday" to "1 January" and so forth.
I include it as an alternative for those who like mostly DOS, while i prefer to rewrite a version that uses the VB conversions that are faster, and also allow DOS to call the script only once. Your virus checkers will like that better. (this version took a second or two on my machine and i feel that's the time to scan the temporary vbscript as it started up 7 times)
so, you play with this or wait a bit for my second attempt:
:: dateas.bat
:: return date as day, date, month, etc
:: this is an re-usable example, not a finished product
:: i've prepended all my variables with t to help prevent conflicts with yours
::
:: you must skip to the "start" line, delete what you don't need and
:: re-adjust what you do need to fit your program.
::
@echo off
setlocal
:: setup - done once at the top
set tscript=%temp%\%~n0.vbs
set tget=cscript -nologo "%tscript%"
set tbatch=%temp%\%~n0_A.bat
:: write the vbscript to get time and difference - XP/2K/NT/9x all the same
>"%tscript%" echo sInterval = wscript.arguments^(0^)
>>"%tscript%" echo wscript.echo DatePart^(sInterval, now^)
:: time setup complete, include your own job setups here
:: subroutines here
goto :start
:setconversions
if "%1"=="%2" set thisis=%3
goto :EOF
:: end of subroutines
:start
for /f %%K in ('%tget% w') do set daynamefull=%%K
::convert 1 to Sunday, etc
for %%K in ("1 Sunday" "2 Monday" "3 Tuesday" "4 Wednesday" "5 Thursday" "6 Friday" "7 Saturday") do call :setconversions %daynamefull% %%~K
set daynamefull=%thisis%
for /f %%K in ('%tget% d') do set day=%%K
for /f %%K in ('%tget% m') do set monthname=%%K
::convert month 1 to Jan, etc
for /f %%K in ('%tget% yyyy') do set year=%%K
for /f %%K in ('%tget% h') do set hour=%%K
for /f %%K in ('%tget% m') do set minute=%%K
for /f %%K in ('%tget% s') do set second=%%K
echo %daynamefull% %day% %monthname% %year% %hour%:%minute%:%second%
pause
as i thought, this is MUCH cleaner, allowing the vb functions to do the convert, even though it makes the vb part longer, and requires DOS to take in a specific format.
Note that a one line vbscript (wscript.echo now) would also give DOS a date and time to work with, EXCEPT: Like the built in DOS dates and times, the regional registry and "Shortdate format" changeable in the control panel effect the output format of the now function making it different for different users on the same machine, so we're back again to string compares and gymnastics to straighten it out. by streatching the vbscript out long enough to force a desired format, the DOS part becomes also much simpler to use.
Enjoy (\o/)
:: dateas.bat
:: return date as day, date, month, etc
:: this is an re-usable example
:: i've prepended all my variables with t to help prevent conflicts with yours
::
:: you must skip to the "start" line, delete what you don't need and
:: re-adjust what you do need to fit your program.
::
@echo off
setlocal
:: setup - done once at the top
set tscript=%temp%\%~n0.vbs
set tget=cscript -nologo "%tscript%"
set tbatch=%temp%\%~n0_A.bat
:: write the vbscript to get time and difference - XP/2K/NT/9x all the same
>"%tscript%" echo dTemp = now
>>"%tscript%" echo wscript.echo weekdayname ^( weekday ^( dTemp ^) ^) _
>>"%tscript%" echo ^& " " ^& day ^( dtemp ^) _
>>"%tscript%" echo ^& " " ^& monthname ^( month ^( dtemp ^) ^) _
>>"%tscript%" echo ^& " " ^& year ^( dtemp ^) _
>>"%tscript%" echo ^& " " ^& hour ^( dtemp ^) _
>>"%tscript%" echo ^& " " ^& minute ^( dtemp ^) _
>>"%tscript%" echo ^& " " ^& second ^( dtemp ^)
:: time setup complete, include your own job setups here
:start
for /f "tokens=1-7" %%K in ('%tget% weekday') do (
set daynamefull=%%K
set day=%%L
set monthname=%%M
set year=%%N
set hour=%%O
set minute=%%P
set second=%%Q
)
echo %daynamefull% %day% %monthname% %year% %hour%:%minute%:%second%
pause
ERROR in semantics !
Please change this one comment line:
:: write the vbscript to get time and difference - XP/2K/NT/9x all the same
to this one line instead:
:: write the vbscript to get time in a specific format
This above does NOT work in Windows 9x.
This above does NOT calculate differences.
Sorry, The first 5 lines of setup, I copied from a longer script to re-use the auto-nameing of temp files from another utility I wrote. I did not include here the DOS SET commands into the vbscript that would allow you to call it with Windows 9x redirected to a temp and then run the temp to set the variables. Windows 9x does not understand FOR /F so this script fails there.
thanks (\o/),
2K
K_2K, well, I've tested both of your scripts and they work nicely. I'm a litle confused with a few things, wonder if you can help....
Script1 and 2 both output basically the same info, what is really the difference between them? Both of them generate a VB script to do the job?
Are you saying that Script 2 is now fool proof and works everything out from whatever the date format is set in control panel? ie. it will work with any format of day/date and any year (even leap years)?
Can you explain Script 2 a little bit... I see that it creates the vb script to put the date info, but where it run it?
Is that this line: set tget=cscript -nologo "%tscript%"
And whats this line for?:
set tbatch=%temp%\%~n0_A.bat
I don't see it create that bat file ?!
Cool stuff though, thanks :)
script one allows DOS to send the VBscript a parameter and get back different info - so all the outputs datepart show are available without changing the vbscript prt.
You could save the temp file it makes to a utiliity name and reuse it across several batch files without re-writing the vbs every time. That one does gives you months 1-12 unless you added the DOS to convert that, and runs MUCH slower on machines that stop the subscript to virus check it each time - it's restarting vb for each item DOS asks for.
For Script two, if you want to get the week of the year, or the Julian date or something, you have to modify the vbscipt part in setup. The vbs part only runs once and spits out a specific line coded the way you need it, letting DOS set each word to a part. I like it much better cause I can easily change the vb part to give instead the final result I want to use in DOS, use "delims=" in the FOR /F, and only one DOS variable for the date. Those who do not want to mess with vbs would probably prefer the first, longer, bulkier code.
<<<tbatch
so right you are, thanks for noticing - it too was the second temporary batch file i used in my windows 98 version. You can delete that line, OR for windows 98 users, modify the vbs to output a line that says "set dosdate= weekdayname ... " then redirect it to file "%tbatch%" then CALL "%tbatch%" to actually set the variable.
Business Accounts
Answer for Membership
by: dragon-itPosted on 2006-02-02 at 13:48:33ID: 15857643
Try typing echo %date% from a Command Prompt. For me it always returns Thu 02/02/2006 on Windows 2000 though I think you don't get the Thu bit on XP?
If the OS you are running this on gives you what you need already and youknow it isn't going to change it's output or language etc. you could do away with some of the code, i.e.
REM This is based on the %date% command returning ddd dd/mm/yyyy
REM If yours returns mm/dd/yyyy then swap over the day and month lines
set dayname=%date:~0,3%
set day=%date:~4,2%
set month=%date:~7,2%
set year=%date:~10,4%
set yy=%date:~12,2%
if %dayname%==Mon set daynamefull=Monday
if %dayname%==Tue set daynamefull=Tuesday
if %dayname%==Wed set daynamefull=Wednesday
if %dayname%==Thu set daynamefull=Thursday
if %dayname%==Fri set daynamefull=Friday
if %dayname%==Sat set daynamefull=Saturday
if %dayname%==Sun set daynamefull=Sunday
if %month%==01 set monthname=January& set shortmonthname=Jan
if %month%==02 set monthname=February& set shortmonthname=Feb
if %month%==03 set monthname=March& set shortmonthname=Mar
if %month%==04 set monthname=April& set shortmonthname=Apr
if %month%==05 set monthname=May& set shortmonthname=May
if %month%==06 set monthname=June& set shortmonthname=Jun
if %month%==07 set monthname=July& set shortmonthname=Jul
if %month%==08 set monthname=August& set shortmonthname=Aug
if %month%==09 set monthname=September& set shortmonthname=Sep
if %month%==10 set monthname=October& set shortmonthname=Oct
if %month%==11 set monthname=November& set shortmonthname=Nov
if %month%==12 set monthname=December& set shortmonthname=Dec
echo %daynamefull% %day% %monthname% %year%
echo %dayname% %day% %shortmonthname% %yy%