使用JavaScript转换日期在Firefox

时间:2015-09-19 19:27:37

标签: javascript date datetime firefox

// 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;
  },
};

JavaScript中的格式化日期...

console.log(dates.formatDate('2014-06-17 00:00:00'));

返回FIrefox中的NaN/NaN/NaN 12:NaN am和Chrome中的6/17/2014 12:00 am

在没有时间的情况下传递日期在Firefox中正常工作

console.log(dates.formatDate('2014-06-17'));

在FIrefox中返回6/16/2014 8:00 pm

我希望能够通过时间传递一个日期,或者能够在FIrefox和Chrome中使用它

0 个答案:

没有答案