# How could I do DateTime.Now.Date (from C#) in JavaScript?

In C# I can use

DateTime dt = DateTime.Now.Date;


This will give the current date, without the TimeSpan value ('00:00:00'). In JavaScript I did this:

var dt = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate());


But is so "ugly" that there must be a better, easier and clean way to do it.

• That is ugly, why would you call new Date(...) 4 times? – JohnMark13 Sep 18 '13 at 20:47
• Also, DateTime.Now is usually used in C# (as it's static property, no need to create new object) – Alex Sep 18 '13 at 21:04
• do you want that date format on any machine, any browser, or do you want your date format when you view the page, what are you using the date for? if you are filling something in, then how it looks on a browser isn't going to matter, you just use the properties of the object to fill in....whatever – Malachi Sep 19 '13 at 14:29
• Note: The Date method doesn't return a DateTime value without the time part, it returns one where the time is actually 00:00:00. A DateTime value doesn't exist without the time part. Note also that using new 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. – Guffa Sep 19 '13 at 15:28
• @Guffa As I put in parentheses, I just use the expression without to mean the value 00:00:00 – Michel Ayres Sep 19 '13 at 15:54

It is not ideal, but you can do it this way...

var today = new Date();
var dateWithoutTime = new Date(today.getFullYear() , today.getMonth(), today.getDate());


There are multiple ways to make Dates in javascript. The format above will create a new Date based on "today"'s year, month, and date (think day of the month).

Technically you could also do this with a DateString, however "new Date(dateString)" is implementation dependent, and may have inconsistent behavior across different browsers.

And if making 2 Date objects makes you uncomfortable, you could always do the following:

var today = new Date();
today.setHours(0); // or today.toUTCString(0) due to timezone differences
today.setMinutes(0);
today.setSeconds(0);

• I liked the idea of setting hours min and sec to 0 – Michel Ayres Sep 19 '13 at 13:50

Your solution can be slightly improved by using a variable:

var now = new Date();
var dt = new Date(now.getFullYear(), now.getMonth(), now.getDate());


However, you can use Date.js, in which case your solution is simple:

var dt = Date.today();


Read more about it in the Date.js documentation, or try it here.

• Good link, but I don't want to use a plugging for this task – Michel Ayres Sep 19 '13 at 13:53

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

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.
• Note that the OP wants a Date object, not a string. And he wants a code shorter than his (even if yours is prettier). – Vedran Šego Sep 18 '13 at 21:52
• quote from the Original Post This will give the current date, without the TimeSpan value ('00:00:00'). – Malachi Sep 19 '13 at 13:06