Foo
Foo

Reputation: 4606

Typescript convert string to datetime

In typescript how do you convert a string like this to datetime?

2019-01-19 14:00:12

I am not sure if this is the right way

new Date("2019-01-19 14:00:12")

I think there would be some way to specify the timezone in it.

Upvotes: 1

Views: 8376

Answers (2)

Waleed Ahmed
Waleed Ahmed

Reputation: 11

to convert from string to date then yo should to as

const date = new Date("2019-01-19 14:00:12");
console.log(date);

For more: Date Utils

Upvotes: 0

MarkoR
MarkoR

Reputation: 553

If you construct new date, it will use time zone from the client. See here for more info how to construct date: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date

You can parse your date string manually and construct new Date object with year,month,day,hour,minute,second values. Or you can use Date.Parse(...) function if your date string is in correct format, see more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

If you want UTC date you need to construct it with new Date.UTC(...)

Upvotes: 2

Related Questions