首页 > web前端 > js教程 > 正文

时间戳与时间格式互换的实例代码

零下一度
发布: 2017-06-24 14:35:40
原创
1234 人浏览过
// 时间戳转化为时间
        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;
        };
登录后复制

使用:

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

 

时间转为时间戳:

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

 

以上是时间戳与时间格式互换的实例代码的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!