Home > Web Front-end > JS Tutorial > Example code for timestamp and time format exchange

Example code for timestamp and time format exchange

零下一度
Release: 2017-06-24 14:35:40
Original
1281 people have browsed it
// 时间戳转化为时间
        Date.prototype.format = function(format) {
            var date = {
                "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+)/i.test(format)) {
                format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
            }
            for (var k in date) {
                if (new RegExp("(" + k + ")").test(format)) {
                    format = format.replace(RegExp.$1, RegExp.$1.length == 1
                        ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
                }
            }
            return format;
        };
Copy after login

使用:

var effStartDate ="1494565993";//你的时间戳
Copy after login
var date= new Date();
date.setTime(effStartDate);
effStartDate = date.format('yyyy-MM-dd h:m:s'); //转化后的时间
Copy after login

 

时间转为时间戳:

var stringTime = "2017-06-16 12:21:12";var timestamp2 = Date.parse(new Date(stringTime));
timestamp2 = timestamp2 / 1000; //如果前后端是按照毫秒为单位,那么不用除以1000
console.log(timestamp2);//转化后的时间戳
Copy after login

 

The above is the detailed content of Example code for timestamp and time format exchange. For more information, please follow other related articles on the PHP Chinese website!

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