首页 > 解决方案 > 将秒转换为年、月、周、日、小时、分钟和秒

问题描述

我在这篇文章中尝试了类似 Andris 的回答:将秒转换为天、小时、分钟和秒,但我在年、月和周内也需要这个。

我正在运行这个

getDateStrings() {
  console.log(req_creation_date);
  const today = new Date();

  const creation_date = new Date('2020-01-06T20:24:00.000Z');

  const creation_date_diff = Math.abs(today.getTime() - creation_date.getTime());
  const creation_date_diffDays = Math.ceil((creation_date_diff / 1000));
  console.log(creation_date_diffDays);
  const creation_date_diffDays_days = Math.ceil((creation_date_diff / (1000 * 3600 * 24)) - 1);
  console.log(creation_date_diffDays_days);
  const y = Math.floor(creation_date_diffDays / (31536000));
  const ms = Math.floor(creation_date_diffDays % (3600 * 24 * 7 * 4.34524 * 12) / 2592000);
  const w = Math.floor(creation_date_diffDays % (3600 * 24 * 7 * 4.34524) /  604800);
  const d = Math.floor(creation_date_diffDays % (3600 * 24 * 7) / 86400);
  const h = Math.floor(creation_date_diffDays % (3600 * 24) / 3600);
  const m = Math.floor(creation_date_diffDays % 3600 / 60);
  const s = Math.floor(creation_date_diffDays % 60);

  const yDisplay = y > 0 ? y + (y === 1 ? ' year, ' : ' years, ') : '';
  const msDisplay = ms > 0 ? ms + (ms === 1 ? ' month, ' : ' months, ') : '';
  const wDisplay = w > 0 ? w + (w === 1 ? ' week, ' : ' weeks, ') : '';
  const dDisplay = d > 0 ? d + (d === 1 ? ' day, ' : ' days, ') : '';
  const hDisplay = h > 0 ? h + (h === 1 ? ' hour, ' : ' hours, ') : '';
  const mDisplay = m > 0 ? m + (m === 1 ? ' minute, ' : ' minutes, ') : '';
  const sDisplay = s > 0 ? s + (s === 1 ? ' second ' : ' seconds') : '' ;
  console.log(yDisplay, msDisplay, wDisplay, dDisplay + hDisplay + mDisplay + sDisplay);
}

从 06-06-2020 到今天已经过去了 60 天。

它返回“2 个月、4 周、4 天、21 分钟、10 秒”出了什么问题,应该是“2 个月、21 分钟、10 秒”

我不知道我做错了什么,我也在尝试 2020 年 1 月 15 日。51 天过去了,但它返回“1 个月、2 周、2 天、41 分钟、43 秒”总共 46 天而不是 51 天,因为它应该是“1 个月、3 周、0 天、41 分钟, 43秒”

非常感谢!

标签: javascriptangulartypescript

解决方案


我使用moment.js创建了这个沙箱。我试图尽可能多地复制您的代码。这是代码:

import "./styles.css";
import moment from "moment";

const getDuration = (startDate, endDate) =>
  moment.duration(moment(endDate).diff(moment(startDate)));

function getDurationText(startDate, endDate) {
  const duration = getDuration(startDate, endDate);
  const years = duration.years();
  const months = duration.months();
  const weeks = duration.weeks();
  const days = duration.subtract(weeks, "w").days();
  const hours = duration.hours();
  const seconds = duration.seconds();
  const milliseconds = duration.milliseconds();

  const yearsText = getDurationTextOrDefault(years, "Year");
  const monthsText = getDurationTextOrDefault(months, "Month");
  const weeksText = getDurationTextOrDefault(weeks, "Week");
  const daysText = getDurationTextOrDefault(days, "Day");
  const hoursText = getDurationTextOrDefault(hours, "Hour");
  const secondsText = getDurationTextOrDefault(seconds, "Second");
  const millisecondsText = getDurationTextOrDefault(
    milliseconds,
    "Millisecond"
  );

  return `${yearsText}${monthsText}${weeksText}${daysText}${hoursText}${secondsText}${millisecondsText}`;
}

function getDurationTextOrDefault(duration, unit) {
  if (duration <= 0) return "";
  unit = duration === 1 ? unit : unit + "s";
  return `${duration} ${unit} `;
}

const start = new Date("2020-01-07T20:24:00.000Z");
const end = new Date();

document.getElementById("app").innerHTML = `
<h1>Duration</h1>
<div>
  ${getDurationText(start, end)}
</div>
`;

输出是否符合您的预期?

编辑从几天中减去几周,因为时刻不会自动执行此操作。


推荐阅读