Skip to content

JavaScript date parse Method | Examples code

  • by

JavaScript Date parse() method parses a date string and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. But if the given string date is unrecognized or, in some cases, contains illegal date values then it will return NaN (Not-a-Number).

Syntax

Date.parse(dateString)

Parameter Values

A string representing a date.

Example of JavaScript date parse

Let’s see simple example, where will try to get the the number of milliseconds between January 1, 1970 and July 21, 2020:

<!DOCTYPE html> 
<html> 
    <body> 
    	
	<script>

		var d = Date.parse("July 21, 2020");
		alert(d);

</script>


    </body> 
</html>  

Output:

Example of JavaScript date parse

How to Javascript convert date format

You should look into momentjs, which is a javascript date/time library. By using this Library, you can easily convert between dates of different formats. In your case, it would be:

string newDate = moment(currentDate, currentFormatString).format(newFormatString)

For example, moment("21/10/14", "DD/MM/YY").format("MM/DD/YY") would return "10/21/14"

Q: What is the common Error javascript date parse method?

Answer: If the input string of date is not correct, it returns NaN (not a number). See below example of it:-

<script> 
  
    var d = "July 31, 2020 12:30 PM"; 

    var md = Date.parse(d); 
    document.write(md); 
      
</script> 

Output: NaN

Do comment if you have any doubts and suggestions on this topic.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version

Tags:

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading