Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Convert date+time to double integer & back

Posted on 2003-02-26
13
Medium Priority
?
583 Views
Last Modified: 2007-12-19
Hi,

I would like to convert a date + time (i.e. 25/02/2003 10:53:50) into a double int and be able from this double int to recompose the date.

Is there any function that does this job ?

Thanks,

Laurent.
0
Comment
Question by:lthiry
13 Comments
 
LVL 4

Expert Comment

by:JNSTAUB
ID: 8024054
yes , they are subroutines to convert the gregorain date to julian date a real number (integer at 12:00 GMT), then you can perform any computations as local time convertion and recompute from the julian number (24xxxxx,yyyyyy) a gregorian date and time. I have these subroutines in archiv on floppy disks , if you don't find them before on the web ,i may post them next monday.
regards
0
 
LVL 4

Expert Comment

by:JNSTAUB
ID: 8024206
i found these subroutines on the bureau des longitudes  web site, it's a french gouvernemental organization and french time keeper!so the subroutine should be very accurate i hope!

<HTML>                                                          
<HEAD>
<title>JavaScript - Julian Day</title>

<SCRIPT LANGUAGE="JavaScript">
<!-- hide this script tag's contents from old browsers
w = self;
browserName = navigator.appName;
browserVer = parseInt(navigator.appVersion);
version = "n1" ;
if (browserName == "Netscape" && browserVer >= 3) version = "ne";
if (browserName == "Microsoft Internet Explorer") version = "ie";

function compute(form) {
    MM=(form.nmonth.value=="")? "0" : eval(form.nmonth.value);
    DD=(form.nday.value=="")? "0": eval(form.nday.value);
    YY=(form.nyear.value=="") ? "0" :eval(form.nyear.value);
    HR=(form.nhour.value=="")? "0" :eval(form.nhour.value);
    MN=(form.nminute.value=="") ? "0" :eval(form.nminute.value);
    SS=(form.nsecondes.value=="") ? "0" : eval(form.nsecondes.value);
    with (Math) {  
      HR = HR + (MN / 60) + (SS / 3600);
      GGG = 1;
      if( YY < 1582 ) GGG = 0;
      if( YY <= 1582 && MM < 10 ) GGG = 0;
      if( YY <= 1582 && MM == 10 && DD < 5 ) GGG = 0;
      JD = -1 * floor(7 * (floor((MM + 9) / 12) + YY) / 4);
      S = 1;
      if ((MM - 9)<0) S=-1;
      A = abs(MM - 9);
      J1 = floor(YY + S * floor(A / 7));
      J1 = -1 * floor((floor(J1 / 100) + 1) * 3 / 4);
      JD = JD + floor(275 * MM / 9) + DD + (GGG * J1);
      JD = JD + 1721027 + 2 * GGG + 367 * YY - 0.5;
      JD = JD + (HR / 24);
    }
    form.result.value = JD;
}
function reverse(form) {
   JD=eval(form.result.value)
   with (Math) {  
   Z = floor(JD+0.5);
   F = JD+0.5 - Z;
   if (Z < 2299161) {
      A = Z
      } else
      {I = floor((Z - 1867216.25)/36524.25);
      A = Z + 1 + I - floor(I/4);
    }
   B = A + 1524;
   C = floor((B - 122.1)/365.25);
   D = floor(365.25 * C);
   T = floor((B - D)/ 30.6001);
   RJ = B - D - floor(30.6001 * T) + F;
   JJ = floor(RJ);
   RH = (RJ - floor(RJ)) * 24;
   Heure=floor(RH);
   Mn = floor((RH - Heure )*60);
   Sec = ((RH - Heure )*60 - Mn )*60;
   if (T < 14) {
      MM = T - 1
   } else
   { if ((T == 14) || (T == 15))  MM = T - 13
   }
   if (MM > 2) {
      AA = C - 4716 } else
   { if ((MM == 1) || (MM == 2)) AA = C - 4715
   }
   }
    form.nmonth.value =  MM;
    form.nday.value   =  JJ;
    form.nhour.value  =  Heure;
    form.nyear.value  =  AA;
    form.nminute.value=  Mn;
    form.nsecondes.value=Sec;
}
function Newdate(form) {
      form.nmonth.value = "";
      form.nday.value = "";
      form.nyear.value = "";
      form.nhour.value = "";
      form.nminute.value = "";
      form.nsecondes.value="";
      form.nday.focus();
}      
function startdate() {
 gettheDate()
}
function gettheDate() {
 Todays = new Date();
 TheYear = Todays.getYear();
 if (version == "ne") TheYear = (Todays.getYear() + 1900);
 TheMonth = (Todays.getMonth()+ 1);
 TheDay = Todays.getDate();
 TheHour = Todays.getHours() - 1; // HEURE D'HIVER - !ATTENTION!
 TheMinu = Todays.getMinutes();
 TheSec = Todays.getSeconds();
 document.form.nyear.value = TheYear;
 document.form.nmonth.value = TheMonth;
 document.form.nday.value = TheDay;
 document.form.nhour.value = TheHour;
 document.form.nminute.value = TheMinu;
 document.form.nsecondes.value = TheSec;
}
function Newday(form) {
      form.result.value = "";
      form.result.focus();
}
// done hiding from old browsers -->
</SCRIPT>
</HEAD>

<BODY bgcolor="#ff7f50" onLoad="startdate()">
<CENTER>
  <B><FONT SIZE=8 COLOR="beige">Calcul du Jour Julien</FONT></B>
  <HR size=5>
</CENTER>
<SCRIPT>
  var dt = new Date();
  document.write("<FONT SIZE=2> " + dt.toGMTString() + "</FONT>")
</SCRIPT>

<FORM NAME="form">
<PRE>

<B>  ENTREZ LA DATE ET L'HEURE EN TEMPS UNIVERSEL COORDONN&Eacute; </B>


          Jour:   <INPUT TYPE="text" NAME="nday"       SIZE=7>
          Mois:   <INPUT TYPE="text" NAME="nmonth"     SIZE=7>        
         Ann&eacute;e:   <INPUT TYPE="text" NAME="nyear" SIZE=7>
         Heure:   <INPUT TYPE="text" NAME="nhour"      SIZE=7 >
       Minutes:   <INPUT TYPE="text" NAME="nminute"    SIZE=7 >
      Secondes:   <INPUT TYPE="text" NAME="nsecondes"  SIZE=7 >
<P>
<B>
       <INPUT TYPE="button" VALUE="Nouvelle date" onClick="Newdate(this.form)"> <INPUT TYPE="button" VALUE="Date courante" onClick="startdate()">
               <INPUT TYPE="button" VALUE="   Calcul    " ONCLICK="compute(this.form)">
</B>
<HR>
<B>
             <INPUT TYPE="text" NAME="result" SIZE=15 onChange="reverse(this.form)">
<P>
           <INPUT TYPE="button" VALUE="Nouveau Jour Julien" onClick="Newday(this.form)">
             <INPUT TYPE="button" VALUE=" Calcul inverse " onClick="reverse(this.form)">
</B>
</PRE>
</FORM>
<HR size=5>
<PRE><B>Exemple: le 13/02/1998 12:00 UTC correspond au jour julien
         2450858.0
</B></PRE>
<img src="/icons/l_yeux.gif"><P>
<B><FONT SIZE=-1>&#169; Copyright 1996 Bureau des longitudes.</FONT></B>
<FONT SIZE=2><B>
<BR><I>Derni&egrave;re mise &agrave; jour : 9 f&eacute;vrier 1998</I><P>
<A HREF="/ephem_txt.html" TARGET="_self"><IMG ALIGN=TOP SRC="/icons/back.xbm" BORDER=0
ALT="Retour"> Retour au serveur d&acute;&eacute;ph&eacute;m&eacute;rides</A>
</B></FONT>

</BODY>
</HTML>
0
 
LVL 28

Expert Comment

by:sybe
ID: 8024702
double int ??
you mean Long ?


DateDiff("s", dtYourDate, CDate("01/01/1970"))

will give you the number of seconds between dtYourDate and January 1 1970.


0
Independent Software Vendors: 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!

 

Expert Comment

by:bazaall
ID: 8025468
A simple simple way is to get the different bits

h = hour(<date var>)
m = month(<date var)

and so on.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 400 total points
ID: 8026405
Why are we making this so complicated?  (That was a rhetorical question, no one need answer)

Date IS stored internaly as a double already, so you can just use:

Response.Write CDbl(datVar)

And the reverse:
Response.Write CDate(dblVar)


For example:
Response.Write CDbl(Now)

Output:
 37678.4253240741  

And:
Response.Write CDbl(37678.4253240741)

Output:
2/26/2003 10:12:28 AM

Anthony
0
 
LVL 7

Expert Comment

by:chisholmd
ID: 8026589
lthirty: You accepted an answer to this same question at:

http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_20529079.html

0
 
LVL 4

Expert Comment

by:JNSTAUB
ID: 8026613
how accurate is the microsoft formula, try with now=1,5 you will found 12/31/1899 12:00:00 , is it one or two days missing in the 20th century?
i agree that for short time intervalle calculation ,it will work but if you want to build calendars (muslim,indian,inca),i recommand to use the previous subroutines. why people have wroten subroutines if they fully trust in the microsoft data conversion?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 8026811
>>how accurate is the microsoft formula<<
Perhaps you misunderstood what I wrote.  There is no formula involved. I repeat Microsoft stores dates natively as doubles or more accurately as floats.  It then displays them in date format according to the regional settings. This is from BOL:

<quote>
Date variables are stored as IEEE 64-bit (8-byte) floating-point numbers that represent dates ranging from 1 January 100 to 31 December 9999 and times from 0:00:00 to 23:59:59.
</quote>

Anthony
0
 
LVL 4

Expert Comment

by:JNSTAUB
ID: 8032279
i don't agree with you ,to convert from an integer (or the int part of a float) to a date ,you need an algoritmus.if date are ranging from 1/1/100 to 12/31/9999 ,why 1=12/31/1899?
regards
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 8034378
>>why 1=12/31/1899?<< Why not?  You are advocating using a Julian date based on the Gregorian calendar which arbitrarily uses the year 0 as a base date.  What does it matter what year Microsoft chose to use as a base year? This is like arguing that Celsius (Centigrade) is better than Fahrenheit.

Anthony
0
 
LVL 4

Expert Comment

by:JNSTAUB
ID: 8034588
i just want to explain that to convert a number to a date isn't so easy for a long period as a year duration is something like 365,249xxx days but change with time as the earth slows down.you need accurate computation to convert a number to date with leap year(365 days),leap century and milenium on a 10000 years period. So a function (systemdatetovariant)is located in OLEaut32.DLL (a microsoft software), but i didn't find any document about its accuracy.
regards
0
 
LVL 4

Expert Comment

by:JNSTAUB
ID: 8034593
i just want to explain that to convert a number to a date isn't so easy for a long period as a year duration is something like 365,249xxx days but change with time as the earth slows down.you need accurate computation to convert a number to date with leap year(365 days),leap century and milenium on a 10000 years period. So a function (systemdatetovariant)is located in OLEaut32.DLL (a microsoft software), but i didn't find any document about its accuracy.
regards
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 8034757
Let me try this once again.  The original question read: "I would like to convert a date + time (i.e. 25/02/2003 10:53:50) ...", but this is only the way it is displayed, internally Microsoft stores this date as 37677.4540509259 (you can verify this by doing Response.Write CDbl(#25/02/2003 10:53:50#)

Hope this clarifies,
Anthony
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

564 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