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 pared with is server time, which can be anywhere in the world.
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 pared with is server time, which can be anywhere in the world.
Share Improve this question edited Aug 3, 2011 at 4:31 Ronald asked Aug 3, 2011 at 4:24 RonaldRonald 1,5424 gold badges19 silver badges34 bronze badges4 Answers
Reset to default 7You can just pare 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() + '?');
}
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 pare 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>
This is an interesting question. First, you get the current datetime in JavaScript with
new Date()
However, paring 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 pare 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.
From the datetime object, find the corresponding midnight time. There are algorithms for this, or you can use a package like
date.js
.Hack it into a string:
d.toISOString().substring(0,10)
. This is a ugly hack, and please, be careful about timezones!
There is no Today()
method in javascript. You can use new Date()
to get the current date.