Batch File: Date conversion to a number

Posted on 2010-09-02
Last Modified: 2012-08-13
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,
Question by:ReneGe
LVL 95

Expert Comment

by:Lee W, MVP
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.
LVL 10

Author Comment

ID: 33593600

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?

LVL 27

Assisted Solution

aburr earned 100 total points
ID: 33593607
fi you look here
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.
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

LVL 95

Assisted Solution

by:Lee W, MVP
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

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).
LVL 52

Accepted Solution

Bill Prew earned 200 total points
ID: 33595372

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.

: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"
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
  if "%~1" NEQ "" (
    set %~1=%JD%
  ) else (
exit /b

Open in new window

LVL 11

Assisted Solution

by:Ben Personick
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.
LVL 10

Author Comment

ID: 33597047
Thanks guys for your explanations.
Thanks bp for the working code.

Guys, you are my heros!!

Thanks and cheers,

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
This is about my first experience with programming Arduino.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now