js时间戳怎么转成日期格式

如题所述

javascript 时间戳自定义格式转换,支持年月日时分秒等多种形式组合的日期和时间。

示例

年、月、日、时、分、秒

var date = jutils.formatDate(new Date(1533686888*1000),"YYYY-MM-DD HH:ii:ss");
console.log(date);
// 2019-07-09 19:44:01

更多自定义参数和用法可以参照:

javascript格式化日期时间

下面是源码的截图:

js时间戳转为日期格式

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-11-23

时间戳就是从1970/1/1 0:00:00起到现在的秒数(按格林威治时间算),根据这个就能转换啦。最简单的方法就是:

var d=new Date(timestamp*1000);  //秒数要转为毫秒数
alert(d.toLocaleString());   //转换为本地时间格式

第2个回答  2016-04-26
//第一种
function getLocalTime(nS) {
return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
}
alert(getLocalTime(1293072805));
//结果是2010年12月23日 10:53//第二种
function getLocalTime(nS) {
return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17)
}
alert(getLocalTime(1293072805));//第三种 格式为:2010-10-20 10:00:00
function getLocalTime(nS) {
return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
}
alert(getLocalTime(1177824835));
第3个回答  2016-08-28

 调用 getSmpFormatDateByLong(long,true)

long是时间戳

true 表示获取到的结果是 yyyy-MM-dd hh:mm:ss 格式

false 则得到的事 yyyy-MM-dd 格式

Date.prototype.format = function (format) { 
var o = { 
"M+": this.getMonth() + 1, 
"d+": this.getDate(), 
"h+": this.getHours(), 
"m+": this.getMinutes(), 
"s+": this.getSeconds(), 
"q+": Math.floor((this.getMonth() + 3) / 3), 
"S": this.getMilliseconds() 

if (/(y+)/.test(format)) { 
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); 

for (var k in o) { 
if (new RegExp("(" + k + ")").test(format)) { 
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); 


return format; 

/** 
*转换日期对象为日期字符串 
* @param date 日期对象 
* @param isFull 是否为完整的日期数据, 
* 为true时, 格式如"2000-03-05 01:05:04" 
* 为false时, 格式如 "2000-03-05" 
* @return 符合要求的日期字符串 
*/ 
function getSmpFormatDate(date, isFull) { 
var pattern = ""; 
if (isFull == true || isFull == undefined) { 
pattern = "yyyy-MM-dd hh:mm:ss"; 
} else { 
pattern = "yyyy-MM-dd"; 

return getFormatDate(date, pattern); 

/** 
*转换当前日期对象为日期字符串 
* @param date 日期对象 
* @param isFull 是否为完整的日期数据, 
* 为true时, 格式如"2000-03-05 01:05:04" 
* 为false时, 格式如 "2000-03-05" 
* @return 符合要求的日期字符串 
*/ 
function getSmpFormatNowDate(isFull) { 
return getSmpFormatDate(new Date(), isFull); 

/** 
*转换long值为日期字符串 
* @param l long值 
* @param isFull 是否为完整的日期数据, 
* 为true时, 格式如"2000-03-05 01:05:04" 
* 为false时, 格式如 "2000-03-05" 
* @return 符合要求的日期字符串 
*/ 
function getSmpFormatDateByLong(l, isFull) { 
return getSmpFormatDate(new Date(l), isFull); 

/** 
*转换long值为日期字符串 
* @param l long值 
* @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss 
* @return 符合要求的日期字符串 
*/ 
function getFormatDateByLong(l, pattern) { 
return getFormatDate(new Date(l), pattern); 

/** 
*转换日期对象为日期字符串 
* @param l long值 
* @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss 
* @return 符合要求的日期字符串 
*/ 
function getFormatDate(date, pattern) { 
if (date == undefined) { 
date = new Date(); 

if (pattern == undefined) { 
pattern = "yyyy-MM-dd hh:mm:ss"; 

return date.format(pattern); 

//alert(getSmpFormatDateByLong(1279829423000,false));

第4个回答  推荐于2017-09-04
将时间戳转换成日期格式:
// 简单的一句代码
var date = new Date(时间戳); //获取一个时间对象 注意:如果是uinx时间戳记得乘于1000。比如php函数time()获得的时间戳就要乘于1000

/*----------下面是获取时间日期的方法,需要什么样的格式自己拼接起来就好了----------*/
date.getFullYear();//获取完整的年份(4位,1970)
date.getMonth();//获取月份(0-11,0代表1月,用的时候记得加上1)
date.getDate();//获取日(1-31)
date.getTime();//获取时间(从1970.1.1开始的毫秒数)
date.getHours();//获取小时数(0-23)
date.getMinutes();//获取分钟数(0-59)
date.getSeconds();//获取秒数(0-59)
//更多好用的方法可以到这查找 -> http://www.w3school.com.cn/jsref/jsref_obj_date.asp

// 例子,比如需要这样的格式:yyyy-MM-dd hh:mm:ss
var date = new Date(1398250549490);
Y = date.getFullYear() + '-';
M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
D = date.getDate() + ' ';
h = date.getHours() + ':';
m = date.getMinutes() + ':';
s = date.getSeconds();
console.log(Y+M+D+h+m+s); //呀麻碟

//输出结果:2014-04-23 18:55:49

将日期格式转换成时间戳:
// 也很简单
date = new Date('2014-04-23 18:55:49:123'); //传入一个时间格式,如果不传入就是获取现在的时间了,就这么简单。
// 有三种方式获取
time1 = date.getTime()
time2 = date.valueOf()
time3 = Date.parse(date)

// 三种获取的区别
第一、第二种:会精确到毫秒
第三种:只能精确到秒,毫秒将用0来代替
// 比如上面代码输出的结果(一眼就能看出区别):
// 1398250549123
// 1398250549123
// 1398250549000

注意:获取到的时间戳除于1000就可以获得unix的时间戳了,在传值给PHP时用得到。本回答被提问者和网友采纳
相似回答