?
Solved

Shot in the dark

Posted on 2005-04-04
4
Medium Priority
?
361 Views
Last Modified: 2006-11-18
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
Comment
Question by:nickel2001
[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
  • 2
  • 2
4 Comments
 
LVL 19

Expert Comment

by:dakyd
ID: 13702413
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
 

Author Comment

by:nickel2001
ID: 13712864
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
 
LVL 19

Accepted Solution

by:
dakyd earned 200 total points
ID: 13717328
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
 

Author Comment

by:nickel2001
ID: 13717394
Great. I'll check it out.

Thanks a lot,
Nickel
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

777 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