Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get todays date in Javascript

Tags:

javascript

How can I get the current date in Javascript in this format?

"M/D/YYYY"?

Thanks.

If this would be today it would be

"2/17/2011", if it was the 3rd it would be "2/3/2011".

Thanks

like image 558
slandau Avatar asked Feb 17 '11 20:02

slandau


People also ask

How can I get today date?

Code To Get Today's date in any specific FormatgetTime(); String todaysdate = dateFormat. format(date); System. out. println("Today's date : " + todaysdate);

How can get today date in HTML?

Use the JavaScript Date() Function to Get the Current Date in HTML. We can use the JavaScript Date() function to get the current date. The function returns the current date and time. We can create an HTML container and display the current date in it using JavaScript.

What is new date () in JavaScript?

It is used to work with dates and times. The Date object is created by using new keyword, i.e. new Date(). The Date object can be used date and time in terms of millisecond precision within 100 million days before or after 1/1/1970.


2 Answers

var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
document.write(month + "/" + day + "/" + year)

I assigned each part into its own variable for this example so that it's more clear as to what it returns.

like image 166
The Muffin Man Avatar answered Sep 23 '22 06:09

The Muffin Man


Use the javascript Date object:

var d = new Date();
alert((d.getMonth() + 1) + '/' + d.getDate() + '/' + d.getFullYear());
like image 33
Chris Baker Avatar answered Sep 24 '22 06:09

Chris Baker