Reputation: 48933
// Date library for DateTime comparisons and checkingifa date is in-between arange of 2 dates!
// Source: http://stackoverflow.com/questions/497790
var dates = {
// Format a Date string or object as "9/17/2015 8:15 pm"
// var date can be;
// - Date String = creates date object with DateTime in the string
// - String value of "now" = creates date object with current DateTime
// - Date Object = uses the Date object passed in
// - null/no value = creates date object with current DateTime
formatDate: function(date) {
if (typeof(date) == "undefined") {
date = new Date();
} else if (typeof(date) == "string") {
if (date == 'now') {
date = new Date();
} else {
var date_string = date;
date = new Date(date_string);
}
}
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return date.getMonth() + 1 + "/" + date.getDate() + "/" + date.getFullYear() + " " + strTime;
},
};
Format Date in JavaScript...
console.log(dates.formatDate('2014-06-17 00:00:00'));
Returns NaN/NaN/NaN 12:NaN am
in FIrefox and 6/17/2014 12:00 am
in Chrome
Passing in a Date without a time works fine in Firefox
console.log(dates.formatDate('2014-06-17'));
Returns 6/16/2014 8:00 pm
in FIrefox
I would like to be able to pass in a Date with a time or witohut a time and have it work in FIrefox and Chrome
Upvotes: 2
Views: 1734
Reputation: 46323
The new Date(date_string)
constructor supports the same date formats supported by Date.parse()
, namely RFC2822 and ISO 8601. Additional support is optional, but not mandatory.
Your string is in neither of the supported formats, and is unrecognized by Firefox. It is also "safe" to assume it won't be supported by other JavaScript engines.
On the other hand, if you specifically expect this format, you can easily convert it to ISO 8601:
alert(new Date('2014-06-17 00:00:00'.replace(' ', 'T')))
Upvotes: 1