js 时间戳转日期时间
Posted 流星蝴蝶没有剑
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js 时间戳转日期时间相关的知识,希望对你有一定的参考价值。
JS 日期格式化
时间戳转日期时间
字符串时间格式化
function timeFomat(t, e)
var s =t ? new Date(t) : new Date(1e3 * t)
, i = s.getFullYear()
, n = s.getMonth() + 1
, a = s.getDate()
, o = s.getHours()
, l = s.getMinutes()
, u = s.getSeconds();
switch (e)
case "ss":
return u;
case "mm:ss":
return l + ":" + u;
case "HH:mm:ss":
return o + ":" + l + ":" + u;
case "dd":
return a;
case "dd HH":
return a + " " + o;
case "dd HH:mm":
return a + " " + o + ":" + l;
case "dd HH:mm:ss":
return a + " " + o + ":" + l + ":" + u;
case "MM":
return n;
case "MM-dd":
return n + "-" + a;
case "MM-dd HH":
return n + "-" + a + " " + o;
case "MM-dd HH:mm":
return n + "-" + a + " " + o + ":" + l;
case "MM-dd HH:mm:ss":
return n + "-" + a + " " + o + ":" + l + ":" + u;
case "yyyy":
return i;
case "yyyy-MM":
return i + "-" + n;
case "yyyy-MM-dd":
return i + "-" + n + "-" + a;
case "yyyy-MM-dd HH":
return i + "-" + n + "-" + a + " " + o;
case "yyyy-MM-dd HH:mm":
return i + "-" + n + "-" + a + " " + o + ":" + l;
case "year":
case "yyyy-MM-dd HH:mm:ss":
default:
return i + "-" + n + "-" + a + " " + o + ":" + l + ":" + u
// 时间戳
timeFomat(new Date().getTime(), "yyyy-MM-dd HH:mm:ss")
// 字符串日期
timeFomat("2022-5-6 15:2:38", "yyyy-MM-dd")
以上是关于js 时间戳转日期时间的主要内容,如果未能解决你的问题,请参考以下文章