js Date对象转成 对应格式的字符串 && NODE.JS从数据库读取的DATETIME对象 转成对应格式的字符串

来源:互联网 发布:nginx视频教程 编辑:程序博客网 时间:2024/06/07 05:43

1.Date对象转成 对应格式的字符串 

//格式化日期,

      function formatDate(date,format){
        var paddNum = function(num){
          num += "";
          return num.replace(/^(\d)$/,"0$1");
        }
        //指定格式字符
        var cfg = {
           yyyy : date.getFullYear() //年 : 4位
          ,yy : date.getFullYear().toString().substring(2)//年 : 2位
          ,M  : date.getMonth() + 1  //月 : 如果1位的时候不补0
          ,MM : paddNum(date.getMonth() + 1) //月 : 如果1位的时候补0
          ,d  : date.getDate()   //日 : 如果1位的时候不补0
          ,dd : paddNum(date.getDate())//日 : 如果1位的时候补0
          ,hh : date.getHours()  //时
          ,mm : date.getMinutes() //分
          ,ss : date.getSeconds() //秒
        }
        format || (format = "yyyy-MM-dd hh:mm:ss");
        return format.replace(/([a-z])(\1)*/ig,function(m){return cfg[m];});

      } 


2.NODE.JS

var moment = require('moment');

moment(value.playing_time_).format('YYYY-MM-DD HH:mm:ss');

阅读全文
0 0
原创粉丝点击