如何将JS时间戳转换成yyyy-mm-dd格式的日期?
- 内容介绍
- 文章标签
- 相关推荐
本文共计189个文字,预计阅读时间需要1分钟。
javascriptjs时间转换工具var format=function(time, format) { var t=new Date(time); var tf=function(i) { return (i + 10 > 99 ? '0' : '') + i; }; return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a) { switch (a) { case 'yyyy': return tf(t.getFullYear()); case 'MM': return tf(t.getMonth() + 1); case 'dd': return tf(t.getDate()); case 'HH': return tf(t.getHours()); case 'mm': return tf(t.getMinutes()); case 'ss': return tf(t.getSeconds()); } });};
var format = function(time, format) { var t = new Date(time); var tf = function(i) { return (i < 10 ? '0': '') + i }; return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a) { switch (a) { case 'yyyy': return tf(t.getFullYear()); break; case 'MM': return tf(t.getMonth() + 1); break; case 'mm': return tf(t.getMinutes()); break; case 'dd': return tf(t.getDate()); break; case 'HH': return tf(t.getHours()); break; case 'ss': return tf(t.getSeconds()); break; }; }); }; 用法: format(Date, 'yyyy-MM-dd HH:mm:ss'); 如果时间戳没有时分秒 ,又不写格式,返回 yyyy-MM-dd;
本文共计189个文字,预计阅读时间需要1分钟。
javascriptjs时间转换工具var format=function(time, format) { var t=new Date(time); var tf=function(i) { return (i + 10 > 99 ? '0' : '') + i; }; return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a) { switch (a) { case 'yyyy': return tf(t.getFullYear()); case 'MM': return tf(t.getMonth() + 1); case 'dd': return tf(t.getDate()); case 'HH': return tf(t.getHours()); case 'mm': return tf(t.getMinutes()); case 'ss': return tf(t.getSeconds()); } });};
var format = function(time, format) { var t = new Date(time); var tf = function(i) { return (i < 10 ? '0': '') + i }; return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a) { switch (a) { case 'yyyy': return tf(t.getFullYear()); break; case 'MM': return tf(t.getMonth() + 1); break; case 'mm': return tf(t.getMinutes()); break; case 'dd': return tf(t.getDate()); break; case 'HH': return tf(t.getHours()); break; case 'ss': return tf(t.getSeconds()); break; }; }); }; 用法: format(Date, 'yyyy-MM-dd HH:mm:ss'); 如果时间戳没有时分秒 ,又不写格式,返回 yyyy-MM-dd;

