1

I'd like to determine if a given date object is the same day as the current day. Below is the psuedo code.

// date is a Date object
function (date)
{
    if (date == Today())
        alert('How are you today?');
    else
        alert('How were you last ' + date.toDateString() + '?');
}

How do I implement the Today() function? It doesn't have to be a function really, an equivalent solution will be just as good. Thanks.

[edit] I forgot to mention. The current time (today) is local time and the date object that it will be compared with is server time, which can be anywhere in the world.

Ronald
  • 1,532
  • 4
  • 18
  • 34

4 Answers4

7

You can just compare the toDateString()s of the date string you're passing to a new Date() without any parameter passed to it (it will default to today).

// date is a Date object
function alertDateGreeting(date)
{
    if (date.toDateString() == (new Date()).toDateString())
        alert('How are you today?');
    else
        alert('How were you last ' + date.toDateString() + '?');
}
Yahel
  • 37,023
  • 22
  • 103
  • 153
  • Not my first instinct but quite elegant; +1 – Domenic Aug 03 '11 at 04:33
  • It cannot be more simple than that I guess. But in the case where the current time is local time and the other is server generated time from a different timezone? – Ronald Aug 03 '11 at 04:37
  • Agreed. Datetimes, local dates, and time zones are much easier with Joda Time, date.js and the advanced packages we are generally used to. Who'd have thought JavaScript's native solution is `dateString`? Oh wait, right, this is _JavaScript_ we are talking about.... – Ray Toal Aug 03 '11 at 04:40
  • @Ronald Timezone adjustments with JavaScript's native `Date` library are, to put it mildly, insane. I'd try date.js if you want to do timezone manipulations without tearing out your eyes. – Yahel Aug 03 '11 at 04:44
4

Use the following:

var today = new Date();

Keep in mind that the Javascript Date object is a timestamp despite its name. You will probably want to compare the individual fields for equality with getMonth(), getDate() and getFullYear(), as per the following sample:

<html>
    <head>
    </head>
    <body>
        <script language="javascript">
            var today = new Date();
            alert(
                today.getFullYear() + "." +
                (today.getMonth() + 1) + "." +
                today.getDate()
            );
        </script>
    </body>
</html>
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
4

This is an interesting question. First, you get the current datetime in JavaScript with

new Date()

However, comparing two dates is not really done with ==

Consider

> d = new Date(2011, 1, 1)
Tue Feb 01 2011 00:00:00 GMT-0800 (PST)
> d2 = new Date(2011, 1, 1)
Tue Feb 01 2011 00:00:00 GMT-0800 (PST)
> d == d2
false

But...

> d2 = new Date(2011, 1, 1).getTime()
1296547200000
> d = new Date(2011, 1, 1).getTime()
1296547200000
> d == d2
true

Because two date objects will be equal only if they are the exact same object. So when you compare dates, use getTime. If you are only concerned with the equality of dates and not date times, you have to do a little more work, but it is not so bad.

ADDENDUM:

The question asked for a way to talk about the current date.

Here you have two ways to go, at least.

  1. From the datetime object, find the corresponding midnight time. There are algorithms for this, or you can use a package like date.js.

  2. Hack it into a string: d.toISOString().substring(0,10). This is a ugly hack, and please, be careful about timezones!

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • `getTime()` isn't so much for date comparisons as it is for comparing precise milliseconds difference. ie, `[date obj for 5pm today].getTime()` would not be equal to `[date obj for 5:01pm today].getTime()`. – Yahel Aug 03 '11 at 04:35
  • Right, @yahelc, it's a date vs datetime thing. I didn't explain it very well. – Ray Toal Aug 03 '11 at 04:42
  • Why does `new Date(2011, 1, 1)` give 1st of **February** 2011?! (Just tried this in IE, am thoroughly confused) – Justin Mar 31 '15 at 14:42
  • Ah - turns out [months start at 0](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) in JavaScript – Justin Mar 31 '15 at 15:04
  • And JavaScript isn't the only one http://stackoverflow.com/questions/2552483/why-does-the-month-argument-range-from-0-to-11-in-javascripts-date-constructor – Ray Toal Mar 31 '15 at 17:06
0

There is no Today() method in javascript. You can use new Date() to get the current date.

ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124