How do I state that a variable is a date?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

How do I state that a variable is a date?

#1 Post by pditty8811 » 23 Mar 2013 23:41

I have variable var5. This could equal 19441213 or 19400815, ( dates during ww2) but the format will always be yyyymmdd. How do I state that this variable is a date, in that format, so when I subtract days from the variable I won't end up with a value like: 19441199 or 19409110?

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: How do I state that a variable is a date?

#2 Post by mfm4aa » 23 Mar 2013 23:51

Batch doesn't have a "Date2Number" function, so you have to write your own.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How do I state that a variable is a date?

#3 Post by foxidrive » 24 Mar 2013 00:17

VBS has a good feature for that.
I'm not sure what date range it works to in history.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How do I state that a variable is a date?

#4 Post by foxidrive » 24 Mar 2013 00:49

Here, this works.

Code: Select all

:: Date foward & backward
@echo off
setlocal
if "%~2"=="" (
echo to get todays date use         call "%~n0" today 0
echo to get yesterdays date use     call "%~n0" today -1
echo to get 25 days before 19441213 call "%~n0" 1944/12/13 -25
echo to get 1250 days in the future call "%~n0" today +1250
goto :EOF)

set date1=%1
set qty=%2
if /i "%date1%" EQU "TODAY" (set date1=now) else (set date1="%date1%")
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)
for /f %%a in ('cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set day=%result:~0,4%-%result:~4,2%-%result:~6,2%
echo %%day%% is set to "%day%" (without the quotes)
pause

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

Re: How do I state that a variable is a date?

#5 Post by Aacini » 24 Mar 2013 12:44

The Batch subroutine below convert a date in yyyymmdd format into a Julian Day Number, that is a sequential number of day:

Code: Select all

rem Convert the date to Julian Day Number

:DateToJDN yyyymmdd jdn=
setlocal
set date=%1
set /A yy=%date:~0,4%, mm=1%date:~4,2% %% 100, dd=1%date:~6% %% 100
set /A a=mm-14, jdn=(1461*(yy+4800+a/12))/4+(367*(mm-2-12*(a/12)))/12-(3*((yy+4900+a/12)/100))/4+dd-32075
endlocal & set %2=%jdn%
exit /B

The Batch subroutine below achieve the opposite conversion:

Code: Select all

rem Convert Julian Day Number back to date

:JDNToDate jdn yyyymmdd=
setlocal
set /A l=%1+68569,n=(4*l)/146097,l=l-(146097*n+3)/4,i=(4000*(l+1))/1461001,l=l-(1461*i)/4+31,j=(80*l)/2447,dd=l-(2447*j)/80,l=j/11,mm=j+2-(12*l),yy=100*(n-49)+i+l
if %dd% lss 10 set dd=0%dd%
if %mm% lss 10 set mm=0%mm%
endlocal & set %2=%yy%%mm%%dd%
exit /B

For example, to subtract 14 days from 1944/12/13:

Code: Select all

call :DateToJDN 19441213 JDN=
set /A JDN-=14
call :JDNToDate %JDN% dateBefore=
echo %dateBefore%

19441129

You may also get the Day Of Week (0=Sunday ... 6=Saturday) with this formula: set /A DOW=(JDN+2) %% 7; for example:

Code: Select all

setlocal EnableDelayedExpansion
set day=0
for %%d in (Sunday Monday Tuesday Wednesday Thursday Friday Saturday) do (
   set DayOfWeek[!day!]=%%d
   set /A day+=1
)
call :DateToJDN 19441213 JDN=
set /A DOW=(JDN+2) %% 7
echo The date 1944/12/13 was !DayOfWeek[%DOW%]!

The date 1944/12/13 was Thursday

Reference: Julian Day Numbers

Antonio

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: How do I state that a variable is a date?

#6 Post by pditty8811 » 24 Mar 2013 13:09

Very nice Antonion, very nice. Thank you.

mfm4aa
Posts: 70
Joined: 13 Feb 2013 14:02
Location: Europe

Re: How do I state that a variable is a date?

#7 Post by mfm4aa » 24 Mar 2013 13:28

Very helpful, thank you. But shoudn't it be:

Code: Select all

set /A DOW=(JDN+1) %% 7
:?:

The date 2013/03/24 was Monday

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

Re: How do I state that a variable is a date?

#8 Post by Aacini » 25 Mar 2013 05:17

Oops, you are right! The adjustment for Day Of Week is +1, not +2!

Post Reply