<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <!-- 根据指定 日期格式 生成 指定格式的指定日期 --> <script> // y代表年 M代表月份 d代表日 s代表秒 m代表分钟 h代表小时 //getTemplateBy$n('yyyy-MM-dd hh:mm:ss',date) 2017-08-02 14:56:22 //getTemplateBy$n('yy-MM-dd hh:mm',date) 17-08-02 14:56 //getTemplateBy$n('hh时mm分ss秒 MM/dd/yyyy',date) 14时56分28秒 08/02/2017 var date = new Date() console.log(getTemplateBy$n('yyyy-MM-dd hh:mm:ss',date)) function getTemplateBy$n(template, date) { if (/y+/.test(template)) { template = template.replace(/y+/g, function (match) { return (date.getFullYear() + '').substr(4 - match.length) }) } var obj = { 'M+': date.getMonth() + 1, 'd+': date.getDate(), 'h+': date.getHours(), 'm+': date.getMinutes(), 's+': date.getSeconds() } for (var k in obj) { var reg = new RegExp('('+k+')', 'g'); if (reg.test(template)) { var str = obj[k]+''; template = template.replace(RegExp.$1,(RegExp.$1.length === 1) ? str : ('00'+str).substr(str.length)) } } return template } </script> </body> </html>
根据指定 日期格式 生成 指定格式的指定日期
最新推荐文章于 2024-08-02 10:47:15 发布