I have an external file that is in the following format which i think is JSON but i am not 100% sure.
{"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"]}
I am trying to get the miniumun date and maximum dates in the format MM/DD/YYYY so that i can use them in a Flatpicker minDate and maxDate options as per https://flatpickr.js.org/options/
I have tried using an AJAX call to the file to then get the minimum and maximumn dates but they are coming out with the same result which is ‘Thu Jan 01 1970 01:00:03 GMT+0100 (Greenwich Mean Time)’ when i console.log the var for maximumDate:
This is the AJAX call:
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) {
}
} );
Does anyone know the best way to get the min and max dates from this data so they can be used as variables for Flatpicker
Thanks
4
Answers
From the result Array use
Array.map
to convert the date strings to real Dates, Math.min/Math.max to determine the maximum and minimum date from that and format the found dates:Here is one way to do it using min/max:
The operation admits to a beautiful (well …) one-liner (reformatted for clarity) implementing the following steps:
result
property to datestamps.The array contains strings denoting dates in a format the JS Date object constructor understands. Using the
.valueOf
method on the constructed Date objects gives the epoch seconds – Actually this is not needed but included for clarity.Math
object’smin
function.Note that the time zone of the current locale is irrelevant for the minimum search (any offset from choosing a different zone would apply to all dates in turn not changing the comparison results).
Notes
max
formin
to compute, yes, the maximum.success
callback):Multiple aggregates
Pushing the one-liner idea, exploiting the fact that functions in JS are first-class citizens allows for computing multiple aggregates (min and max in this case) from the same set of data.
Because your dates are appropriately formatted (
YYYY-MM-DD HH:MM:SS
), you can simply sort them alphabetically. Then you can take the first and last elements of the array and format them as required usingtoLocaleDateString
: