-1

What is the best way to convert YYYY-MM-DDTHH:mm:ss±hh:mm(string) to YYYYMMDDHHmmss(string) using regex or any other way?

For eg: 2020-02-27T01:23:44-05:30 to 20200227012344

Can someone help me here?

joy08
  • 7,650
  • 5
  • 32
  • 63
santhosh
  • 36
  • 8

2 Answers2

-1

Fastest way for me. Relies on constant size of input string.

const input = "2020-02-27T01:23:44-05:30"
const input2 = "2020-02-27T01:23:44+05:30"

const testOutput ="20200227012344"

const transformTime = time => time
     .split('-')
     .join('')
     .split('+')
     .join('')
     .split(':')
     .join('')
     .split('T')
     .join('')
     .substring(0, 14)

console.log(transformTime(input) === testOutput)
console.log(transformTime(input2) === testOutput)
Josh Wulf
  • 4,482
  • 2
  • 19
  • 33
-1

You could try this regex

(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\+(\d{2}):(\d{2})
Jonathan Sigg
  • 111
  • 2
  • 10