Answers for "javascript difference between two dates in days, hours and minutes"

3

showing difference between dates in minutes js

const today = new Date();
const endDate = new Date(startDate.setDate(startDate.getDate() + 7));
const days = parseInt((endDate - today) / (1000 * 60 * 60 * 24));
const hours = parseInt(Math.abs(endDate - today) / (1000 * 60 * 60) % 24);
const minutes = parseInt(Math.abs(endDate.getTime() - today.getTime()) / (1000 * 60) % 60);
const seconds = parseInt(Math.abs(endDate.getTime() - today.getTime()) / (1000) % 60);
Posted by: Guest on November-06-2020
5

javascript get hours difference between two dates

// date1 and date2 are date objects

let hours = Math.abs(date1 - date2) / 36e5;

// The subtraction returns the difference between the two dates in milliseconds.
// 36e5 is the scientific notation for 60*60*1000, dividing by which converts
// the milliseconds difference into hours.
Posted by: Guest on January-12-2021
1

javascript get remaining time from start and end datetime

function calculateExamRemainingTime(exam_end_at) {

    $(function(){

        const calcNewYear = setInterval(function(){

            const exam_ending_at    = new Date(exam_end_at);
            const current_time      = new Date();
           
            const totalSeconds     = Math.floor((exam_ending_at - (current_time))/1000);;
            const totalMinutes     = Math.floor(totalSeconds/60);
            const totalHours       = Math.floor(totalMinutes/60);
            const totalDays        = Math.floor(totalHours/24);

            const hours   = totalHours - ( totalDays * 24 );
            const minutes = totalMinutes - ( totalDays * 24 * 60 ) - ( hours * 60 );
            const seconds = totalSeconds - ( totalDays * 24 * 60 * 60 ) - ( hours * 60 * 60 ) - ( minutes * 60 );

            const examRemainingHoursSection = document.querySelector('#remainingHours');
            const examRemainingMinutesSection = document.querySelector('#remainingMinutes');
            const examRemainingSecondsSection = document.querySelector('#remainingSeconds');

            examRemainingHoursSection.innerHTML = hours.toString();
            examRemainingMinutesSection.innerHTML = minutes.toString();
            examRemainingSecondsSection.innerHTML = seconds.toString();

        },1000);
    });
}

calculateExamRemainingTime('2025-06-03 20:20:20');
Posted by: Guest on June-28-2020
2

javascript difference between two dates in days

const diffDays = (date, otherDate) => Math.ceil(Math.abs(date - otherDate) / (1000 * 60 * 60 * 24));

// Example
diffDays(new Date('2014-12-19'), new Date('2020-01-01'));   // 1839
Posted by: Guest on July-03-2020
2

javascript difference between two dates in days

/* difference between date1 and date2 in days (date2 - date1) */
/* date1 and date 2 are already javascript date objects */
function dateDifference(date2, date1) {
    const _MS_PER_DAY = 1000 * 60 * 60 * 24;

    // Discard the time and time-zone information.
    const utc1 = Date.UTC(date1.getFullYear(), date1.getMonth(), date1.getDate());
    const utc2 = Date.UTC(date2.getFullYear(), date2.getMonth(), date2.getDate());

    return Math.floor((utc2 - utc1) / _MS_PER_DAY);
}
Posted by: Guest on July-16-2020
-3

calculate time difference between two dates in javascript

Gettings diffrence between daytes
Posted by: Guest on May-26-2021

Code answers related to "javascript difference between two dates in days, hours and minutes"

Code answers related to "Javascript"

Browse Popular Code Answers by Language