4
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ That is ugly, why would you call new Date(...) 4 times? \$\endgroup\$ – JohnMark13 Sep 18 '13 at 20:47
  • 2
    \$\begingroup\$ Also, DateTime.Now is usually used in C# (as it's static property, no need to create new object) \$\endgroup\$ – Alex Sep 18 '13 at 21:04
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ – Malachi Sep 19 '13 at 14:29
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Guffa Sep 19 '13 at 15:28
  • 1
    \$\begingroup\$ @Guffa As I put in parentheses, I just use the expression without to mean the value 00:00:00 \$\endgroup\$ – Michel Ayres Sep 19 '13 at 15:54
6
\$\begingroup\$

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);
\$\endgroup\$
  • \$\begingroup\$ I liked the idea of setting hours min and sec to 0 \$\endgroup\$ – Michel Ayres Sep 19 '13 at 13:50
3
\$\begingroup\$

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.

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

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.

\$\endgroup\$
  • 3
    \$\begingroup\$ Note that the OP wants a Date object, not a string. And he wants a code shorter than his (even if yours is prettier). \$\endgroup\$ – Vedran Šego Sep 18 '13 at 21:52
  • 2
    \$\begingroup\$ It's again a string. If I understand right, the OP wants today's date with the time set to 00:00:00. \$\endgroup\$ – Vedran Šego Sep 19 '13 at 0:32
  • 3
    \$\begingroup\$ quote from the Original Post This will give the current date, without the TimeSpan value ('00:00:00'). \$\endgroup\$ – Malachi Sep 19 '13 at 13:06
  • 3
    \$\begingroup\$ Referencing to w3schools on StackOverflow is like begging for downvotes. There are better sources, for example the Official Mozilla Documentation \$\endgroup\$ – Simon Forsberg Nov 20 '13 at 17:25
  • \$\begingroup\$ @SimonAndréForsberg, I know, but I also provided code and explanation to show that the information was correct, and that it worked the way I said it did. I will add a link from that page as well \$\endgroup\$ – Malachi Nov 20 '13 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.