Yasser1984
Yasser1984

Reputation: 2441

Javascript, how to find the difference between two datetimes in mysql timestamp style

Let's say I have

a = "2011-11-09 08:00:00"
b = "2011-11-10 08:30:00"

What's the best way of finding how many days, hours, minutes the difference between these two timestamps are in Javascript?

So the output should be "1 day" (ignore the minutes since there is a larger unit (day) in the difference) ?

Upvotes: 1

Views: 4968

Answers (3)

Milimetric
Milimetric

Reputation: 13549

EDITED to work in any browser

var matchDate = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/;
var firstDateParsed = matchDate.exec("2011-11-09 08:00:00");
var secondDateParsed = matchDate.exec("2011-11-10 08:30:00");

var a = new Date(firstDateParsed[1], firstDateParsed[2], firstDateParsed[3], firstDateParsed[4], firstDateParsed[5], firstDateParsed[6], 0);
var b = new Date(secondDateParsed[1], secondDateParsed[2], secondDateParsed[3], secondDateParsed[4], secondDateParsed[5], secondDateParsed[6], 0);
var differenceInMilliseconds = a.getTime() - b.getTime();

// minutes
alert(differenceInMilliseconds / 1000 / 60);

// hours
alert(differenceInMilliseconds / 1000 / 60 / 60);

// days
alert(differenceInMilliseconds / 1000 / 60 / 60 / 24);

Tested in IE and Firefox as well as Chrome: http://jsfiddle.net/xkBTS/4/

Upvotes: 1

gilly3
gilly3

Reputation: 91467

You'll have to parse the timestamp to a date yourself:

function parseMySQLTimestamp(timestamp) {
    var parts = timestamp.match(/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/);
    return new Date(+parts[1], (+parts[2] - 1), +parts[3], +parts[4], +parts[5], +parts[6]);
}

Get the difference in milliseconds by subtracting one date from the other:

var msDifference = parseMySQLTimestamp(b) - parseMySQLTimestamp(a);

Simple arithmetic will let you convert milliseconds to seconds, minutes, or whatever.

By the way, this function will throw an error if a timestamp is passed in that doesn't match the expression. From a software design point of view, this behavior makes sense to me. However, if you want to be able to use that function with strings that may not be in the correct format, you can just do a null check against parts and return null if there is no match.

Upvotes: 0

RobG
RobG

Reputation: 147363

The only reliable way to convert a string to a date in javascript is to parse it manually. If the format is consistent with what you have posted, then you can convert it to a date as follows:

function stringToDate(s) {
  var dateParts = s.split(' ')[0].split('-'); 
  var timeParts = s.split(' ')[1].split(':');
  var d = new Date(dateParts[0], --dateParts[1], dateParts[2]);
  d.setHours(timeParts[0], timeParts[1], timeParts[2])

  return d
}

so you can do:

var a = "2011-11-09 08:00:00"
var b = "2011-11-10 08:30:00"
alert(stringToDate(a) - stringToDate(b));

to get the difference in milliseconds. However, the difference in days between two dates may not be a simple matter of dividing the difference by 8.64e7 (milliseconds in one da). You need to confirm the business logic in regard to that.

Upvotes: 2

Related Questions