时间格式的处理方法总结以及时间格式和时间戳之间的转换方式,以及如何计算日期相差的天数

 

1、Date()

 

——返回此刻的日期和时间。  

 

"Wed Aug 21 2019 10:07:20 GMT+0800 (中国标准时间)"

 

2、getDate()

 

——从 Date 对象返回一个月中的某一天 (1 ~ 31)。

 

new Date().getDate()  21(号)

 

3、getDay()  ——从 Date 对象返回一周中的某一天。

 

new Date().getDay()    返回3  即周三   

 

4、getMonth()——从 Date 对象返回月份 (0 ~ 11)。

 

new Date().getMonth() 7 此处是0-11,所以月份要记得+1处理

 

5、getFullYear()  ——从 Date 对象以四位数字返回年份。

 

new Date().getFullYear()    2019  

 

6、getYear()  ——请使用 getFullYear() 方法代替。

    119  

 

7、getHours()  ——返回 Date 对象的小时 (0 ~ 23)。

 

new Date().getHours()   9 对应当前的几点钟 ,和当前时钟数字吻合,故是否+1看具体情况而定

 

8、getMinutes()  ——返回 Date 对象的分钟 (0 ~ 59)。

 

new Date().getMinutes()   15 对应当前的几分钟 是否+1根据自己需要

例子:15分01秒就算第16分钟,但返回的分钟数为15,和当前时钟数字吻合,故是否+1看具体情况而定

 

9、getSeconds()——返回 Date 对象的秒数 (0 ~ 59)。

 new Date().getSeconds()  25

 

10、getMilliseconds()——返回 Date 对象的毫秒(0 ~ 999)

 

  new Date().getMilliseconds()   712

 

11、getTime() ——返回 1970 年 1 月 1 日至今的毫秒数。

 new Date().getTime()  1566353988564

 

12、 Date.parse(datestring(必需。表示日期和时间的字符串。))

 

parse() 方法可解析一个日期时间字符串,并返回 1970/1/1 午夜距离该日期时间的毫秒数。

该方法是 Date 对象的静态方法。一般采用 Date.parse() 的形式来调用,而不是通过 dateobject.parse() 调用该方法。

Date.parse(2019-07-06)     1136073600000(单位:毫秒)

 

13、将毫秒数转换为多少年(非具体日期)

var minutes = 1000 * 60  --一分钟多少毫秒

var hours = minutes * 60  --一小时多少毫秒

var days = hours * 24  --一天多少毫秒

var years = days * 365  --一年365天多少毫秒

var t = Date.parse("Jul 8, 2019")  --获取指定日期的毫秒数

var y = t/years  --计算得出多少年

 

14、获取当前时间的时分秒

 

new Date().toTimeString();   "10:34:54 GMT+0800 (中国标准时间)"

 

15、 获取当前日期,toLocaleString()根据本地时间格式,把 Date 对象转换为字符串。

 

new Date().toLocaleString();    

"2019/8/21 上午10:38:11"

 

new Date().toLocaleTimeString()  ---根据本地时间格式,把 Date 对象的时间部分转换为字符串。;

"上午10:39:25"

 

new Date().toLocaleDateString();

"2019/8/21"  根据本地时间格式,把 Date 对象的日期部分转换为字符串。

 

16.将时间戳转换成日期格式(//时间戳若为10位需*1000,时间戳为13位的话不需乘1000);

 

function timestampTodateformat(timestamp) {

        var date = new Date(timestamp * 1000);

        var Y = date.getFullYear() + '-';

        var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';

        var D = date.getDate() + ' ';

        var h = date.getHours() + ':';

        var m = date.getMinutes() + ':';

        var s = date.getSeconds();

        return Y+M+D+h+m+s;

    }

    timestampTodateformat(时间戳);

    console.log(timestampTodateformat(时间戳));

 

17. 将日期格式转换成时间戳:

 

var date = new Date('2019-08-21 12:30:36:12');

    // 有三种方式获取

    var time1 = date.getTime();

    var time2 = date.valueOf();

    var time3 = Date.parse(date);

console.log(time1,time2 ,time3 );

 

18.计算两个日期相差多少天

原理:现将日期转换为毫秒格式,相减后得到两个日期之间时间的差值,然后换算为具体的天数

 function DateMinus(date1,date2){

//date1:小日期   date2:大日期

 

   var sdate = new Date(date1); 

 

  var now = new Date(date2); 

 

       var days = now.getTime() - sdate.getTime();

 

      var day = parseInt(days / (1000 * 60 * 60 * 24)); 

 

  return day; 

 

 }

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值