skip to Main Content

I am about the compare two dates in JavaScript with each other
for example consider this two value :

date1 = 2024-5-13T20:30:00
date2 = 2024-5-13T21:30:20

the day and month is the same but the hours is different
my goal is to return true for each day that have the same value as month and year and …

its should compare the date in the format yyyy/mm/dd

the method I used first time :

date1.toISOString().split('T')[0] === date2.toISOString().split('T')[0]

this method is not suitable for me because its loop between the characters of time string then split and its reduce the performance

3

Answers


  1. Cut time in UTC:

    const date1 = new Date('2024-05-13T23:30:00'), date2 = new Date('2024-05-14T00:30:20');
    
    const DAY_TIME = 3600 * 24 * 1000;
    const isSameDate =  (a, b) => (a = a.getTime(), b = b.getTime(), (a - a % DAY_TIME) === (b - b % DAY_TIME));
    console.log(date1.toLocaleString(), '-', date2.toLocaleString());
    console.log(isSameDate(date1, date2));

    Cut time in local timezone:

    const date1 = new Date('2024-05-13T23:30:00'), date2 = new Date('2024-05-14T00:30:20');
    
    const DAY_TIME = 3600 * 24 * 1000;
    const offset = date1.getTimezoneOffset() * 60000;
    const isSameDate =  (a, b) => (a = a.getTime() - offset, b = b.getTime() - offset, (a - a % DAY_TIME) === (b - b % DAY_TIME));
    console.log(date1.toLocaleString(), '-', date2.toLocaleString());
    console.log(isSameDate(date1, date2));

    Let’s benchmark:

    ` Chrome/124
    ----------------------------------------------------------
    cut time         ■ 1.00x | x1000000000 331 338 338 339 346
    Wimanicesir     3353.47x |     x100000 111 112 113 113 115
    DateTimeFormat  3806.65x |     x100000 126 127 129 130 132
    ----------------------------------------------------------
    https://github.com/silentmantra/benchmark `
    
    const date1 = new Date('2024-05-13T20:30:00'), date2 = new Date('2024-05-13T21:30:20');
    
    // @benchmark Wimanicesir
    const isSameDate0 = (a, b) => a.toLocaleDateString() === b.toLocaleDateString();
    // @run
    isSameDate0(date1, date2);
    
    // @benchmark DateTimeFormat
    const {format} = Intl.DateTimeFormat('en-US');
    const isSameDate1 = (a, b) => format(a) === format(b);
    // @run
    isSameDate1(date1, date2);
    
    // @benchmark cut time
    const DAY_TIME = 3600 * 24 * 1000;
    const isSameDate2 = (a, b) => (a = a.getTime(), b = b.getTime(), (a - a % DAY_TIME) === (b - b % DAY_TIME));
    // @run
    isSameDate2(date1, date2);
    
    /*@skip*/ fetch('https://cdn.jsdelivr.net/gh/silentmantra/benchmark/loader.js').then(r => r.text().then(eval));
    Login or Signup to reply.
  2. Using toLocaleDateString is a short way to check this, however Intl.DateTimeFormat is faster:

    const date1 = new Date('2024-05-13T20:30:00'), date2 = new Date('2024-05-13T21:30:20');
    
    const isSameDate = (a, b) => new Intl.DateTimeFormat('en-US').format(a) === new Intl.DateTimeFormat('en-US').format(b);
    
    console.log(isSameDate(date1, date2));
    Login or Signup to reply.
  3. var date1 = 2024-5-13T20:30:00
    var date2 = 2024-5-13T21:30:20
    
    var tempdate1 = new Date(date1);
    var tempdate2 = new Date(date2);
    
    // Extract the date parts
    var year1 = tempdate1 .getFullYear();
    var month1 = tempdate1 .getMonth() + 1;
    var day1 = tempdate1 .getDate();
    
    var year2 = tempdate2 .getFullYear();
    var month2 = tempdate2 .getMonth() + 1;
    var day2 = tempdate2 .getDate();
    
    if(year1 === year2 && month1 === month2 && day1 === day2){
    
    }
    

    Try this code. If you have any problem further, feel free to reach out me.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search