简体   繁体   English

如何从字符串日期数组中获取最大日期字符串?

[英]How to get a string of max date from an array of string dates?

What would be the most elegant way to get max date from an array of strings like below?从如下所示的字符串数组中获取最大日期的最优雅方法是什么?

var dates = ["2018-12-29T15:23:20.486695Z", "2018-12-29T15:23:21.613216Z", "2018-12-29T15:23:22.695710Z", "2018-12-29T15:23:24.013567Z", "2018-12-29T15:23:25.097649Z", "2018-12-29T15:23:26.692125Z", "2018-12-29T15:23:27.918561Z", "2018-12-29T15:23:29.217879Z", "2018-12-29T15:23:30.468284Z", "2018-12-29T15:23:31.548761Z"]

I have tried:我试过了:

var timestamps = dates.map(date => Date.parse(date));
var max_date = Math.max.apply(Math, timestamps)

But this leaves me with a timestamp that I would need to convert back to the exact original format (and I don't know how to do that).但这给我留下了一个时间戳,我需要将其转换回确切的原始格式(我不知道该怎么做)。

You could compare the ISO 8601 date like strings and take the greater value.您可以像字符串一样比较ISO 8601日期并取更大的值。

 var dates = ["2018-12-29T15:23:20.486695Z", "2018-12-29T15:23:21.613216Z", "2018-12-29T15:23:22.695710Z", "2018-12-29T15:23:24.013567Z", "2018-12-29T15:23:25.097649Z", "2018-12-29T15:23:26.692125Z", "2018-12-29T15:23:27.918561Z", "2018-12-29T15:23:29.217879Z", "2018-12-29T15:23:30.468284Z", "2018-12-29T15:23:31.548761Z"], latest = dates.reduce((a, b) => a > b ? a : b); console.log(latest);

That's the format provided by toISOString on Date .这是toISOStringDate上提供的格式。 So you take your timestamp value, feed it into new Date , and use toISOString on the result:因此,您将时间戳值输入到new Date ,然后对结果使用toISOString

console.log(new Date(max_date).toISOString());

Example:例子:

 var dates = [ "2018-12-29T15:23:20.486695Z", "2018-12-29T15:23:21.613216Z", "2018-12-29T15:23:22.695710Z", "2018-12-29T15:23:24.013567Z", "2018-12-29T15:23:25.097649Z", "2018-12-29T15:23:26.692125Z", "2018-12-29T15:23:27.918561Z", "2018-12-29T15:23:29.217879Z", "2018-12-29T15:23:30.468284Z", "2018-12-29T15:23:31.548761Z" ]; var timestamps = dates.map(date => Date.parse(date)); var max_date = Math.max.apply(Math, timestamps) console.log(new Date(max_date).toISOString());

Note that you get "2018-12-29T15:23:31.548Z" rather than "2018-12-29T15:23:31.548761Z" because you've parsed the strings to JavaScript dates, and JavaScript dates only hold milliseconds, not microseconds.请注意,您得到的是"2018-12-29T15:23:31.548Z"而不是"2018-12-29T15:23:31.548761Z"因为您已将字符串解析为 JavaScript 日期,而 JavaScript 日期仅保留毫秒,而不是微秒.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用date-fns的max函数从日期字符串数组中查找最大日期 - Find max date from a string array of dates using max function of date-fns 如何立即将日期字符串数组解析为Dates - How to parse array of date string into Dates in moment 如何使用javascript / jquery从数组中获取最大和最小日期? - How to get max and min dates from array using javascript/jquery? 按即将到来的日期对以日期为字符串的数组进行排序 - Sort array with date as string by upcoming dates 如何获取数组中的最大日期? - How to get Max Date in an array? 如何在不重复月份的情况下从日期数组中获取最多 8 个日期,这些日期的索引之间的间隔相同? - How do I get max 8 dates from an array of dates with the same interval between their indexes without repeating month? 如何从字符串数组获取? - How to get from a string array? 如何从jQuery或JavaScript中的静态日期数组获取日期范围 - How to get date ranges from an array of static dates in jquery or javascript 使用JavaScript从日期数组中查找最大日期 - Finding the max date from an array of dates using javascript 如何从当前日期的数组中获得7个连续日期(数组中的日期不同)? - How do i get 7 consecutive dates(dates are different in array) from an array onwards the current date?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM