2016-12-03 43 views
0

隨着每個電子郵件線程,我使用的.getLastMessageDate()和產生陣列看起來像這樣[Mon Nov 14 05:58:24 GMT+08:00 2016, Thu Nov 17 07:46:01 GMT+08:00 2016, Tue Sep 27 07:00:06 GMT+08:00 2016, Wed Sep 07 10:46:50 GMT+08:00 2016, Sun Jul 31 18:47:16 GMT+08:00 2016, Sat Jul 09 06:14:15 GMT+08:00 2016, Sun Apr 24 20:33:50 GMT+08:00 2016, Sun Apr 03 11:48:40 GMT+08:00 2016, Thu Mar 31 12:55:39 GMT+08:00 2016, Wed Mar 23 06:59:00 GMT+08:00 2016]最大日期與谷歌輸出

我的問題是如何找出什麼是最新的日期?在Python中這很容易,但我認爲在這種情況下,每個元素都是一個字符串。

回答

1

如何:

var dates = ['Mon Nov 14 05:58:24 GMT+08:00 2016', 'Thu Nov 17 07:46:01 GMT+08:00 2016', 
      'Tue Sep 27 07:00:06 GMT+08:00 2016', 'Wed Sep 07 10:46:50 GMT+08:00 2016', 
      'Sun Jul 31 18:47:16 GMT+08:00 2016', 'Sat Jul 09 06:14:15 GMT+08:00 2016', 
      'Sun Apr 24 20:33:50 GMT+08:00 2016', 'Sun Apr 03 11:48:40 GMT+08:00 2016', 
      'Thu Mar 31 12:55:39 GMT+08:00 2016', 'Wed Mar 23 06:59:00 GMT+08:00 2016']; 

var max = dates.reduce(function(a, b) { 
    return new Date(a) > new Date(b) ? a : b; 
}); 
// max should contain the latest date as a string. 
Logger.log(max); 
// Thu Nov 17 07:46:01 GMT+08:00 2016 
+0

你希望的日期構造函數將正確分析所提供的字符串格式,但它並不需要一個得到ECMA-262的支持。支持取決於實施,不應該依賴。 – RobG

+0

公平點。它在Apps Script上工作​​,但是,這是目標。 – Bardy

0

你可以像這樣的日期進行排序:

var dates = [ 
    "Mon Nov 14 05:58:24 GMT+08:00 2016", 
    "Thu Nov 17 07:46:01 GMT+08:00 2016", 
    "Tue Sep 27 07:00:06 GMT+08:00 2016", 
    "Wed Sep 07 10:46:50 GMT+08:00 2016", 
    "Sun Jul 31 18:47:16 GMT+08:00 2016", 
    "Sat Jul 09 06:14:15 GMT+08:00 2016", 
    "Sun Apr 24 20:33:50 GMT+08:00 2016", 
    "Sun Apr 03 11:48:40 GMT+08:00 2016", 
    "Thu Mar 31 12:55:39 GMT+08:00 2016", 
    "Wed Mar 23 06:59:00 GMT+08:00 2016" 
]; 

dates.sort(function(a, b) { 
    return new Date(b) - new Date(a); 
}); 

alert(dates[0]);