JavaScript日期格式转换

来源:互联网 发布:vx什么软件 编辑:程序博客网 时间:2024/06/16 05:59
var formatDate = function(oDate,sFormation){  var obj = {    yyyyyyyy:oDate.getFullYear(),    yy:oDate.getFullYear(),    MM:oDate.getMonth()+1,    dd:oDate.getDate(),    HH:oDate.getHours(),    hh:oDate.getHours() % 12,    mm:oDate.getMinutes(),    ss:oDate.getSeconds(),    ww:"星期"+['日', '一', '二', '三', '四', '五', '六'][oDate.getDay()]  };  return sFormation.replace(/([a-z]+)/ig,function($1){      return obj[$1+$1]||('0'+obj[$1]).slice(-2);  });}

内容非原创,借鉴了牛客网某个题的回答,感觉这种思路比较优秀,也较为实用
使用示例:console.log(formatDate(new Date(),’yyyy-MM-dd HH:mm:ss w’));

原创粉丝点击