js中的时间格式化
2018-02-21 13:58
253 查看
js的时间格式化和根据标准时间格式获得时间。
opt可以根据需要改写。
opt可以根据需要改写。
var dateTime = { /** * 根据标准时间格式(yyyy-MM-dd HH:mm:ss)获得时间 */ getDateByFormat: function (dateStr) { dateStr = dateStr.replace('-', '/'); return new Date(Date.parse(dateStr)); }, /** * 根据模板获得时间的格式化 */ dateFormat: function (date, formatStr) { // 转换成Date类型 var date = new Date(date); var opt = { 'yyyy': date.getFullYear(), 'MM': addZero(date.getMonth() + 1), 'M': date.getMonth() + 1, 'dd': addZero(date.getDate()), 'd': date.getDate(), 'HH': addZero(date.getHours()), 'H': date.getHours(), 'mm': addZero(date.getMinutes()), 'm': date.getMinutes(), 'ss': addZero(date.getSeconds()), 's': date.getSeconds() }; // 如果是个位数则前面添加0 function addZero(value) { return value < 10 ? '0' + value : value; } // 遍历替换 for (var k in opt) { formatStr = formatStr.replace(k, opt[k]); } return formatStr; }, /** * 标准时间格式 */ formatPattern: 'yyyy-MM-dd HH:mm:ss' }