【问题标题】:convert work seconds into weeks, daysm, hours and minutes将单词秒转换为周、天、小时和分钟
【发布时间】:2017-04-25 18:57:47
【问题描述】:

我正在使用 jira rest api 来解决问题。问题估计时间以秒数形式返回 - 但基于每周 5 天、每天 8 小时。

因此,例如,1d 的估计时间被返回为 28800 秒

我有这段代码可以做我想做的事,但它有异味,我想知道是否有更好的函数来做我想做的事

 let es = 246180; // time (number of *work* seconds) is "1w 3d 4h 23m"
 let sInMin = 60;
 let sInHour = (60 * 60);
 let sInDay  = (sInHour * 8);
 let sInWeek = (sInDay * 5);

 let w = Math.trunc(es / sInWeek);
 let d = Math.trunc((es - (w * sInWeek)) / sInDay);
 let h = Math.trunc((es - (w * sInWeek) - (d * sInDay)) / sInHour);
 let m = Math.trunc((es - (w * sInWeek) - (d * sInDay) - (h * sInHour)) / sInMin);

 console.log(w,d,h,m)  // 1 3 4 23

如果我将momentmoment-duration-format 添加到组合中,我可以这样做

moment.duration({w,d,h,m}).format("w[w] d[d] h[h] m[m]")

获得神奇的1w 3d 4h 23m字符串

我该如何改进?

【问题讨论】:

    标签: javascript time momentjs jira-rest-api


    【解决方案1】:

    JIRA REST API 已经为您提供了您尝试计算的字段。

    来自他们的文档here中的示例响应:

    "timetracking": {
        "originalEstimate": "10m",
        "remainingEstimate": "3m",
        "timeSpent": "6m",
        "originalEstimateSeconds": 600,
        "remainingEstimateSeconds": 200,
        "timeSpentSeconds": 400
        }
    

    您正在获取originalEstimateSeconds 字段并尝试进行数学运算以获取字符串,但该字符串已经在originalEstimate 字段中为您生成。不用计算。

    【讨论】:

    • 好吧,这有点糟糕,因为我必须使用搜索 api 来解决问题(通过 externalId 搜索)并且 that api 似乎没有返回timetracking ,只是实际的秒值。 jira api 是如此血腥 不一致,它让我发疯;(感谢您的提示
    • 您调用的是哪个特定的 api,您从哪里获得以秒为单位显示估算值的字段?这是您在请求的fields 参数中要求的吗?如果是这样,也许有一个不同的字段可以将值显示为字符串?只是根据文档推测。
    猜你喜欢
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    相关资源
    最近更新 更多