admin管理员组

文章数量:1414628

I tried to make null date. so I tried to make them all 0 when I click the clear button.

 this.clear = function () {
            _this.newQuarters.forEach(function (_value, index) {
                _value.startDate = 0;
                _value.endDate = 0;
                _value.suppressAllocation = false;
                _value.quarters.forEach(function (_childValue, index) {
                    _childValue.startDate = 0;
                    _childValue.endDate = 0;
                    _childValue.suppressAllocation = false;
                });
            });
        };
    }

After that, I tried to add 0 at moment from other function.

 this.newQuarters.forEach(function (_value, index) {
            var startDate = moment(_value.startDate);

but, it display startDate = Wed Dec 31 1969. Please help me to find to make all date are null.

I tried to make null date. so I tried to make them all 0 when I click the clear button.

 this.clear = function () {
            _this.newQuarters.forEach(function (_value, index) {
                _value.startDate = 0;
                _value.endDate = 0;
                _value.suppressAllocation = false;
                _value.quarters.forEach(function (_childValue, index) {
                    _childValue.startDate = 0;
                    _childValue.endDate = 0;
                    _childValue.suppressAllocation = false;
                });
            });
        };
    }

After that, I tried to add 0 at moment from other function.

 this.newQuarters.forEach(function (_value, index) {
            var startDate = moment(_value.startDate);

but, it display startDate = Wed Dec 31 1969. Please help me to find to make all date are null.

Share Improve this question asked Sep 25, 2019 at 18:03 Jin HuhJin Huh 495 bronze badges 1
  • 1 What do you mean by "make all date null"? Setting day, month and year to zero? – Bergi Commented Sep 25, 2019 at 18:14
Add a ment  | 

3 Answers 3

Reset to default 9

When passing a number to the moment() function, it interpret it as a unix timestamp.
Which is the number of second since EPOCH time, or 01-01-1970.

So passing 0 to that function result in the very first second of jan 1 1970.
As Bergi pointed out, you are probably displaying your dates in your local timezone, which could result in a time before 01-01-1970.

If you want to create a null date, you should set your startDate to null and handle it correctly ( with an if statement).

You could also set the date back to the current time by passing no argument to the moment() function.

Dates can be tricky to work with in an application. There is no such thing as time that is not time. At least not in the world of programming. For this reason we have to create our own understanding of what a lack of time will be.

There is no time without time but there is a lack of value.

You obviously understand this, you had an approach, in your case, you seem to be fine with simply having time be zero, it just broke when working with moment as moment is using EPOCH time. Thus, zero is time.

You are going to have to test like the rest of us.

this.newQuarters.forEach(function (_value, index) {
  var startDate = _value.startDate ? moment(_value.startDate) : null;

Lack of time could be whatever you want, undefine, null, a beginning date

Be consistent: Database vendors and languages handle things differently, front end is not always the same time zone, etc.

If you are dealing with momentjs library, there is a function: invalid()

You can easily make your variables invalid moment object and control their validity.

let currentDate = moment(); // Valid moment object

console.log(currentDate.format());

// make the moment object invalid
currentDate = moment.invalid();

console.log(currentDate.isValid()); // Check validity
console.log(currentDate.format()); // Invalid date

// make the moment object valid again
currentDate = moment();

console.log(currentDate.isValid()); // Check validity
console.log(currentDate.format()); // A valid date
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.24.0/moment.min.js"></script>

本文标签: javascriptMoment js moment(0)Wed Dec 31 1969Stack Overflow