Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Batch File: Date conversion to a number

Posted on 2010-09-02
7
Medium Priority
?
2,235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 57

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

715 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