Meena Patel
Meena Patel

Reputation: 147

Date Difference in JavaScript with Timezone

I am very new in javascript and trying get day difference with set timezone. My script is like below

let localCurrentDate = new Date();
let booking_date = '06/11/2020';
var inputdate = booking_date + ' ' + '00:00:00 UTC+2';
const oneDay = 24 * 60 * 60 * 1000;
// date eg; "006/15/2020 00:00:00 UTC+2"
var utc2Date = inputdate;
let parsedDate = new Date(utc2Date);
//let currentDate = new Date();
let curDate = new Date(localCurrentDate.toLocaleString("en-US", {
  timeZone: "Europe/Rome"
}));
let diffTime = Math.abs(curDate - parsedDate);
let diffDays = Math.ceil(diffTime / oneDay);
//return diffDays;
console.log("input date" + parsedDate);
console.log("current date" + curDate);
console.log("day difference: " +diffDays);

But since I am setting time 00:00:00 with my input date, and current time getting current time, so difference is coming +1 day . Can I know how I can get day difference with void time? or how I can set time as 00:00:00 with getting current date? I want set and use timezone with it. Thanks!

Upvotes: 1

Views: 1722

Answers (4)

dezman
dezman

Reputation: 19388

Typescript version (in milliseconds):

export const timeDiffMs = (
  timeA: Date | string,
  timeB: Date | string
): number => {
  timeA = new Date(
    (typeof timeA === "Date" ? timeA : new Date(timeA)).toUTCString()
  );
  timeB = new Date(
    (typeof timeB === "Date" ? timeB : new Date(timeB)).toUTCString()
  );

  return timeA.getTime() - timeB.getTime();
};

Upvotes: 0

Siddharth Rathod
Siddharth Rathod

Reputation: 638

            let currDate = new Date();
            let bookingDate = window.document.getElementById("bookingDate");
            let olddate = bookingDate.value;
            
            let isChanged = function(){
                if(bookingDate.value!== olddate){
                    olddate = bookingDate.value;
                    return true;
                };
                return false;
            };
            
            bookingDate.addEventListener("blur", function(){
                
              if(isChanged()){
                    
                    bDate = new Date(bookingDate.value);
                    
                    let diffTime = Math.abs(bDate - currDate);
                    let diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 

                    alert(diffDays + " days");
              }
                
            });
<html>
    <head>
        <title>Date Difference in JavaScript with Timezone</title>
    </head>
    <body>
        
        <h3>Select Date</h3>
        <input type="date" id="bookingDate" >
        <input type="button" value="Get Date Difference" >
        
    </body>
</html>

Upvotes: -1

ABGR
ABGR

Reputation: 5235

You just have to adjust the dates to getTimezoneOffset and then compare.

So let's say you want to compare Date A with time zone Indian Standard Time (+ 5:30) with another date Date B with different time zone, say with (+ 2:30).

What you need to do here is bring both of these dates to their UTC date and time and then compare. getTimezoneOffset() will give you the minutes past the UTC time. If you set this in the minutes you'll have normalise the date to UTC date and time. If you set both the dates with getTimezoneOffset(), you'll be able to compare them easily.

var d = new Date();

d.setMinutes(d.getTimezoneOffset());

simlarly,

inputDate.setMinutes(inputDate.getTimezoneOffset())

let localCurrentDate = new Date();

let curDate = new Date(localCurrentDate.toLocaleString("en-US", {
  timeZone: "Europe/Rome"
}));

curDate.setMinutes(curDate.getTimezoneOffset());
localCurrentDate.setMinutes(localCurrentDate.getTimezoneOffset());

var diff = Math.abs(localCurrentDate - curDate);
const oneDay = 24 * 60 * 60 * 1000;
   
console.log(diff/oneDay)

Upvotes: 1

Karan
Karan

Reputation: 12629

Try converting your inputdate to UTC date and then use new Date() to get inputdate object to get in current timezone. Then you can get difference.

Updated code let parsedDate = new Date((new Date(utc2Date)).toUTCString());.

Check it below.

let localCurrentDate = new Date();
let booking_date = '2020-06-12';
var inputdate = booking_date + ' ' + '00:00:00 UTC+2';
const oneDay = 24 * 60 * 60 * 1000;
// date eg; "006/15/2020 00:00:00 UTC+2"
var utc2Date = inputdate;
let parsedDate = new Date((new Date(utc2Date)).toUTCString());
//let currentDate = new Date();
let curDate = new Date();
let diffTime = Math.abs(curDate - parsedDate);
let diffDays = Math.ceil(diffTime / oneDay);
//return diffDays;
console.log("input date" + parsedDate);
console.log("current date" + curDate);
console.log("day difference: " +diffDays);

In case if you want to get difference from date only without considering time (set time to 00:00:00.000) then use date.setHours(0,0,0,0);.

Here is the code snippet.

let localCurrentDate = new Date();
let booking_date = '2020-06-12';
var inputdate = booking_date + ' ' + '00:00:00 UTC+2';
const oneDay = 24 * 60 * 60 * 1000;
// date eg; "006/15/2020 00:00:00 UTC+2"
var utc2Date = inputdate;
let parsedDate = new Date((new Date(utc2Date)).toUTCString());
//let currentDate = new Date();
let curDate = new Date();
curDate.setHours(0,0,0,0);   // set time to 00:00:00.000
parsedDate.setHours(0,0,0,0); // set time to 00:00:00.000
let diffTime = Math.abs(curDate - parsedDate);
let diffDays = Math.ceil(diffTime / oneDay);
//return diffDays;
console.log("input date" + parsedDate);
console.log("current date" + curDate);
console.log("day difference: " +diffDays);

Upvotes: 1

Related Questions