Link to home
Start Free TrialLog in
Avatar of lthiry
lthiry

asked on

Convert date+time to double integer & back

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.
Avatar of JNSTAUB
JNSTAUB
Flag of France image

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
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>
Avatar of sybe
sybe

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.


A simple simple way is to get the different bits

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

and so on.
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
>>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
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
>>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
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
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
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