javascript - How to convert seconds to date in js?

WBOY
Release: 2016-08-20 09:04:08
Original
1039 people have browsed it

现在有一个情况:后台返回给我一个1970年1月1日到现在的秒数,然后我需要将秒数转换成年月日,自己想了些方法都觉得算起来太复杂麻烦了,想问问大家有没有什么好点的方法或者思路?

回复内容:

现在有一个情况:后台返回给我一个1970年1月1日到现在的秒数,然后我需要将秒数转换成年月日,自己想了些方法都觉得算起来太复杂麻烦了,想问问大家有没有什么好点的方法或者思路?

直接代码

<code>Date.prototype.Format = function (fmt) { 
    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(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

//调用
new Date(time).Format("yyyy-MM-dd hh:mm");</code>
Copy after login

<code>var d = new Date(1463068800 * 1000);
alert(d.toISOString());</code>
Copy after login

1463068800unix 时间戳。用上面的方法就可以直接转换为 js 中的时间。

var d = new Date()
d.setTime(seconds * 1000);
var year = d.getFullYear();
var ...
可以获得年月日等你想要的一切

直接用moment.js 吧,简单易懂 http://momentjs.cn/

使用moment.js

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!