js时间戳日期转换
程序员文章站
2022-03-29 18:33:21
...
时间戳转换js工具类
// 时间戳转换日期
function add0(m){
return m<10?'0'+m:m
}
/**
* 时间戳转 Date 2020年6月30日20点53分
* @param timeStamp
* @returns {string}
*/
function format(timeStamp){
var result = "";
if (timeStamp != undefined) {
//timeStamp是整数,否则要parseInt转换
var time = new Date(timeStamp);
var y = time.getFullYear();
var m = time.getMonth()+1;
var d = time.getDate();
var h = time.getHours();
var mm = time.getMinutes();
var s = time.getSeconds();
result = y+'年'+add0(m)+'月'+add0(d)+'日'+add0(h)+'时'+add0(mm)+'分';
}
return result;
}
/**
* 时间戳转 Date 2020-06-30 20:53
* @param timeStamp
* @returns {string}
*/
function formatToDate(timeStamp){// eg: 2020-06-30 20:53
var result = "";
if (timeStamp != undefined) {
//timeStamp是整数,否则要parseInt转换
var time = new Date(timeStamp);
var y = time.getFullYear();
var m = time.getMonth()+1;
var d = time.getDate();
var h = time.getHours();
var mm = time.getMinutes();
var s = time.getSeconds();
result = y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm);
}
return result;
}
上一篇: 跟老黄拼刀法 消息称AMD将推出新版RX 6000显卡:6nm工艺有戏
下一篇: js时间戳日期转换