skip to Main Content

I want to get the client machine local time.

I have to covert a specific UTC time to user’s local time.

2/10/2019 12:00:00 AM –>> I have to convert this to user’s time

I want to back the time to user’s time.
Can tou please conver the time to user’s time

2

Answers


  1. You can use either getTimezoneOffset or toLocaleString or toLocaleDateString.

    const d = new Date('2/10/2019 12:00:00 AM');
    const clientDate = d.toLocaleString();
    const offset = d.getTimezoneOffset(); // offset in minutes
    console.log({clientDate, offset});
    Login or Signup to reply.
  2. Hi you can use Date constructor of JS
    if you want to get time for the current TimeZone you can use

    const time = new Date().toUTCString();
    

    or if you want to date according to some locale and time zone you can do something like this:

    const locale = "en-US"; 
    const  tz ="Australia/Sydney"
    const current = new Date();
    const options = {
      timeZone: tz,
      hour12: false,
      hour: "numeric",
      minute: "numeric",
      second: "numeric"
    };
    
    const formatter = new Intl.DateTimeFormat(locale, options);
    const localTime = formatter.format(new Date());
    

    Can read more about Date and Intl on MDN docs

    INTL -https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl

    Date – https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

    Hope it helps

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