javascript Today() 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6921606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:20:34  来源:igfitidea点击:

javascript Today() function

javascriptjquery

提问by Ronald

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.

如何实现 Today() 函数?它不一定是真正的函数,等效的解决方案也一样好。谢谢。

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

[编辑] 我忘了提及。当前时间(今天)是本地时间,与之比较的日期对象是服务器时间,可以是世界上任何地方。

回答by Yahel

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

您可以只比较toDateString()传递给 a 的日期字符串的snew 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() + '?');
}

回答by paxdiablo

Use the following:

使用以下内容:

var today = new Date();

Keep in mind that the Javascript Dateobject 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:

请记住,Date尽管名称如此,Javascript对象是一个时间戳。您可能希望按照以下示例将各个字段与getMonth(),getDate()和 的相等性进行比较getFullYear()

<html>
    <head>
    </head>
    <body>
        <script language="javascript">
            var today = new Date();
            alert(
                today.getFullYear() + "." +
                (today.getMonth() + 1) + "." +
                today.getDate()
            );
        </script>
    </body>
</html>

回答by Ray Toal

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

这是个有趣的问题。首先,您使用 JavaScript 获取当前日期时间

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.

因为两个日期对象只有在它们是完全相同的对象时才会相等。因此,当您比较日期时,请使用getTime. 如果您只关心日期的相等性而不关心日期时间,则必须做更多的工作,但还不错。

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!

  1. 从 datetime 对象中,找到对应的午夜时间。有这方面的算法,或者您可以使用像date.js.

  2. 把它变成一个字符串: d.toISOString().substring(0,10). 这是一个丑陋的黑客,请注意时区

回答by ShankarSangoli

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

Today()javascript中没有方法。您可以使用new Date()来获取当前日期。