我想确定给定的日期对象是否与当天相同.下面是伪代码.
// date is a Date object
function (date)
{
if (date == Today())
alert('How are you today?');
else
alert('How were you last ' + date.toDateString() + '?');
}
Run Code Online (Sandbox Code Playgroud)
如何实现Today()函数?它不一定是一个功能,一个等效的解决方案也同样好.谢谢.
[编辑]我忘了提.当前时间(今天)是本地时间,与之比较的日期对象是服务器时间,可以是世界上任何地方.
您可以toDateString()将传递的日期字符串的s与new Date()没有传递给它的任何参数进行比较(默认为今天).
// 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() + '?');
}
Run Code Online (Sandbox Code Playgroud)