AM / PM JavaScript time comparison

Suppose I have two datetime variables:

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

I want to compare these two times. How can I get javascript to find out if the time is AM or PM?

I think Javascript will compare the time in 24 hour format. Do I need to convert time to 24 hours format? It's right? Can anyone suggest the right solution ....

+4
source share
3 answers

Just use direct 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"); } 

After the date is parsed in the form of a date, you can do anything with it. And you can compare dates using 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 a good link.

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

and the best that XX removed :( http://jsfiddle.net/QpFcW/1/

+6
source

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); } 
0
source

Try it.

 var fromdt="2013/05/29 12:30 PM"; var todt="2013/05/29 01:30 AM"; var from = Date.parse(fromdt); var to = Date.parse(todt); alert(from); alert(to) if (from > to){ alert("From"); }else{ alert("To"); } 
0
source

Source: https://habr.com/ru/post/1484015/


All Articles