How do I state that a variable is a date?
Moderator: DosItHelp
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
How do I state that a variable is a date?
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?
Re: How do I state that a variable is a date?
Batch doesn't have a "Date2Number" function, so you have to write your own.
Re: How do I state that a variable is a date?
VBS has a good feature for that.
I'm not sure what date range it works to in history.
I'm not sure what date range it works to in history.
Re: How do I state that a variable is a date?
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
Re: How do I state that a variable is a date?
The Batch subroutine below convert a date in yyyymmdd format into a Julian Day Number, that is a sequential number of day:
The Batch subroutine below achieve the opposite conversion:
For example, to subtract 14 days from 1944/12/13:
You may also get the Day Of Week (0=Sunday ... 6=Saturday) with this formula: set /A DOW=(JDN+2) %% 7; for example:
Reference: Julian Day Numbers
Antonio
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
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: How do I state that a variable is a date?
Very nice Antonion, very nice. Thank you.
Re: How do I state that a variable is a date?
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
Re: How do I state that a variable is a date?
Oops, you are right! The adjustment for Day Of Week is +1, not +2!