我在javascript 0000-00-00 00:00:00没有得到确切的mysql默认时间格式

时间:2017-12-11 08:11:37

标签: javascript mysql date datetime format

我已经尝试了很多方法在JavaScript中打印出MySQL默认日期时间(0000-00-00 00:00:00)格式。任何帮助将受到高度赞赏。

由于

1 个答案:

答案 0 :(得分:0)

你可以在date& time字符串并用作。

var date = '2017-12-11';
var time = '11:20:05';
var yourDateString = new Date(date + "T" + time+"Z");
console.log(yourDateString);
console.log(date2str(yourDateString,''));

function date2str(x, y) {
var z = {
    YR: x.getFullYear(),
    M: x.getMonth() + 1,
    d: x.getDate(),
    h: x.getHours(),
    m: x.getMinutes(),
    s: x.getSeconds()
};

{
      var now = new Date();
      var timeOpt = {hour:'2-digit', hour12:true, minute:'2-digit'};
      var monthOpt = {day:'numeric', month:'short'};
      var fullOpt = {day:'numeric', month:'short', year:'2-digit'};
      if ( ((now - x) < 8.64e7) && (now.getDate() == z.d) ) {
          var timeFragements = x.toLocaleString().toLowerCase();
          if(null == timeFragements.match(/(\d+:\d+:\d+)+/)){
              return x.toLocaleString("en-US", timeOpt).toLowerCase();
          }
          timeFragements =(timeFragements.match(/(\d+:\d+:\d+)+/)[0])
          //Calculation of Am /Pm.
          var hours = x.getHours();
          var amPmStr = hours >= 12 ? ' pm' : ' am';
          if(timeFragements.split(':')[0]<12){
              return (("0" + timeFragements.split(':')[0]).slice(-2) +':'+ ("0" + timeFragements.split(':')[1]).slice(-2) + amPmStr );
          }else if(timeFragements.split(':')[0]== 12){
               return (("0" + (timeFragements.split(':')[0]) ).slice(-2) +':'+ ("0" + timeFragements.split(':')[1]).slice(-2) + amPmStr );
          }else{
              return (("0" + (timeFragements.split(':')[0]-12) ).slice(-2) +':'+ ("0" + timeFragements.split(':')[1]).slice(-2) + amPmStr );
          }
      } else if (  (now.getFullYear() == x.getFullYear()) && (now.getMonth() == x.getMonth()) ) {
         return ("" + x.toDateString().split(' ')[1])+" "+("0" + x.getDate().toString()).slice(-2);
      }else if( (now.getFullYear() == x.getFullYear()) && (now.getMonth() != x.getMonth())){
          return ("" + x.toDateString().split(' ')[1])+" "+("0" + x.getDate().toString()).slice(-2);
      }else if(now.getFullYear() !== x.getFullYear()){
          return x.toUTCString().split(',')[1].slice(0, 12);
      }

}

}