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
410 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,680 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
309 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
311 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
246 views
0 votes
0 answers

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

Usually I would expect a String.contains() method, but there ...READ MORE

Sep 13, 2023 in Java by Rahul
• 200 points
192 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
447 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,680 points
4,282 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
• 9,060 points
6,732 views
0 votes
0 answers

How do I format a date in JavaScript?

May 7, 2022 in Java-Script by narikkadan
• 63,420 points
249 views
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,680 points
397 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,680 points
331 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