请考虑以下事项:
Date.parse('August 28th 2016');
NaN
等什么?什么是获得日期时间的正确方法,所以我可以做类似的事情:
data.sort((a, b) =>
Date.parse(Object.keys(a)[0]) -
Date.parse(Object.keys(b)[0]))
Object.keys(a)[0] // => August 28th 2016
那么......又怎么样了?
答案 0 :(得分:2)
我建议使用momentjs来更轻松地处理日期。您的任务可以通过以下方式完成:
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main() {
int i = 4;
double d = 4.0;
string s = "HackerRank ";
// Declare second integer, double, and String variables.
int n;
double d2;
string str;
// Read and save an integer, double, and String to your variables.
cin >> n;
cin >> d2;
cin.ignore();
getline(cin, str);
// Print the sum of both integer variables on a new line.
cout << i + n << endl;
// Print the sum of the double variables on a new line.
cout << d + d2 << endl;
// Concatenate and print the String variables on a new line
cout << s << str << endl;
// The 's' variable above should be printed first.
return 0;
}
var dates = [moment('August 28th 2016' ,'MMMM Do YYYY'),moment('August 22nd 2016' ,'MMMM Do YYYY'),moment('August 12th 2016' ,'MMMM Do YYYY')];
dates.sort(function(a,b) {
return a.isAfter(b);
});
console.log(dates);
或者更好 - 如果您的对象的键现在是字符串:
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.0/moment-with-locales.min.js"></script>
var dates = ['August 28th 2016','August 22nd 2016','August 12th 2016'];
dates.sort(function(a,b) {
return moment(a,'MMMM Do YYYY').isAfter(moment(b,'MMMM Do YYYY'));
});
console.log(dates);