0

I have a Date Time string 2018-08-10T14:33:44.000Z . I want to convert it into, 2018-08-10 14:33:44 by using Javascript.

I can convert it to UTC or Local time. But, I am looking for that specific format only.

Dinesh Ahuja
  • 925
  • 3
  • 15
  • 29
  • 1
    Possible duplicate of [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – Obsidian Age Aug 16 '18 at 23:51

2 Answers2

1

Luckily, since your input is a datetime string, you can transform the string easily with a regular expression, no need to mess with date manipulation at all. Match the T, capture the following time string in a group, then replace the T and everything that follows with a space followed by that group:

const input = '2018-08-10T14:33:44.000Z'
console.log(
  input.replace(/T((\d{2}:){2}\d\d).+/, ' $1')
);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1

Use the 'moment' javascript library. Then you can do something like:

moment(new Date()).format("YYYY-MM-DD HH:mm:ss")
Carlo Bos
  • 3,105
  • 2
  • 16
  • 29