Getting difference in seconds from two dates in JavaScript

后端 未结 2 1909
时光说笑
时光说笑 2021-01-16 00:01

I have dates stored in MongoDB using Date(), MongoDB uses UTC it\'s date type, and as a string looks like Mon, 02 Apr 2012 20:16:31 GMT.

I want to get t

2条回答
  •  再見小時候
    2021-01-16 00:37

    You could use getTime() on the Date objects to get the difference in milliseconds then divide the difference by 1000;

    e.g.

    now = new Date();
    current_date = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
    end_date = obj.end_date (Mon, 02 Apr 2012 20:16:35 GMT);
    var millisDiff = end_date.getTime() -  current_date.getTime();
    console.log(millisDiff / 1000);
    

提交回复
热议问题