1

The below code gave me IST time date

const x=new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '')

Output: 2020-09-01 17:49:48

I want to get IST time instead of UTC which is

2020-09-01 23:20:48

How to get above output in node js.

imsaiful
  • 1,556
  • 4
  • 26
  • 49

2 Answers2

4
  • Consider using this:
new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
  • give correct date time but format is different. Can you help to get format mentioned in the question? – imsaiful Sep 01 '20 at 18:04
  • You can convert it to 24 hr format using `new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata', hour12: false})` . After that treat it as a string and format it the way you want. – Anubhav Ujjawal Sep 01 '20 at 18:13
  • time is fine now thanks but date is creating issue. Need in format YYYY:MM:DD HH:MM:SS. – imsaiful Sep 01 '20 at 18:16
0

Install moment package and write the following two lines code to solve the problem:

var moment = require('moment');
console.log(moment().format("YYYY-MM-DD HH:mm:ss"));
imsaiful
  • 1,556
  • 4
  • 26
  • 49