7

Suppose I have 2 datetime variables:

var fromdt = "2013/05/29 12:30 PM";
var todt = "2013/05/29 01:30 AM";

I want to compare these 2 datetimes. How can I get Javascript to recognize whether the time is AM or PM?

I think Javascript will compare the time in 24 hours format. Do I need to convert the time to 24 hour format? Is that correct? Could someone please suggest the correct solution....

Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
Dylan
  • 137
  • 1
  • 2
  • 10
  • 1
    take a look at http://momentjs.com/ – c0deNinja Jun 02 '13 at 07:23
  • Are they in 24 hour format? – JJJ Jun 02 '13 at 07:23
  • no 12 hours format....like AM and PM – Dylan Jun 02 '13 at 07:24
  • @codezNinja..I don't want to use external librarries.. – Dylan Jun 02 '13 at 07:24
  • The usual way to deal with dates is having them in 24 hour format where `01:30` = AM and `13:30` = PM. With the times you provide, there is no way to tell whether they are AM or PM. – Pekka Jun 02 '13 at 07:24
  • 5
    So how would **YOU** recognize if `01:30` is a daytime or night time? – zerkms Jun 02 '13 at 07:24
  • @zerkms..I think Javascript will compare the time by 24 hours format. And we need to convert the time to 24 hours format? – Dylan Jun 02 '13 at 07:26
  • @Dylan: I didn't ask about JS, I asked about you. Btw, `AM/PM` format is just a time representation, how it stores internally - is another question. But for sake of simplicity - internal data doesn't care of am/pm at all – zerkms Jun 02 '13 at 07:27
  • 1
    So the strings do have "AM" or "PM" in them? You didn't think that was relevant information? – JJJ Jun 02 '13 at 07:28
  • @zerkms: Yes, I've edited my code and set AM and PM to the datestrings.. – Dylan Jun 02 '13 at 07:28

2 Answers2

8

Just use straight javascript functions

var fromdt = "2013/05/29 12:30 PM";
var todt = "2013/05/29 01:30 AM";
var from = new Date(Date.parse(fromdt));
var to = new Date(Date.parse(todt));

alert(from);
alert(to)

if (from > to) alert("From");
else alert("To");

Once the date is parsed into date form, you can do what you like with it. And you can compare dates using the standard operator signs ( >, < etc)

I'm not sure what you need to do with them, but http://www.w3schools.com/jsref/jsref_obj_date.asp is an okay reference.

And heres a crappy sandbox with the above code http://jsfiddle.net/QpFcW/

and a better one that XX deleted :( http://jsfiddle.net/QpFcW/1/

Manas Khandelwal
  • 3,790
  • 2
  • 11
  • 24
monkeyhouse
  • 2,875
  • 3
  • 27
  • 42
0

Use this function

function get_time() {
  var time_t = "";
  var d = new Date();
  var cur_hour = d.getHours();

  cur_hour < 12 ? (time_t = "am") : (time_t = "pm");
  cur_hour == 0 ? (cur_hour = 12) : (cur_hour = cur_hour);
  cur_hour > 12 ? (cur_hour = cur_hour - 12) : (cur_hour = cur_hour);
  var curr_min = d.getMinutes().toString();
  var curr_sec = d.getSeconds().toString();
  if (curr_min.length == 1) {
    curr_min = "0" + curr_min;
  }
  if (curr_sec.length == 1) {
    curr_sec = "0" + curr_sec;
  }
  $("#updatedTime").html(
    cur_hour + ":" + curr_min + ":" + curr_sec + " " + time_t
  );
  alert(cur_hour + ":" + curr_min + ":" + curr_sec + " " + time_t);
}
Manas Khandelwal
  • 3,790
  • 2
  • 11
  • 24
Mahmoude Elghandour
  • 2,921
  • 1
  • 22
  • 27