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 in Java by Rahul
• 8,980 points
22 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 by Aditya
• 7,280 points

Related Questions In Java

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,170 points
1,014 views
0 votes
1 answer

How to divide a string in two parts

String s="yourstring"; boolean flag = true; for(int i=0;i<s.length();i++) { ...READ MORE

answered Apr 13, 2018 in Java by Rishabh
• 3,620 points
418 views
0 votes
3 answers

How to reverse a string in java?

public static String reverse(String s) { ...READ MORE

answered Aug 17, 2018 in Java by samarth295
• 2,220 points
439 views
0 votes
3 answers

Convert date object to a String

We parse the full date to time ...READ MORE

answered Jul 31, 2018 in Java by samarth295
• 2,220 points
705 views
0 votes
1 answer

Compare two dates with JavaScript

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

answered Feb 18 in Java by Rahul
• 8,980 points
21 views
0 votes
1 answer

Format JavaScript date as yyyy-mm-dd

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

answered Feb 23 in Java-Script by Aditya
• 7,280 points
143 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 in Tableau by Neha
• 6,260 points
84 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 in Java by Aditya
• 7,280 points
21 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 in Java by Aditya
• 7,280 points
55 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP