Home  >  Article  >  Backend Development  >  javascript - How to convert js timestamp to time format

javascript - How to convert js timestamp to time format

WBOY
WBOYOriginal
2016-09-12 17:44:471034browse

从数据库中取得值 时间戳是秒

javascript - How to convert js timestamp to time format
发现显示的都是1970年

回复内容:

从数据库中取得值 时间戳是秒

javascript - How to convert js timestamp to time format
发现显示的都是1970年

时间戳的单位是毫秒

如果obj.create_time是秒,那应该是obj.create_time * 1000

//设定时间格式化函数
    Date.prototype.format = function (format) {
        var args = {
            "M+": this.getMonth() + 1,
            "d+": this.getDate(),
            "h+": this.getHours(),
            "m+": this.getMinutes(),
            "s+": this.getSeconds(),
        };
        if (/(y+)/.test(format))
            format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var i in args) {
            var n = args[i];
            if (new RegExp("(" + i + ")").test(format))
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ("00" + n).substr(("" + n).length));
        }
        return format;
    };

使用:new Date(time).format("yyyy-MM-dd")

Statement:
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