Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
145
rated 0 times [  152] [ 7]  / answers: 1 / hits: 22003  / 13 Years ago, wed, august 3, 2011, 12:00:00

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.


More From » jquery

 Answers
3

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() + '?');
}

[#90854] Monday, August 1, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dylondarrianb

Total Points: 48
Total Questions: 109
Total Answers: 104

Location: Zambia
Member since Thu, Jun 25, 2020
4 Years ago
;