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

JavaScript计算上一个10分钟整点时间的实现方案

解决方案:生成10分钟整点的纪元秒数用于Gmail API查询

下面是直接可用的JavaScript实现,能精准计算你需要的时间点并转成Gmail API要求的纪元秒数:

function getLastTenMinuteEpoch() {
  const now = new Date();
  // 计算最近的10分钟整点的分钟数:当前分钟减去余数,比如53分→50分,8分→0分
  const targetMinutes = now.getMinutes() - (now.getMinutes() % 10);
  // 设置时间到目标整点,秒和毫秒清零
  const targetTime = new Date(now);
  targetTime.setMinutes(targetMinutes, 0, 0);
  // 转成纪元秒数(Gmail API要求的格式)
  return Math.floor(targetTime.getTime() / 1000);
}

使用说明:

  • 调用getLastTenMinuteEpoch()会返回最近一次10分钟整点的纪元秒数,比如10:50发送邮件时,返回的就是10:50对应的Unix时间戳
  • 将这个值拼到Gmail API的q参数中,格式为:
    const epochSeconds = getLastTenMinuteEpoch();
    const query = `in:sent after:${epochSeconds}`;
    // 之后将query传入Gmail API的Users.messages.list接口的q参数
    

逻辑说明:

  • 不管当前处于哪个时区,Date对象会自动处理本地时间计算,转成的纪元秒数是UTC时间戳,完全适配Gmail API的时区无关要求
  • 当当前时间刚好是10分钟整点时(比如16:20),函数会直接返回该时间的纪元秒数,符合你的查询需求

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 07:35:29