我的时间清单如下, '一天前, 10天前, 3个月前, 6 个月前'.
如何验证这些日期是升序的?在 javascript 硒中
我想不出尝试任何事情,我对 dd/mm/yyyy 或 mm/dd/yyyy 有一些想法。但当时的这种文本格式让我很困惑。
您可以详细阐述许多方法,它们的范围从非常简单到更高级。
这是一个非常简单的变体,假设您的 时间字符串 中始终包含三个部分,因此模式保持不变,即
"<quantity token> <time span token> ago"
这个想法非常简单
<quantity token>
的数字或 <time span token>
number * number of milliseconds
即可获取事件发生前的毫秒数。对每个输入时间字符串执行此操作,您将得到一个充满前毫秒数值的数组const times = ["a day ago", "10 days ago", "3 months ago", "6 months ago"];
const termsOfQuanity = {
a: 1,
an: 1,
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
nine: 9,
ten: 10,
};
const milliSecondsPerTimeSpan = {
seconds: 1000,
minutes: 60000,
hours: 3600000,
days: 86400000,
weeks: 604800000,
months: 2629800000,
years: 31557600000,
};
const timeSpans = {
second: milliSecondsPerTimeSpan.seconds,
seconds: milliSecondsPerTimeSpan.seconds,
"second(s)": milliSecondsPerTimeSpan.seconds,
minute: milliSecondsPerTimeSpan.minutes,
minutes: milliSecondsPerTimeSpan.minutes,
"minute(s)": milliSecondsPerTimeSpan.minutes,
hour: milliSecondsPerTimeSpan.hours,
hours: milliSecondsPerTimeSpan.hours,
"hour(s)": milliSecondsPerTimeSpan.hours,
day: milliSecondsPerTimeSpan.days,
days: milliSecondsPerTimeSpan.days,
"day(s)": milliSecondsPerTimeSpan.days,
week: milliSecondsPerTimeSpan.weeks,
weeks: milliSecondsPerTimeSpan.weeks,
"week(s)": milliSecondsPerTimeSpan.weeks,
month: milliSecondsPerTimeSpan.months,
months: milliSecondsPerTimeSpan.months,
"month(s)": milliSecondsPerTimeSpan.months,
year: milliSecondsPerTimeSpan.years,
years: milliSecondsPerTimeSpan.years,
"year(s)": milliSecondsPerTimeSpan.years,
};
function convertTimeStringToDate(timeString) {
const [quantityToken, timeSpanToken, _] = timeString.split(" ");
const quantity = convertQuantityTokenToNumber(quantityToken);
const timeSpan = convertTimeSpanTokenToMilliseconds(timeSpanToken, quantity);
return timeSpan * quantity;
}
function convertQuantityTokenToNumber(termOfQuantity) {
const possibleNumberQuantity = parseInt(termOfQuantity);
if (isNaN(possibleNumberQuantity)) {
return termsOfQuanity[termOfQuantity];
}
return possibleNumberQuantity;
}
function convertTimeSpanTokenToMilliseconds(timeSpan) {
return timeSpans[timeSpan];
}
const isSortedCorrectly = times
.map(convertTimeStringToDate)
.every((time, index, array) => (index > 0 ? array[index - 1] <= time : true));
console.log(`Is sorted correctly? ${isSortedCorrectly}`);
today
、yesterday
等字符串。/days?/i.test(timeSpanToken)
可能有一些未量化的术语,例如少数,一对等,例如几分钟前,几个小时前,那么您如何比较它们?什么更大/更小? 一对还是几个?