最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - javascript Today() function - Stack Overflow

programmeradmin5浏览0评论

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 badges
Add a ment  | 

4 Answers 4

Reset to default 7

You 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.

  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!

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

发布评论

评论列表(0)

  1. 暂无评论