使用 Moment().max 和 min 查找最大和最小日期不起作用
Finding max and min dates using Moment().max and min not working
我有一个日期数组,想在数组中找到最大和最小日期。
这是我的代码:
let date_list = [
"2021-03-19T00:00:00Z",
"2021-03-20T00:00:00Z",
"2021-04-12T00:00:00Z",
"2021-04-13T00:00:00Z",
"2021-04-11T00:00:00Z",
"2021-04-12T00:00:00Z",
"2021-03-18T00:00:00Z"
];
let uniq_dates = [...new Set(date_list.map(d => moment.utc(d).format() ))];
console.log(uniq_dates);
console.log(moment().max(date_list));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>
我有时会收到一条错误消息 NaN
或者我收到 Deprecation warning: moment().max is deprecated, use moment.min instead.
如何获取日期数组中的最大值和最小值?
您可以将 Array#reduce
与 moment.max
结合使用。
let date_list = [
"2021-03-19T00:00:00Z",
"2021-03-20T00:00:00Z",
"2021-04-12T00:00:00Z",
"2021-04-13T00:00:00Z",
"2021-04-11T00:00:00Z",
"2021-04-12T00:00:00Z",
"2021-03-18T00:00:00Z"
];
let uniq_dates = [...new Set(date_list.map(d => moment.utc(d).format() ))];
console.log(uniq_dates);
console.log(uniq_dates.reduce((acc,curr)=>acc.max(curr), moment()));
.as-console-wrapper{max-height:100%!important;top:0}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>
您可以使用此代码。
let date_list = [
"2021-03-19T00:00:00Z",
"2021-03-20T00:00:00Z",
"2021-04-12T00:00:00Z",
"2021-04-13T00:00:00Z",
"2021-04-11T00:00:00Z",
"2021-04-12T00:00:00Z",
"2021-03-18T00:00:00Z"
];
let moments = date_list.map(d => moment(d)),
maxDate = moment.max(moments)
console.log(maxDate);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>
我有一个日期数组,想在数组中找到最大和最小日期。
这是我的代码:
let date_list = [
"2021-03-19T00:00:00Z",
"2021-03-20T00:00:00Z",
"2021-04-12T00:00:00Z",
"2021-04-13T00:00:00Z",
"2021-04-11T00:00:00Z",
"2021-04-12T00:00:00Z",
"2021-03-18T00:00:00Z"
];
let uniq_dates = [...new Set(date_list.map(d => moment.utc(d).format() ))];
console.log(uniq_dates);
console.log(moment().max(date_list));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>
我有时会收到一条错误消息 NaN
或者我收到 Deprecation warning: moment().max is deprecated, use moment.min instead.
如何获取日期数组中的最大值和最小值?
您可以将 Array#reduce
与 moment.max
结合使用。
let date_list = [
"2021-03-19T00:00:00Z",
"2021-03-20T00:00:00Z",
"2021-04-12T00:00:00Z",
"2021-04-13T00:00:00Z",
"2021-04-11T00:00:00Z",
"2021-04-12T00:00:00Z",
"2021-03-18T00:00:00Z"
];
let uniq_dates = [...new Set(date_list.map(d => moment.utc(d).format() ))];
console.log(uniq_dates);
console.log(uniq_dates.reduce((acc,curr)=>acc.max(curr), moment()));
.as-console-wrapper{max-height:100%!important;top:0}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>
您可以使用此代码。
let date_list = [
"2021-03-19T00:00:00Z",
"2021-03-20T00:00:00Z",
"2021-04-12T00:00:00Z",
"2021-04-13T00:00:00Z",
"2021-04-11T00:00:00Z",
"2021-04-12T00:00:00Z",
"2021-03-18T00:00:00Z"
];
let moments = date_list.map(d => moment(d)),
maxDate = moment.max(moments)
console.log(maxDate);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>