使用矩来计算两个日期之间的天数

时间:2019-06-24 14:05:11

标签: javascript momentjs

我正在尝试使用以下代码计算Javascript中两个日期之间的天数。

    console.log(self.StartDate());
    var start = moment(self.StartDate(), "YYYY-MM-DD");
    console.log(start);
    console.log(self.EndDate());
    var end = moment(self.EndDate(), "YYYY-MM-DD");
    console.log(end);
    var duration = moment.duration(start.diff(end));
    console.log(duration);

    //no of days between start and end
    console.log(duration.asDays());

控制台输出:

Thu Jun 20 2019 01:00:00 GMT+0100 (British Summer Time)
n {_isAMomentObject: true, _i: Thu Jun 20 2019 01:00:00 GMT+0100 (British Summer Time), _f: "YYYY-MM-DD", _isUTC: false, _pf: {…}, …}_d: Wed Aug 19 0020 00:00:00 GMT-0001 (British Summer Time) {}_f: "YYYY-MM-DD"_i: Thu Jun 20 2019 01:00:00 GMT+0100 (British Summer Time) {}_isAMomentObject: true_isUTC: false_locale: r {_ordinalParse: /\d{1,2}(th|st|nd|rd)/, ordinal: ƒ, _abbr: "en", _ordinalParseLenient: /\d{1,2}(th|st|nd|rd)|\d{1,2}/}_pf: {empty: false, unusedTokens: Array(0), unusedInput: Array(3), overflow: 1, charsLeftOver: 49, …}__proto__: Object
Mon Jun 24 2019 15:10:41 GMT+0100 (British Summer Time)
n {_isAMomentObject: true, _i: Mon Jun 24 2019 15:10:41 GMT+0100 (British Summer Time), _f: "YYYY-MM-DD", _isUTC: false, _pf: {…}, …}
Ea {_milliseconds: -126230400000, _days: 0, _months: 0, _data: {…}, _locale: r}
-1461

self.StartDate()为20/06/2019,self.EndDate()为24/06/2019,所以我很高兴显示4的警报,但显示-1461。

为什么会这样?

3 个答案:

答案 0 :(得分:2)

问题在于您的日期不是formatted the way you tell moment.js they are20/06/2019DD/MM/YYYY,而不是YYYY-MM-DD。根据文档,非数字字符将被忽略,因此20/06/2019将被解析为2006-20-19。由于不存在这样的日期,因此Moment.js会尝试猜测您的实际含义,并附带2020-06-19

var start = moment("20/06/2019", "YYYY-MM-DD");

console.log(start);
<script src="https://cdn.jsdelivr.net/momentjs/2.14.1/moment-with-locales.min.js"></script>

结束日期也发生了同样的事情。

您需要更改您告诉Moment.js的格式,或者固定开始日期和结束日期以使用您指定的格式,在两种情况下,您都将获得正确的结果:

var self = {
    StartDate: () => "20/06/2019",
    EndDate: () => "24/06/2019"
}

var start = moment(self.StartDate(), "DD/MM/YYYY"); //using the correct format
var end = moment(self.EndDate(), "DD/MM/YYYY");
var duration = moment.duration(start.diff(end));

console.log(duration.asDays());
<script src="https://cdn.jsdelivr.net/momentjs/2.14.1/moment-with-locales.min.js"></script>

var self = {
    StartDate: () => "2019-06-20", //using dates in the correct format
    EndDate: () => "2019-06-24"
}

var start = moment(self.StartDate(), "YYYY-MM-DD");
var end = moment(self.EndDate(), "YYYY-MM-DD");
var duration = moment.duration(start.diff(end));

console.log(duration.asDays());
<script src="https://cdn.jsdelivr.net/momentjs/2.14.1/moment-with-locales.min.js"></script>

答案 1 :(得分:0)

您的代码应该可以正常工作。您仅应在偏差计算中交换Axes.violinplot(self, dataset, positions=None, vert=True, widths=0.5, showmeans=False, showextrema=True, showmedians=False, points=100, bw_method=None, *, data=None) start以获得正值。

end
var startDate = moment("2019-06-20", "YYYY-MM-DD");
var endDate = moment("2019-06-24", "YYYY-MM-DD");
var duration = moment.duration(startDate.diff(endDate));

//no of days between start and end
console.log(duration.asDays());


var duration = moment.duration(endDate.diff(startDate));
console.log(duration.asDays());

如果您仅对以天为单位的差额感兴趣,则此代码会更短:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
var startDate = moment("2019-06-20", "YYYY-MM-DD");
var endDate = moment("2019-06-24", "YYYY-MM-DD");
var diff  = startDate.diff(endDate, 'days');
console.log(diff);


var diff  = endDate.diff(startDate, 'days');
console.log(diff);

日期本身必须存在一些问题。
实际上,这就是解析日期字符串的方式

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
// This is parsed as the 19th day of 6th month in year 2020
var startDate = moment("20/06/2019", "YYYY-MM-DD");
// This is parsed as the 19th day of 6th month in year 2024
var endDate = moment("24/06/2019", "YYYY-MM-DD");
var diff  = endDate.diff(startDate, 'days');
console.log({"start": startDate, "end": endDate, "diff": diff});

// You either need to change the date strings or the format strings
var startDate = moment("20/06/2019", "DD/MM/YYYY");
var endDate = moment("24/06/2019", "DD/MM/YYYY");
var diff  = endDate.diff(startDate, 'days');
console.log({"start": startDate, "end": endDate, "diff": diff});


// You either need to change the date strings or the format strings
var startDate = moment("2019-06-20", "YYYY-MM-DD");
var endDate = moment("2019-06-24", "YYYY-MM-DD");
var diff  = endDate.diff(startDate, 'days');
console.log({"start": startDate, "end": endDate, "diff": diff});

答案 2 :(得分:-1)

您的日期无效,您指定的开始日期为“ 20/06/2019”,它是“ DD / MM / YYYY”的模板,但提示您使用“ YYYY-MM-DD”的模板。

您只需要替换当前传递的模板,这是一种更简洁的方法:

import * as WordCloud from 'wordcloud';