从 JSON 文件获取最小和最大日期以与 FlatPicker 一起使用

问题描述 投票:0回答:1

我有一个外部文件,其格式如下,我认为是 JSON,但我不能 100% 确定。

{"result":["2008-09-29T00:00:00","2008-09-30T00:00:00","2008-10-01T00:00:00","2008-10-02T00:00:00","2008-10-03T00:00:00","2008-10-06T00:00:00","2008-10-07T00:00:00","2008-10-08T00:00:00","2008-10-09T00:00:00","2008-10-10T00:00:00","2008-10-13T00:00:00","2008-10-14T00:00:00","2008-10-15T00:00:00","2008-10-16T00:00:00","2008-10-17T00:00:00","2008-10-20T00:00:00","2008-10-21T00:00:00","2008-10-22T00:00:00","2008-10-23T00:00:00","2008-10-24T00:00:00","2008-10-27T00:00:00","2008-10-28T00:00:00","2008-10-29T00:00:00","2008-10-30T00:00:00","2008-10-31T00:00:00","2008-11-03T00:00:00","2008-11-04T00:00:00","2008-11-05T00:00:00","2008-11-06T00:00:00","2008-11-07T00:00:00","2008-11-10T00:00:00","2008-11-11T00:00:00","2008-11-12T00:00:00","2008-11-13T00:00:00","2008-11-14T00:00:00","2008-11-17T00:00:00","2008-11-18T00:00:00","2008-11-19T00:00:00","2008-11-20T00:00:00","2008-11-21T00:00:00","2008-11-24T00:00:00","2008-11-25T00:00:00","2008-11-26T00:00:00","2008-11-27T00:00:00"]}

我正在尝试以 MM/DD/YYYY 格式获取最小日期和最大日期,以便我可以按照 https://flatpickr.js.org/options/

在 Flatpicker minDate 和 maxDate 选项中使用它们

我尝试使用 AJAX 调用文件来获取最小和最大日期,但它们得到相同的结果,即“Thu Jan 01 1970 01:00:03 GMT+0100(格林威治标准时间)”我控制台记录最大日期的变量:

这是 AJAX 调用:

var webMethod = "path to my file";

$.ajax({
    type: "GET",
    url: webMethod,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        var allAvailableDates = [];
        $.each(msg.result, function (record) {
            allAvailableDates.push( new Date( record) );
        });
        var maximumDate = new Date(Math.max.apply(null, allAvailableDates));
        console.log( maximumDate );
    },
    error: function (e) {
    }
} );

有谁知道从这些数据中获取最小和最大日期的最佳方法,以便它们可以用作 Flatpicker 的变量

谢谢

javascript jquery json ajax
1个回答
0
投票

从结果数组中使用

Array.map
将日期字符串转换为实际日期,Math.min/Math.max 确定最大和最小日期并格式化找到的日期:

const pad = nr => `${nr}`.padStart(2, `0`);
const format = d => `${pad(d.getMonth()+1)}/${pad(d.getDate())}/${d.getFullYear()}`;
const exampleData = {
  "result": ["2008-09-29T00:00:00", "2008-09-30T00:00:00", "2008-10-01T00:00:00", "2008-10-02T00:00:00", "2008-10-03T00:00:00", "2008-10-06T00:00:00", "2008-10-07T00:00:00", "2008-10-08T00:00:00", "2008-10-09T00:00:00", "2008-10-10T00:00:00", "2008-10-13T00:00:00", "2008-10-14T00:00:00", "2008-10-15T00:00:00", "2008-10-16T00:00:00", "2008-10-17T00:00:00", "2008-10-20T00:00:00", "2008-10-21T00:00:00", "2008-10-22T00:00:00", "2008-10-23T00:00:00", "2008-10-24T00:00:00", "2008-10-27T00:00:00", "2008-10-28T00:00:00", "2008-10-29T00:00:00", "2008-10-30T00:00:00", "2008-10-31T00:00:00", "2008-11-03T00:00:00", "2008-11-04T00:00:00", "2008-11-05T00:00:00", "2008-11-06T00:00:00", "2008-11-07T00:00:00", "2008-11-10T00:00:00", "2008-11-11T00:00:00", "2008-11-12T00:00:00", "2008-11-13T00:00:00", "2008-11-14T00:00:00", "2008-11-17T00:00:00", "2008-11-18T00:00:00", "2008-11-19T00:00:00", "2008-11-20T00:00:00", "2008-11-21T00:00:00", "2008-11-24T00:00:00", "2008-11-25T00:00:00", "2008-11-26T00:00:00", "2008-11-27T00:00:00"]
};
const dates = exampleData.result.map(d => new Date(d));
const [min, max] = [
  format(new Date(Math.min(...dates))), 
  format(new Date(Math.max(...dates))) ];
console.log(`min: ${min}; max: ${max}`);

© www.soinside.com 2019 - 2024. All rights reserved.