Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-从日期数组中获取1个最早的日期_Javascript_Arrays_Date - Fatal编程技术网

Javascript-从日期数组中获取1个最早的日期

Javascript-从日期数组中获取1个最早的日期,javascript,arrays,date,Javascript,Arrays,Date,我有一个这样格式的数组。我只想取消最早的约会 数组中的值如下所示: 数组: creationDate=[“2019年2月13日星期三21:14:55格林威治标准时间”,“2019年2月13日星期三21:19:42格林威治标准时间”,“2019年2月13日星期三21:28:29格林威治标准时间”,“2019年2月13日星期三21:31:04格林威治标准时间] 这是我的代码: 代码: 预期结果: 2019年2月13日星期三格林尼治标准时间21:14:55 if(creationDates){ r

我有一个这样格式的数组。我只想取消最早的约会

数组中的值如下所示:

数组: creationDate=[“2019年2月13日星期三21:14:55格林威治标准时间”,“2019年2月13日星期三21:19:42格林威治标准时间”,“2019年2月13日星期三21:28:29格林威治标准时间”,“2019年2月13日星期三21:31:04格林威治标准时间]

这是我的代码:

代码: 预期结果: 2019年2月13日星期三格林尼治标准时间21:14:55

if(creationDates){
  return creationDates.sort()[0]
}

您可以使用
Array.reduce()
并在每次迭代中比较日期并取最早的日期:

const creationDate=[“2019年2月13日星期三21:14:55 GMT”,“2019年2月13日星期三21:19:42 GMT”,“2019年2月13日星期三21:28:29 GMT”,“2019年2月13日星期三21:31:04 GMT”];
常数最早=创建日期减少((c,n)=>
Date.parse(n)console.log(最早的)
您希望返回一个数字,而不是布尔值(因此使用
-
而不是
):

var creationDate=[“2019年2月13日星期三21:14:55格林尼治标准时间”,“2019年2月13日星期三21:19:42格林尼治标准时间”,“2019年2月13日星期三21:28:29格林尼治标准时间”,“2019年2月13日星期三21:31:04格林尼治标准时间”,“2019年2月13日星期三21:33:04格林尼治标准时间];
var orderedDates=creationDate.sort(函数(a,b){
返回日期.parse(a)-Date.parse(b);
});
console.log(orderedDates[0])
尝试使用
Date.parse(a)-Date.parse(b)
(或者切换a和b以相反的顺序排序)。compare函数要求返回一个数字,而不是布尔值。检查此处电子比较功能的规则:
if(creationDates){
  return creationDates.sort()[0]
}