Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Batch File: Date conversion to a number

Posted on 2010-09-02
7
Medium Priority
?
2,316 Views
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,
Rene
0
Comment
Question by:ReneGe
7 Comments
 
LVL 96

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.
0
 
LVL 10

Author Comment

by:ReneGe
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

by:aburr
aburr earned 400 total points
ID: 33593607
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 96

Assisted Solution

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

Accepted Solution

by:
Bill Prew earned 800 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

Open in new window

0
 
LVL 14

Assisted Solution

by:Ben Personick (Previously QCubed)
Ben Personick (Previously QCubed) earned 400 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

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

Guys, you are my heros!!

Thanks and cheers,
Rene
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

When we purchase storage, we typically are advertised storage of 500GB, 1TB, 2TB and so on. However, when you actually install it into your computer, your 500GB HDD will actually show up as 465GB. Why? It has to do with the way people and computers…
Aerodynamic noise is the cause of the majority of the noise produced by helicopters. The inordinate amount of noise helicopters produce is a major problem in the both a military and civilian setting. To remedy this problem the use of an aerogel coat…
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 …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

926 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