Solved

# Batch File: Date conversion to a number

Posted on 2010-09-02
1,866 Views
Hi there,

I need to make a batch file that will convert %date% into a number.

If you know nothing about batch files but know how to make the formula... Please share you knowledge.

For example:
2010-09-03 = 40424
2010-09-04 = 40425
and so on...

Rene
0
Question by:ReneGe

LVL 95

Expert Comment

ID: 33593563
Do you mean a number as in the number of days since 1900?  If that's the base number you want, just do the math - you can use SET /A for calculations... it's a huge PITA, but you're welcome to do it, obviously.

For example:
current year-1900 = years elapsed
Years elapsed * 365.25
Then add the days for each month past
Now you have the number of days.
0

LVL 10

Author Comment

ID: 33593600
leew,

Is there a formula for calculatiing "The days for each month past"? And what about leap years?

Do you have any batch file script you can share?

Thanks,
Rene
0

LVL 27

Assisted Solution

aburr earned 100 total points
ID: 33593607
fi you look here
http://en.wikipedia.org/wiki/Julian_day
Basically the calculation is as leew stated but with a more conventional starting date.
0

LVL 95

Assisted Solution

Lee W, MVP earned 100 total points
ID: 33593691
Frankly, I wouldn't convert dates into numbers like that - if I wanted numerically ordered dates, I'd just reformat the date into YYYYMMDD

Days in months is easy -
If month = 1 then Days = Day of the month
IF month = 2 then days = 31 + day of the month
If month = 3 then days = 59 (Jan. + feb) + the day of the month
etc.

Since leap years are every 4 years, divide the elapsed number of years by 4 and that's the number of leap days.  (-1 if the current month is January or february and the remainder of that division is 0).
0

LVL 51

Accepted Solution

Bill Prew earned 200 total points
ID: 33595372
Rene,

Can you better define what you are trying to accomplish?  As LEEW mentions, if you just want sortability then using YYYYMMDD is often enough.  But if you want to be able to do some date math, like find the number of days between two dates, then I convert to Julian.  Attached are two versions of a conversion routine I have used.  One assumes the date passed to it is in YYYYMMDD format, the other accepts a slightly more flexible date string and tries to intelligently parse it.

~bp
``````:jdate return-variable date-string(YYYYMMDD)
set DateStr=%~2
set yy=%DateStr:~0,4%
set /A mm=1%DateStr:~4,2%-100
set /A dd=1%DateStr:~6,2%-100
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
set /a %~1=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
exit /b

:jdate JD DateStr
REM converts a date string to julian day number with respect to regional date format
REM JD      [out,opt] - julian days
REM DateStr [in,opt]  - date string, e.g. "03/31/2006" or "Fri 03/31/2006" or "31.3.2006"
setlocal
set DateStr=%~2
if "%~2"=="" set DateStr=%date%
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
for /f "tokens=1-3 delims=/.- " %%A in ("%DateStr:* =%") do (
set %%a=%%A
set %%b=%%B
set %%c=%%C
)
)
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
(
endlocal
if "%~1" NEQ "" (
set %~1=%JD%
) else (
echo.%JD%
)
)
exit /b
``````
0

LVL 11

Assisted Solution

Ben Personick earned 100 total points
ID: 33596464

As Leew mentions YYYYMMDD (The ISO Standard) is the best method for sorting and comparing dates.  But if you really need to know Days, you do have to break the date down.  I only usually get close, ignoring leapyears because the leap-year rule is highly complex (Leap years are calculated Something like: Every four years, except those divisible by 20, unless divisible by 100) and also ignoring the gregorian reformation, becuase most dates I work with are in the 20th or 21st century and being off by a hand full of days wouldn't kill me.

Months and days are easy

30 days hath September, April, June, and November, all the rest have 31, except February with 28, and in leap-year 29.

Iactually code to set the day to the 28th if it's Feb 29th.
0

LVL 10

Author Comment

ID: 33597047
Thanks bp for the working code.

Guys, you are my heros!!

Thanks and cheers,
Rene
0

## Featured Post

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …