1

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.

2
  • Please proofread your question title. Commented Jun 20, 2016 at 5:17
  • Dates don't have timezones. Timezone is an aspect of how a date is represented. For more information, read w3c.github.io/i18n-drafts/articles/definitions-time/…. Commented Jun 20, 2016 at 5:18

2 Answers 2

2

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(...)

Sign up to request clarification or add additional context in comments.

3 Comments

If you construct new date, it will use time zone from the client. Actually, this is incorrect. You probably mean that if you convert a date to a string, ti will use the timezone from the client to represent it.
No, I mean't that it will construct date according to client time zone. From msdn: "the constructor creates a JavaScript Date object for the current date and time according to system settings". What Date actually represents, is number of milliseconds since 1 January 1970 00:00:00 UTC . So if time zone on your PC is +2 it will construct Date with 2*60*60*1000 more miliseconds.
That is also how you can check the local timezone on the client, you construct the new date and get it's offset from UTC time: new Date().getTimezoneOffset() (see more here: link )
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.