欢迎!从2017年开始,将慢慢的不在xmind上写总结了,全部转到博客中!这里将不再随便写写,将继承在xmind的精神,继续前行!!!

时间 函数使用问题

简单记录

1、当前时间换时间戳

var timestamp = parseInt(new Date().getTime()/1000);    // 当前时间戳
console.log(timestamp)
//1553762487 生成10位的 

推荐使用

console.log(new Date().getTime())

2、时间戳转 时间 

关键是 new Date(时间戳);  直接传入毫秒数作为参数,给Date对象就可以得到普通的时间了,然后通过getHours,getFullYear等方法获取年月日,时分秒了

var oldTime = (new Date("2011/11/11 20:10:10")).getTime(); //得到毫秒数
var newTime = new Date(oldTime); //就得到普通的时间了

 

记录一个常用的 转化就好

format : function (fmt, date) {
            fmt = (fmt || '') + '';
            date = new Date(date || new Date());
            var map = {};
            map['M+'] = date.getMonth() + 1;
            map['d+'] = date.getDate();
            map['h+'] = date.getHours();
            map['m+'] = date.getMinutes();
            map['s+'] = date.getSeconds();
            map['q+'] = Math.floor((date.getMonth() + 3) / 3);
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
            if (/(W+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, ['日', '一', '二', '三', '四', '五', '六'][date.getDay()]);
            if (/(S+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, date.getMilliseconds() + '');
            for (var key in map)
                if (new RegExp('(' + key + ')').test(fmt))
                    fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? map[key] : ('00' + map[key]).substr(('' + map[key]).length));
            return fmt;
            //wfy.format('yyyy-MM-dd hh-mm-ss',new Date())
        },

//

 

在此遇到一个坑 忽略了问题所在,所以记录下

console.log(wfy.format('yyyy-MM-dd',new Date(parseInt("1553760490788"))))

console.log(wfy.format('yyyy-MM-dd',new Date("1553760490788")))

console.log(wfy.format('yyyy-MM-dd',new Date(1553760490788)))

//上述 三个使用! 第二个转换失败!!关键的在与 传入的值 需要是number 型

3、其他问题

3.1

new Date('2016-04-12').getTime();
// 1460419200000
 
new Date(1460419200000);
// Tue Apr 12 2016 08:00:00 GMT+0800

new Date(new Date('2016/04/12').getTime());
// Tue Apr 12 2016 00:00:00 GMT+0800

//用“2016-04-12”转出来的时间戳是 2016-04-12 08:00的时间点,而不是0点。
//将日期格式换成“2016/04/12”,则正常换算成0点。


3.2

Date()

  作为一个函数,Date对象可以直接调用,返回一个当前日期和时间的字符串。

Date()
// "Tue Dec 01 2015 09:34:43 GMT+0800 (CST)"
 
Date(2000, 1, 1)
// "Tue Dec 01 2015 09:34:43 GMT+0800 (CST)"

  上面代码说明,无论有没有参数,直接调用Date总是返回当前时间。

new Date()

  Date对象还是一个构造函数,对它使用new命令,会返回一个Date对象的实例。如果不加参数,生成的就是代表当前时间的对象。

 

 

 

333

posted @ 2019-03-28 17:03  拐进web的奋斗者  阅读(155)  评论(0编辑  收藏  举报