Parsing a string to a date in JavaScript

0 votes

How can I convert a string to a Date object in JavaScript?

var st = "date in some format" var dt = new Date(); var dt_st = // st in Date format, same as dt.

Feb 18, 2022 in Java by Rahul
• 9,670 points
146 views

1 answer to this question.

0 votes
The best string format for string parsing is the date ISO format together with the JavaScript Date object constructor.

Examples of ISO format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS.

But however, just using the "ISO format" doesn't work reliably by itself. String are sometimes parsed as UTC and sometimes as localtime. The best practice should always be to store dates as UTC and make computations as UTC.

To parse a date as UTC, append a Z - e.g.: new Date('2011-04-11T10:20:30Z').

To display a date in UTC, use .toUTCString(),

To display a date in the user's local time, use .toString().
answered Feb 18, 2022 by Aditya
• 7,660 points

Related Questions In Java

0 votes
0 answers

How to check whether a string contains a substring in JavaScript?

There doesn't seem to be a String.contains() ...READ MORE

Sep 21, 2022 in Java by Nicholas
• 7,760 points
109 views
0 votes
0 answers

How to convert a string to an integer in JavaScript?

In JavaScript, how do I convert a ...READ MORE

Sep 22, 2022 in Java by Nicholas
• 7,760 points
109 views
0 votes
0 answers

What's the best way to convert a number to a string in JavaScript?

What method is the "best" for convert ...READ MORE

Nov 8, 2022 in Java by Nicholas
• 7,760 points
52 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,210 points
1,331 views
0 votes
1 answer

Compare two dates with JavaScript

 The Date object will do what you ...READ MORE

answered Feb 18, 2022 in Java by Rahul
• 9,670 points
153 views
0 votes
1 answer

Format JavaScript date as yyyy-mm-dd

You could try using the following lines ...READ MORE

answered Feb 23, 2022 in Java-Script by Aditya
• 7,660 points
3,290 views
0 votes
1 answer

Convert String to Date in Tableau using DATEPARSE

You should be able to make the ...READ MORE

answered Mar 28, 2022 in Tableau by Neha
• 8,940 points
1,731 views
0 votes
0 answers
0 votes
1 answer

How do I get the current date in JavaScript?

To ensure that you get the current ...READ MORE

answered Feb 18, 2022 in Java by Aditya
• 7,660 points
115 views
0 votes
1 answer

How to merge two arrays in JavaScript and de-duplicate items?

With Underscore.js or Lo-Dash you can use: console.log(_.union([1, ...READ MORE

answered Feb 18, 2022 in Java by Aditya
• 7,660 points
152 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP