Pawan Kumar
Pawan Kumar

Reputation: 558

getting incorrect time while converting string into date object

I am using calling this function getFormattedTimeFromString(startTime)

getFormattedTimeFromString(timeString){
    return  (new Date('1970-01-01T' + timeString + 'Z'));
  }

Upvotes: 0

Views: 71

Answers (1)

Endless
Endless

Reputation: 37945

if you are passing in getFormattedTimeFromString("14:00:00") and getting Thu Jan 01 1970 19:30:00 GMT+0530 (IST) as Output well then that is expected...

What do you want it to be? Maybe you want to remove the Z? for it to be local?

the Thu Jan 01 1970 19:30:00 GMT+0530 (IST) is just a representation in your local timezone while i get Thu Jan 01 1970 15:00:00 GMT+0100 (CET) but it still being the same in UTC.

If you would do:

new Date(`Thu Jan 01 1970 19:30:00 GMT+0530 (IST)`).toJSON()
// you get same input back
"1970-01-01T14:00:00.000Z"

which is still the same input

You also have diffrent method on the date object like getUTCxxxx if that is what u want

Upvotes: 1

Related Questions