码迷,mamicode.com
首页 > Web开发 > 详细

js - 日期格式化

时间:2021-01-01 12:46:23      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:func   time   日期   datetime   add   atd   ddd   date()   ==   

  • yyyy-mm-dd
formatDate: function (date) {
	function addDateZero(num) {
		return (num < 10 ? "0" + num : num);
	}
	var d = new Date(date);
	var formatdatetime = d.getFullYear() + ‘-‘ + addDateZero(d.getMonth() + 1) + ‘-‘ + addDateZero(d.getDate());
	return formatdatetime;				
}
  • yyyy.mm.dd hh:mm:ss AM/PM
formatDate1: function (date) {
    function addDateZero(num) {
        return (num < 10 ? "0" + num : num);
    }
    var d = new Date(date);
    var formatdatetime = d.getFullYear() + ‘.‘ + addDateZero(d.getMonth() + 1) + ‘.‘ + addDateZero(d.getDate()) + ‘ ‘ 
       + (d.getHours() <= 12 ?  (d.getHours() == 0 ? addDateZero(d.getHours() + 12) : addDateZero(d.getHours())) : addDateZero(d.getHours() - 12)) 
       + ‘:‘ + addDateZero(d.getMinutes()) 
       + ‘:‘ + addDateZero(d.getSeconds()) + ‘ ‘
       + (d.getHours() < 12 ? ‘AM‘ : ‘PM‘);
    return formatdatetime;                                                       
}

js - 日期格式化

标签:func   time   日期   datetime   add   atd   ddd   date()   ==   

原文地址:https://www.cnblogs.com/huangbinlooksgood/p/14201757.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!