JavaScript Date Object
Mozilla Documentation on toLocalDateString
this code will give you the date in the format mm/dd/yyyy
(at least that is what it gave me, because that is my locale convention)
var today = new Date();
document.write(today.toLocaleDateString());
Note: var today = new Date();
is a Date object that has ToString
Methods for displaying it's properties
if your local conventions are to give you yyyy/mm/dd
then it should give you what you want. as w3schools says
toLocaleDateString
--> Returns the date portion of a Date object as a string, using locale conventions
Code and Result of another answer doesn't look right to me
var today = new Date();
today.setHours(0); // or today.toUTCString(0) due to timezone differences
today.setMinutes(0);
today.setSeconds(0); document.write(today);
This code gives the result of
Thu Sep 19 2013 00:00:00 GMT-0500 (Central Daylight Time)
in Chrome
my answer gives
9/19/2013
because that is my Locale Convention, if the OP wanted yyyy/mm/dd
because that is the locale convention then my answer would be correct.
new Date(...)
4 times? \$\endgroup\$ – JohnMark13 Sep 18 '13 at 20:47Date
method doesn't return aDateTime
value without the time part, it returns one where the time is actually00:00:00
. ADateTime
value doesn't exist without the time part. Note also that usingnew Date()
multiple times is not just ugly, it could give a completely wrong result if you run the code right at midnight so that the date changes from one call to the next. Given, that is extremely unlikely to happen, but it's better to write it so that it can never happen. \$\endgroup\$ – Guffa Sep 19 '13 at 15:28without
to mean the value 00:00:00 \$\endgroup\$ – Michel Ayres Sep 19 '13 at 15:54