您的位置:首页 > Web前端 > JavaScript

js时间格式化

2016-09-18 15:28 471 查看
const formatDate = timestamp => {
  const date = new Date(timestamp);
  const m = date.getMonth() + 1;
  const d = date.getDate();
  const h = date.getHours();
  const i = date.getMinutes();
  return m + '月' + d + '日' + ' ' + h + ':' + i;
};

使用 :
formatDate(startTime * 1000)

/**
* 日期时间格式化 (CST 转化为标准时间可用)

*/
exports.formatDateCst = function(stamp, formatString){
var date = new Date(stamp);
var o = {
'M+': date.getMonth() + 1,
'D+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds(),
'S' : date.getMilliseconds()
};
if(/(Y+)/.test(formatString)){
formatString = formatString.replace(RegExp.$1, (date.getFullYear() + "").substr(2 - RegExp.$1.length));
}
for(var k in o){
if(new RegExp('(' + k + ')').test(formatString)){
formatString = formatString.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
}
}
return formatString;
}

use: formatDateCst(value,'YY-MM-DD hh:mm:ss')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: