How to get the exact local time of client? How to get the exact local time of client? jquery jquery

How to get the exact local time of client?


In JavaScript? Just instantiate a new Date object

var now = new Date();

That will create a new Date object with the client's local time.


Nowadays you can get correct timezone of a user having just one line of code:

const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;

source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/resolvedOptions

You can then use moment-timezone to parse timezone like:

const currentTime = moment().tz(timezone).format();


Try

let s= new Date().toLocaleString();console.log(s);