简体   繁体   English

我的Javascript日期比较有什么问题

[英]Whats wrong with my Javascript Date Comparison

Im tyring to compare two dates in javascript but im getting strange values when calculating the dates. 我试图比较两个日期在javascript中,但是在计算日期时却得到了奇怪的值。

Can anyone see anything obvious in my code that is causing the issue? 谁能看到导致问题的代码中明显的东西?

The problem is the Todays date variable looks like a normal date, but my calculation for next week and last week are coming out as large numbers and the comparison wont work. 问题是,今天的日期变量看起来像是正常日期,但是我对下周和上周的计算结果很大,因此无法进行比较。

//Handles client side date selection changed
function dateSelectionChanged(sender, args) {

    //Declare array for Day names
    var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

    //Get the date
    var date = sender.get_selectedDate();

    //Get todays Date
    var today = new Date();
    var nextWeek = new Date().setDate(today.getDate() + 7);
    var lastWeek = new Date().setDate(today.getDate() - 7);

    //Show the day name
    $('#<%= txtDay.ClientID %>').val(days[date.getDay()]);

    if ( date < lastWeek ) {
        alert('Date Under Week');
    }
    if ( date > nextWeek ) {
        alert('Date Over Week');
    }
}

And here is the Code in Debug so you can see the values: 这是“调试”中的代码,因此您可以看到这些值: 在此处输入图片说明

EDIT: Solution 编辑:解决方案

//Handles client side date selection changed
function dateSelectionChanged(sender, args) {

    //Declare array for Day names
    var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

    //Get the date
    var date = sender.get_selectedDate();

    //Get todays Date
    var today = new Date();
    var nextWeek = new Date().setDate(today.getDate() + 7);
    var lastWeek = new Date().setDate(today.getDate() - 7);

    //Get the dates in easier to compare format
    nextWeek = new Date(nextWeek);
    lastWeek = new Date(lastWeek);

    //Show the day name
    $('#<%= txtDay.ClientID %>').val(days[date.getDay()]);

    if ( date < lastWeek) {
        alert('Date Under Week');
    }
    if ( date > nextWeek) {
        alert('Date Over Week');
    }
}

For setDate , JavaScript returns timestamps, which represent the amount of milliseconds since 1 January 1970 00:00:00 to a specific moment in time. 对于setDate ,JavaScript返回时间戳,代表从1970年1月1日00:00:00到特定时间的毫秒数。 It might look useless, but in fact is very useful as you can represent any time as a simple number. 它可能看起来没有用,但实际上非常有用,因为您可以随时将其表示为一个简单的数字。

In case you want to get back a Date , you can use: 如果您想找回Date ,可以使用:

new Date(timestamp);

so eg add: 所以例如添加:

nextWeek = new Date(nextWeek);
lastWeek = new Date(lastWeek);

Another method is to set dates like this. 另一种方法是像这样设置日期。

var today = new Date();
var nextWeek = new Date();
nextWeek.setDate(today.getDate() + 7);
var lastWeek = new Date();
lastWeek.setDate(today.getDate() - 7);

If you supply no arguments, the constructor creates a Date object for today's date and time according to local time. 如果不提供任何参数,则构造函数会根据当地时间为今天的日期和时间创建一个Date对象。 If you supply some arguments but not others, the missing arguments are set to 0. If you supply any arguments, you must supply at least the year, month, and day. 如果提供一些参数,但不提供其他参数,则缺少的参数将设置为0。如果提供任何参数,则必须至少提供年,月和日。 You can omit the hours, minutes, seconds, and milliseconds. 您可以省略小时,分钟,秒和毫秒。

The date is measured in milliseconds since midnight 01 January, 1970 UTC. 该日期以自世界标准时间1970年1月1日午夜以来的毫秒数为单位。 A day holds 86,400,000 milliseconds. 一天可容纳86,400,000毫秒。 The Date object range is -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC. 相对于1970年1月1日UTC,日期对象范围是-100,000,000天至100,000,000天。

try the following code while comparing the dates 在比较日期时尝试以下代码

if ( today < lastWeek.getMilliseconds() ) {
        alert('Date Under Week');
    }
    if ( today > nextWeek.getMilliseconds() ) {
        alert('Date Over Week');
    }

The Date object provides uniform behavior across platforms. Date对象提供跨平台的统一行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM