klye_g
klye_g

Reputation: 1242

Javascript DATE add AM PM

I am doing a check of two different date times to see if one is greater than the other:

Here is my (now) current date time: Thu Aug 01 2013 10:27:40 GMT-0500 (CDT) And here is my date time that I am seeing if it is greater or less than: Thu Aug 01 2013 12:15:00 GMT-0500 (CDT) - (that should be 12:15 am by the way)

Here is my code:

var current_date_time = new Date();
var date_time_checking_against = new Date(date_segment[0], date_segment[1]-1, date_segment[2], time_segment[0], time_segment[1]);

Which comes out to Thu Aug 01 2013 12:15:00 GMT-0500 (CDT). And then I am doing a simple if check:

if(current_date_time >= date_time_checking_against){ }

This is not working as 10:27:40 is not after 12:15:00. But it should be, seeing as how both times are AM. I need to know if this is the right way, or if there is a way to change it to 24 hour format or add am pm in there somehow. Any help is greatly appreciated, let me know if you need more clarity.

Thanks!

EDIT:

Here is the date time array:

var date_time_str = date+' '+time;
date_time_str = date_time_str.split(' ');
["2013-08-01", "12:15", "am"] // result from split above
var date_segment = date_time_str[0].split('-');
var time_segment = date_time_str[1].split(':');
var date_time_checking_against = new Date(date_segment[0], date_segment[1]-1, date_segment[2], time_segment[0], time_segment[1]);

Upvotes: 0

Views: 956

Answers (1)

Pluto
Pluto

Reputation: 3026

Given the following data sources, this is how you'd properly create the Date object for it...

date_time_str = ["2013-08-01", "12:15", "am"];
var date_segment = date_time_str[0].split('-');
var time_segment = date_time_str[1].split(':');
var date_time_checking_against = new Date(
    date_segment[0], // year
    date_segment[1]-1, // month of year
    date_segment[2], // day of month
    (time_segment[0]%12) + (date_time_str[2] == 'pm' ? 12 : 0), // hour of day
    time_segment[1]); // minute of hour
console.log(new Date() >= date_time_checking_against); // true, we've already passed this time

Upvotes: 1

Related Questions