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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

Shot in the dark

Hi All:

I am working with an OLE application which data can be manipulated with javascript. I have as a database native date format 1,049,200,679 For example.
This corresponds to  01-APR-2003 06:37:26.
My goal is to substract two such dates and return the number of month, days, hours and seconds in between.
I think that the easiest would be to work with the 1,049,200,679 format. For example: If I substract, 1,049,200,679 with  1,049,200,604 (01-APR-2003 06:36:44), I get  75. Now the question is....
How do I convert this 75 into a month,days, hours and seconds format.

Thanks!
Nickel
0
nickel2001
Asked:
nickel2001
  • 2
  • 2
1 Solution
 
dakydCommented:
Are you sure that 1,049,200,679 corresponds to 01-APR-2003 06:37:26?  The reason I ask is that those numbers look suspiciously close to the number of seconds since Jan 1, 1970 00:00:00.  Here's a little test script I wrote that illustrates this fact:

<script type="text/javascript">
var date = new Date(1049200679*1000);     // expects argument is in ms, multiply by 1000
var date2 = new Date(1049200604*1000);

document.write(date + "<br />" + date2);
</script>

When I did that, I got:
Tue Apr 1 04:37:59 PST 2003
Tue Apr 1 04:36:44 PST 2003

Hence my question, that seems way too close to be a coincidence.  If that's the case, then the difference between any two given numbers is equal to the number of seconds between those two dates.  It's also a bit of a stab in the dark, but hope it helps.
0
 
nickel2001Author Commented:
You are right. My assumptions about the format were wrong.
How do I go from the numbers of seconds between the two dates to a
days, hours, minutes format?

Thanks a lot for tackling this.

Nickel
0
 
dakydCommented:
Here's a sample script that will do the conversion for you.  It uses the absolute value of the difference, so it doesn't matter what order you put the dates in.  Hope that helps.

<script type="text/javascript">
function dateDiff(d1, d2)
{
  var totalSecs = Math.abs(d1 - d2);

  var days = parseInt(totalSecs / (60 * 60 * 24));
  totalSecs = totalSecs % (60 * 60 * 24);

  var hours = parseInt(totalSecs / (60 * 60));
  totalSecs = totalSecs % (60 * 60);

  var mins = parseInt(totalSecs / 60);
  totalSecs = totalSecs % 60;

  document.write("<h4 style='margin-bottom: 0'>For dates of " + d1 + " and " + d2 + ":</h4>");
  document.write("<p style='margin-top: 0'>Difference is " + days + " days, " + hours + " hours, " + mins + " minutes, and " + totalSecs + " seconds</p>");
}

dateDiff(1049200679, 1049200604);        // should return 1 min, 15 seconds
dateDiff(3661, 0);                  // should return 1 hour, 1 min, 1 second
dateDiff(90061, 0);                  // should return 1 day, 1 hour, 1 min, 1 second
dateDiff(310, 10);                  // should return 5 mins
</script>
0
 
nickel2001Author Commented:
Great. I'll check it out.

Thanks a lot,
Nickel
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now