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

如何修复硬编码时间问题,按规则显示正确到账时长?

修正到账时长显示逻辑的代码修改方案

问题背景

现有代码存在硬编码的时间显示逻辑,无法精准匹配到账时长的不同场景,需要按照以下规则调整显示文案:

  • 到账时长0-1分钟:显示「我们已为您到账金额₹x,耗时仅x分钟。」
  • 到账时长2-59分钟:显示「我们已为您到账金额₹x,耗时仅x分钟。」(分钟数为复数)
  • 到账时长1小时:显示「我们已为您到账金额₹x,耗时1小时。」
  • 到账时长超过1小时:显示「我们已为您到账金额₹x,耗时x小时。」

原问题代码

details.banner ={};
let hours = moment.duration(moment(lastStage.timeStamp) - moment(firstStage.timeStamp)).asHours();
if(lastStage.showBanner && hours <= 24){
   let timeInfo;
   if(hours <= 1){
        timeInfo = " In an hour.";
   } else {
        timeInfo = " in " + Math.trunc(hours) + " hour(s)";
   }
   details.banner.message = "Amount credited of ₹ " + amount.toFixed(2) + timeInfo;
}
return details;

修改后的代码

details.banner = {};
// 计算总时长的分钟数,实现精准区间判断
const duration = moment.duration(moment(lastStage.timeStamp) - moment(firstStage.timeStamp));
const totalMinutes = duration.asMinutes();
const totalHours = duration.asHours();

if(lastStage.showBanner && totalHours <= 24){
    let message;
    const formattedAmount = `₹${amount.toFixed(2)}`;
    
    if(totalMinutes <= 1){
        // 0-1分钟场景
        const minutes = Math.round(totalMinutes);
        message = `我们已为您到账金额${formattedAmount},耗时仅${minutes}分钟。`;
    } else if(totalMinutes < 60){
        // 2-59分钟场景
        const minutes = Math.trunc(totalMinutes);
        message = `我们已为您到账金额${formattedAmount},耗时仅${minutes}分钟。`;
    } else if(totalHours === 1){
        // 刚好1小时场景
        message = `我们已为您到账金额${formattedAmount},耗时1小时。`;
    } else {
        // 超过1小时场景
        const hours = Math.trunc(totalHours);
        message = `我们已为您到账金额${formattedAmount},耗时${hours}小时。`;
    }
    
    details.banner.message = message;
}
return details;

修改说明

  • 新增总分钟数计算,替代原有的仅按小时判断,实现分钟级别的精准匹配
  • 严格按照需求划分四个判断区间,生成对应场景的文案
  • 统一金额格式化逻辑,简化代码结构
  • 完全替换原硬编码的时间文案,贴合需求规则

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 16:40:19