Javascript get a date without time | Display Example

You can JavaScript display date without time, by fetching only YEAR, DATE, and Month data from a date object.

How to get current date without time in JavaScript?

Here is an example of Javascript get a date without time, you have to create a date object then fetch the day, month, and year and combine it as the format you want.

See below example of display date without time or remove time from date object.

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

		var d = new Date(); 
		var NoTimeDate = d.getFullYear()+'/'+(d.getMonth()+1)+'/'+d.getDate(); 
		alert(NoTimeDate);

</script>


    </body> 
</html>  

Output:

Javascript get a date without time

Another way to javascript remove time from date string

Use split method: Split it by space and take the first part like the below example code.

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

		var d = '12/12/1020 12:00:00 AM';; 
		d = d.split(' ')[0];
		console.log(d);

</script>


    </body> 
</html> 

Output: 12/12/1020

Q: How to Javascript compare dates without time?

Answer: By using operators you can compare dates in JS, see below example code of it:-

<!DOCTYPE html> 
<html> 
    <body> 
    	
<script> 
    // Current Date 
    var d1 = new Date('Aug 04 2020'); 
    var d2 = new Date('Aug 04 2020'); 
    if (d1.getTime() === d2.getTime()) 
        alert("Both  are equal"); 
    else
        document.write("Not equal");  
</script> 
 
 
    </body> 
</html>   

Output: Both are equal

Read More Examples:- JavaScript Date comparison | String, time, Operators

Do comment if you have any questions 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


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.