2

I am trying to get Indian current time and date and i tried this:

let today = new Date();
let indianTime = today.toLocaleString("en-US", "Asia/Delhi");

it returns:

"Mon Jul 26 2021 18:08:11 GMT+0530 (India Standard Time)"

but it'st not currect.

Ho can i get currect indian time?

  • 1
    by default, when you use `new Date()` in js, it will return the current date/time in your browser, and if you are trying this from India, you should not require any further operations, however, if you wish to fiddle with timezones, pls consider converting it to unix timestamps and then dealing with it, it would be more consistent and easier – tsamridh86 Jul 27 '21 at 04:26
  • 1
    Does this answer your question? [Convert date to another timezone in JavaScript](https://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript) – monesul haque Jul 27 '21 at 04:44

2 Answers2

5

Timezone "Asia/Delhi" is not a valid timezone, try "Asia/Kolkata" instead.

let currentdate = new Date();
let indian date = new Date().toLocaleString("en-Us", {timeZone: 'Asia/Kolkata'});
iftikharyk
  • 870
  • 5
  • 10
0

You could try the Intl.DateTimeFormat object. This gives you a lot of flexibility when formatting dates and/or times.

You can also specify a timezone to use when formatting. This should be an IANA timezone, in your case Asia/Kolkata.

const date = new Date()
const timeZone = 'Asia/Kolkata';

// Potential formatters to use to display date / time
const formatters = [
    new Intl.DateTimeFormat('en-US', { timeStyle: 'long', dateStyle: 'short', timeZone }),
    new Intl.DateTimeFormat('en-US', { timeStyle: 'medium', dateStyle: 'medium', timeZone }),
    new Intl.DateTimeFormat('en-US', { timeStyle: 'medium', dateStyle: 'medium', hour12: false, timeZone }),
    // Using an 'sv' locale will give an ISO-8601 output
    new Intl.DateTimeFormat('sv', { timeStyle: 'medium', dateStyle: 'short', timeZone }),
    new Intl.DateTimeFormat('en-US', { timeStyle: 'short', timeZone }),
    new Intl.DateTimeFormat('en-US', { timeStyle: 'short', hour12: false, timeZone })
]

formatters.forEach(fmt => console.log(fmt.format(date)))
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40