You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何验证从数组提取的日期是否按升序/降序正确显示

Verify Date Order (Ascending/Descending) for Captured Mailbox Dates

Here's a practical, step-by-step solution to validate whether your captured dates are sorted correctly. The core idea is to first gather all valid dates, convert them into comparable Date objects, then check their sequence.

Step 1: Collect All Valid Date Strings

Your current code logs dates as it retrieves them, but we need to gather all dates first to compare their order. We’ll use Promise.all to handle async operations properly (since map with async functions returns promises that need to be resolved collectively):

// Collect all date strings from mailbox items
const dateStrings = await Promise.all(
  mailbox.mailbox_mailboxlist.map(async (ele) => {
    if (ele === undefined) return null; // Skip undefined elements
    const text = await ele.getText();
    const lines = text.split("\n");
    // Only return the date if the third line exists
    return lines.length >= 3 ? lines[2].trim() : null;
  })
);

// Filter out null/empty values to keep only valid dates
const validDateStrings = dateStrings.filter(str => str !== null && str !== "");

Step 2: Convert Date Strings to Parsable Date Objects

Your date format uses a dot in the time (e.g., 10.59am) which isn’t natively parsable by the Date constructor. We’ll clean this up first, then convert each string to a Date object:

// Helper function to parse your custom date format
function parseCustomDate(dateStr) {
  // Replace the dot in time with a colon (e.g., "10.59am" → "10:59am")
  const formattedStr = dateStr.replace(/(\d{1,2})\.(\d{2})([ap]m)/, "$1:$2$3");
  return new Date(formattedStr);
}

// Convert all valid strings to Date objects
const dateObjects = validDateStrings.map(parseCustomDate);

Step 3: Check Ascending/Descending Order

Now we can write simple helper functions to verify if the dates are sorted in ascending (oldest to newest) or descending (newest to oldest) order:

// Check if dates are in ascending order
function isAscending(dates) {
  for (let i = 0; i < dates.length - 1; i++) {
    // Compare timestamp values (getTime() returns milliseconds since epoch)
    if (dates[i].getTime() > dates[i + 1].getTime()) {
      return false;
    }
  }
  return true;
}

// Check if dates are in descending order
function isDescending(dates) {
  for (let i = 0; i < dates.length - 1; i++) {
    if (dates[i].getTime() < dates[i + 1].getTime()) {
      return false;
    }
  }
  return true;
}

// Run the checks and log results
const sortedAscending = isAscending(dateObjects);
const sortedDescending = isDescending(dateObjects);

console.log("Dates are in ascending order:", sortedAscending);
console.log("Dates are in descending order:", sortedDescending);

How It Works With Your Sample Data

For your console output:

Dates :Wed 30 Dec 2020 10.59am
Dates :Wed 30 Dec 2020 10.59am
Dates :Tue 29 Dec 2020 3.18pm
Dates :Thu 22 Oct 2020 10.47am
  • sortedAscending will return false (since dates go from newest to oldest)
  • sortedDescending will return true

Key Notes

  • Edge Cases: The code skips undefined elements and empty date strings to avoid parsing errors.
  • Date Parsing: If your date format varies (e.g., different time separators), adjust the regex in parseCustomDate to match your specific string structure.
  • Order Reliability: Comparing timestamps directly is the most accurate way to check date order, as it avoids issues with string comparison of date formats.

内容的提问来源于stack exchange,提问作者Rajesh T

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:34:51