Batch File: Date conversion to a number

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...

Thanks for your help,
Rene
LVL 10
ReneGeAsked:
Who is Participating?
 
Bill PrewConnect With a Mentor Commented:
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

Open in new window

0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
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
 
ReneGeAuthor Commented:
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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
aburrConnect With a Mentor Commented:
fi you look here
http://en.wikipedia.org/wiki/Julian_day
you will find more information about calculating dates than you really wanted to know.
Basically the calculation is as leew stated but with a more conventional starting date.
0
 
Lee W, MVPConnect With a Mentor Technology and Business Process AdvisorCommented:
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
 
Ben Personick (Previously QCubed)Connect With a Mentor Lead Network EngineerCommented:

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
 
ReneGeAuthor Commented:
Thanks guys for your explanations.
Thanks bp for the working code.

Guys, you are my heros!!

Thanks and cheers,
Rene
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.