Q:

Javascript date and time is showing wrong

I have developed a website , where end users are in Toronto . I have set timezone for them but it is showing wrong date. ahead one day from Toronto .
It shows correct time at some time but at the end of day it shows one day ahead time .. i don't know why is it doing it ?
function calcTime(city, offset) {

    // create Date object for current location
    d = new Date();

    utc = d.getTime() + (d.getTimezoneOffset() * 60000);

    // create new Date object for different city
    // using supplied offset
    nd = new Date(utc + (3600000*offset));

    // return time as a string
    return  nd;

}
function getdatetime() {
var date =calcTime('Toronto', '-8');

var currentDate = date.toISOString().slice(0,10);
var currentTime = date.getHours() + ':' + (date.getMinutes()<10?'0':'') + date.getMinutes() ; 

document.getElementById('datetime').value = (currentDate+" " + currentTime); 
}

Last Entries:
2020-01-16 16:44
2020-01-16 16:42    
2020-01-16 16:41    //from here
2020-01-15 15:25
2020-01-15 15:23
2020-01-15 15:22    

A:

Try the below code.

function getdatetime() {
    let timezone =  new Date().toLocaleString("en-US", {timeZone: "America/Toronto"});
    let date = new Date(timezone);
    
    var currentDate = date.toISOString().slice(0,10);
    var currentTime = date.getHours() + ':' + (date.getMinutes()<10?'0':'') + date.getMinutes() ; 
    console.log(date);
}

getdatetime();

