1

I have two dates grabbed from MySQL database using XML as follows. I want to get time difference in seconds.

I wrote this script, but it giving "NaN"

<script type="text/javascript">
function show(){
var t1 = new Date("2014-01-21 13:50:20.123");
var t2 = new Date("2014-01-21 12:50:20.123");
var dif = t1.getTime() - t2.getTime()

var Seconds_from_T1_to_T2 = dif / 1000;
var Seconds_Between_Dates = Math.abs(Seconds_from_T1_to_T2);

document.getElementById("TDIFF_IN_SECONDS").value=Seconds_Between_Dates;
}
</script>

HTML CODE

<body onload="show()">
<input type="text" id="TDIFF_IN_SECONDS" />
</body>

If anyone know, please help me ...

LahiruTM
  • 638
  • 4
  • 16
  • You could just Date.parse and shorten your code a little bit. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse – thatidiotguy Jan 23 '14 at 15:21
  • Depending on which browser you are using this on, it may be that `Date` doesn't support ISO8601 parsing. Would require an ECMA5 compatible browser. Even then some support a space ' ' date/time delimeter, and some require 'T' – Xotic750 Jan 23 '14 at 15:32

4 Answers4

0

Your date object initialization not supported by the browser you are using.

var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds); 

You can see more details in http://www.w3schools.com/jsref/jsref_obj_date.asp

You can set your date time as

var t1 = new Date(2014, 1, 21, 13, 50, 20, 123);

Xotic750 added a very good point:

Depending on which browser you are using this on, it may be that Date doesn't support ISO8601 parsing. Would require an ECMA5 compatible browser. Even then some support a space ' ' date/time delimeter, and some require 'T'

ZubaiR
  • 607
  • 4
  • 7
  • Nothing wrong with the Date object initialisation, but does depend on whether the browser he/she is using can supports parsing an ISO8601 `dateString` – Xotic750 Jan 23 '14 at 15:46
  • If the date object does not support all browser versions then isn't that the problem for this question ? – ZubaiR Jan 23 '14 at 15:49
  • Date parsing in Javascript has been a very hit and miss affair, well before ECMA5 added ISO8601 support, take a look at this [support table](http://dygraphs.com/date-formats.html). So it's more a case of avoiding `Date.parse`, where possible, and either doing it yourself or use a library to normalise functionality. – Xotic750 Jan 23 '14 at 16:04
0

If your dateString is always the same format then you can do it yourself without the need of loading jQuery and moments libraries. I am assuming that your timestamp is UTC, ISO8601 would assume that it is local (different browsers and libraries also do not agree with one another on this point). This should work across browsers.

MDN is a much better reference for Date than w3schools.

HTML

<input type="text" id="TDIFF_IN_SECONDS" />

Javascript

function parseMyDate(dateString) {
    var parts = dateString.split(/[ \-:\.]/g);

    parts[1] -= 1;
    return new Date(Date.UTC.apply(undefined, parts)).getTime();
}

document.getElementById("TDIFF_IN_SECONDS").value = Math.abs(parseMyDate('2014-01-21 13:50:20.123') - 
                                                              parseMyDate('2014-01-21 12:50:20.123')) / 1000;

On jsFiddle

Xotic750
  • 22,914
  • 8
  • 57
  • 79
  • Thank You for all, this one works well for me. function parseMyDate(dateString) { var parts = dateString.split(/[ \-:\.]/g); parts[1] -= 1; return new Date(Date.UTC.apply(undefined, parts)).getTime(); } document.getElementById("TDIFF_IN_SECONDS").value = Math.abs(parseMyDate('2014-01-21 13:50:20.123') - parseMyDate('2014-01-21 12:50:20.123')) / 1000; – LahiruTM Feb 14 '14 at 19:45
-1

The issue is that you are trying to subtract two datetime objects. These aren't integers and you can't do math with them in this way. However, the following link will guide you in the right direction: Link

In it, David Hedlund made the following post:

"his will give you the difference between two dates, in milliseconds

var diff = Math.abs(date1 - date2);

In your example, it'd be

var diff = Math.abs(new Date() - compareDate);

You need to make sure that compareDate is a valid Date object.

Something like this will probably work for you

var diff = Math.abs(new Date() - new Date(dateStr.replace(/-/g,'/')));

i.e. turning "2011-02-07 15:13:06" into new Date('2011/02/07 15:13:06'), which is a format the Date constructor can comprehend."

Community
  • 1
  • 1
WreithKassan
  • 205
  • 1
  • 7
  • 1
    `getTime` converts the Date object to a numeric value (integer) that represents the time as a number of milliseconds since `1 January 1970 00:00:00 UTC` So there is nothing wrong with applying math those numbers, as the OP is doing. – Xotic750 Jan 23 '14 at 15:49
  • Very true. Good catch. +1 – WreithKassan Jan 23 '14 at 15:50
-1

Use a library to make your life easier - http://momentjs.com/

Example: http://jsfiddle.net/c6sYz/

Requires: jQuery & momentjs. If you don't use jQuery, this can be changed to pure JS with onload.

$( document ).ready(function() {
    var t1 = moment("2014-01-21 13:50:20.123", "YYYY-MM-DD HH:mm:ss.sss");

    var t2 = moment("2014-01-21 12:50:20.123", "YYYY-MM-DD HH:mm:ss.sss");

    var diff = t1.diff(t2, 'seconds');

    $("#diff").val(diff);    
});
Tan Hong Tat
  • 6,671
  • 2
  • 27
  • 25
  • Why is it a problem that means I need to use some library? How does it make my life easier to use one? Can you give an example of how to achieve what was attempted but by using the library suggested (perhaps a jsFiddle)? – Xotic750 Jan 23 '14 at 17:24
  • Browser compatibility, simplifying complex operations, and so much more. Similar to why jQuery (and other JS frameworks), and why not pure Javasript? - http://jsfiddle.net/c6sYz/ – Tan Hong Tat Jan 24 '14 at 02:37