简体   繁体   English

日期对象的行为不符合预期

[英]Date object not behaving as expected

I have a control that shows the current month and year at the start of my application. 我有一个控件,显示我的应用程序开始时的当前月份和年份。 It has 2 buttons that will add or subtract 1 from the current month. 它有2个按钮,可以在当前月份中加1或减1。 I use the same function to add/subtract to initialize the date by calling it with 0. Here is the function: 我使用相同的函数来添加/减去初始化日期,方法是用0调用它。这是函数:

$scope.SetDateMonth = function (modifier) {
        if (modifier === 0) {
            //initialize the current date, only called on load
            $scope.CurrentDate = new Date();

        } else {
            //alter the currentdate
            $scope.CurrentDate.setMonth($scope.CurrentDate.getMonth() + modifier);
        }
        /*
        $scope.StartOfRange = new Date($scope.CurrentDate.getFullYear(), $scope.CurrentDate.getMonth());
        alert($scope.StartOfRange.getDate());
        $scope.StartOfRange.setDate(1);
        alert($scope.StartOfRange.getDate());
        //$scope.StartOfRange.setDate(1);
        $scope.EndOfRange = new Date($scope.CurrentDate.getFullYear(), $scope.CurrentDate.getMonth()+1);
        $scope.EndOfRange.setDate($scope.EndOfRange.getDate() - 1);
       */

        //set startofrange to first day of month
        $scope.StartOfRange = new Date($scope.CurrentDate.getFullYear(), $scope.CurrentDate.getMonth(), 1);
        //this sets the date to the last day of the previous month. Bc. we add 1 to the month, this will result in the last day of CurrentMonth
        $scope.StartOfRange = new Date($scope.CurrentDate.getFullYear(), $scope.CurrentDate.getMonth()+1, 0); 

        console.log("Start");
        console.log($scope.StartOfRange);
        console.log("End");
        console.log($scope.EndOfRange);

        //.setHours(0,0,0,0)

        //$scope.DateChanged();
    }

I want to add 2 global variables to this. 我想为此添加2个全局变量。 Start/EndOfRange. 启动/ EndOfRange。 They have to store the first and last day of the month. 他们必须存储该月的第一天和最后一天。 You can see that quite a bit of code has been commented out, I've been trying a lot of diffrent examples I found online. 您可以看到相当多的代码被注释掉了,我一直在尝试在网上找到很多不同的例子。 I'm stuck on the setDate() (maybe its not in the code I provided anymore, I did try to use it but couldn't get it to work) function. 我停留在setDate()(也许它不在我提供的代码中,我确实尝试使用它但无法使其工作)功能。 If I call it with param 1, it sets the date to 30. I've tried UTC times aswell. 如果我用param 1调用它,它会将日期设置为30.我也尝试过UTC时间。

The odd thing is that when I use an alert, it shows 1 as supposed. 奇怪的是,当我使用警报时,它显示为1。 When I use console.log, it does not. 当我使用console.log时,它没有。 I tried all values from 1 to 30, they all give vague unexpected results. 我尝试了从1到30的所有值,它们都给出了模糊的意外结果。

Reading from w3 ( https://www.w3schools.com/jsref/jsref_obj_date.asp ) I'm calling it correctly. 从w3( https://www.w3schools.com/jsref/jsref_obj_date.asp )读取我正确地调用它。 I did manage to get the correct date out of it by constructing a new date object with day(or date, bit confusing, 3rd parameter) set to -29. 我设法通过构造一个新的日期对象来设置为-29,从而获得正确的日期。日期(或日期,位混淆,第三个参数)设置为-29。 Seems to consistently set the date to 1 but deffinitly not a long term solution. 似乎始终将日期设置为1,但不是长期解决方案。

You can use the following two functions to find the first day and last day of the current month. 您可以使用以下两个功能查找当月的第一天和最后一天。

$scope.StartOfRange = $scope.getFirstDay();    
$scope.EndOfRange = $scope.getLastDay();
$scope.getFirstDay = function ()
{
    var currDate = new Date();     
    year = currDate.getFullYear();
    month = currDate.getMonth();
    day = currDate.getDate();
    var lastMonth = month - 1;
    if (month == 0) {
        year--;
        lastMonth = 11;
    }
    lastDay = 28;
    var previousMonthDate = new Date(year, lastMonth,lastDay);

    while (previousMonthDate < currDate)
    {
        previousMonthDate = new Date(year, lastMonth, lastDay++);
        var curMonth = previousMonthDate.getMonth();
        if (curMonth == month)
        {
            console.log("First Day :" + previousMonthDate);
            return previousMonthDate;
        }
    }
}
$scope.getLastDay = function () {
    var currDate = new Date();
    year = currDate.getFullYear();
    var nextYear = year;
    month = currDate.getMonth();
    day = currDate.getDate();
    var nextMonth = month + 1;
    if (month == 11) {
        nextYear++;
        nextMonth = 0;
    }
    lastDay = 28;
    var currentMonthDate = new Date(year, month, lastDay);
    var nextMonthDate = new Date(nextYear, nextMonth, 1);
    while (currentMonthDate < nextMonthDate) {
        var finalDate = currentMonthDate;
        currentMonthDate = new Date(year, month, lastDay++);
        var curMonth = currentMonthDate.getMonth();
        if (curMonth == nextMonth) {
            console.log("LastDay :" + finalDate);
            return finalDate;
        }
    }
}

Using this code: 使用此代码:

$scope.SetDateMonth = function (modifier) {
        if (modifier === 0) {
            //initialize the current date, only called on load
            $scope.CurrentDate = new Date();

        } else {
            //alter the currentdate
            $scope.CurrentDate.setMonth($scope.CurrentDate.getMonth() + modifier);
        }

        //set startofrange to first day of month
        $scope.StartOfRange = new Date($scope.CurrentDate.getFullYear(), $scope.CurrentDate.getMonth(), 1);
        //this sets the date to the last day of the previous month. Bc. we add 1 to the month, this will result in the last day of CurrentMonth
        $scope.EndOfRange = new Date($scope.CurrentDate.getFullYear(), $scope.CurrentDate.getMonth()+1, 0); 
        console.log("Start");
        console.log($scope.StartOfRange);
        console.log("End");
        console.log($scope.EndOfRange);
   }

Checking the console output in both firefox and internet explorer. 检查firefox和Internet Explorer中的控制台输出。 Since the current date is 4-5-2017, StartOfRange should be 1-5-2017, EndOfRange should be 31-5-2017. 由于当前日期是4-5-2017,StartOfRange应为1-5-2017,EndOfRange应为31-5-2017。

FireFox: 火狐:

Start
Date 2017-04-30T22:00:00.000Z
End
Date 2017-05-30T22:00:00.000Z

IE: IE:

Start
[date] Mon May 01 2017 00:00:00 GMT+0200
End
[date] Wed May 31 2017 00:00:00 GMT+0200

Will edit if I get firefox working with the same code. 如果我让firefox使用相同的代码,将编辑。

edit 1: added this code: 编辑1:添加了这段代码:

var test = $scope.CurrentDate;
test.setDate(1);
console.log("test:");
console.log(test);

This works as expected for some reason. 由于某种原因,这可以按预期工作。 However, it doesn't only set test's date, because its a reference to CurrentDate. 但是,它不仅设置测试的日期,因为它是对CurrentDate的引用。 I don't see why this would work but setting date to 1 in the constructor wouldn't.. 我不明白为什么这会工作,但在构造函数中将日期设置为1不会..

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

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