我想用天、小时、分钟、秒、毫秒、纳秒来计算日期差异。我该怎么做呢?


当前回答

我做了一个下面的函数来得到现在和“2021-02-26T21:50:42.123”之间的区别。

差值返回以毫秒为单位的答案,所以我使用以下公式进行转换:

(1000 * 3600 * 24).

function getDiff(dateAcquired) {
      let calDiff = Math.floor(
        (new Date() - new Date(dateAcquired)) / (1000 * 3600 * 24)
      );
      return calDiff;
    }
    console.log(getDiff("2021-02-26T21:50:42.123"));

其他回答

这段代码将返回两个日期的差值(以天为单位):

const previous_date = new Date("2019-12-23");
const current_date = new Date();

const current_year = current_date.getFullYear();
const previous_date_year = 
previous_date.getFullYear();

const difference_in_years = current_year - 
previous_date_year;

let months = current_date.getMonth();
months = months + 1; // for making the indexing 
// of months from 1

for(let i = 0; i < difference_in_years; i++){
months = months + 12;
}

let days = current_date.getDate();

days = days + (months * 30.417);

console.log(`The days between ${current_date} and 
${previous_date} are : ${days} (approximately)`);

另一种解决方案是将difference转换为一个新的Date对象,并获得该日期的年(与1970年不同)、月、日等。

var date1 = new Date(2010, 6, 17);
var date2 = new Date(2013, 12, 18);
var diff = new Date(date2.getTime() - date1.getTime());
// diff is: Thu Jul 05 1973 04:00:00 GMT+0300 (EEST)

console.log(diff.getUTCFullYear() - 1970); // Gives difference as year
// 3

console.log(diff.getUTCMonth()); // Gives month count of difference
// 6

console.log(diff.getUTCDate() - 1); // Gives day count of difference
// 4

所以差异就像“3年6个月零4天”。如果你想以一种人类可读的风格呈现不同,这将对你有所帮助。

            // the idea is to get time left for new year.
           // Not considering milliseconds as of now, but that 
           //  can be done
           
            var newYear = '1 Jan 2023';
            const secondsInAMin = 60;
            const secondsInAnHour = 60 * secondsInAMin;
            const secondsInADay = 24 * secondsInAnHour;

            function DateDiffJs() {
                var newYearDate = new Date(newYear);
                var currDate = new Date();

                var remainingSecondsInDateDiff = (newYearDate - currDate) / 1000;
                var days = Math.floor(remainingSecondsInDateDiff / secondsInADay);

                var remainingSecondsAfterDays = remainingSecondsInDateDiff - (days * secondsInADay);
                var hours = Math.floor(remainingSecondsAfterDays / secondsInAnHour);

                var remainingSecondsAfterhours = remainingSecondsAfterDays - (hours * secondsInAnHour);
                var mins = Math.floor(remainingSecondsAfterhours / secondsInAMin);

                var seconds = Math.floor(remainingSecondsAfterhours - (mins * secondsInAMin));


                console.log(`days :: ${days}`)
                console.log(`hours :: ${hours}`)
                console.log(`mins :: ${mins}`)
                console.log(`seconds :: ${seconds}`)

            }

            DateDiffJs();

我做了一个下面的函数来得到现在和“2021-02-26T21:50:42.123”之间的区别。

差值返回以毫秒为单位的答案,所以我使用以下公式进行转换:

(1000 * 3600 * 24).

function getDiff(dateAcquired) {
      let calDiff = Math.floor(
        (new Date() - new Date(dateAcquired)) / (1000 * 3600 * 24)
      );
      return calDiff;
    }
    console.log(getDiff("2021-02-26T21:50:42.123"));
var d1=new Date(2011,0,1); // jan,1 2011
var d2=new Date(); // now

var diff=d2-d1,sign=diff<0?-1:1,milliseconds,seconds,minutes,hours,days;
diff/=sign; // or diff=Math.abs(diff);
diff=(diff-(milliseconds=diff%1000))/1000;
diff=(diff-(seconds=diff%60))/60;
diff=(diff-(minutes=diff%60))/60;
days=(diff-(hours=diff%24))/24;

console.info(sign===1?"Elapsed: ":"Remains: ",
             days+" days, ",
             hours+" hours, ",
             minutes+" minutes, ",
             seconds+" seconds, ",
             milliseconds+" milliseconds.");