javascript 如何将秒分别转换为年、月、日、小时、分钟?

iibxawm4  于 2023-02-15  发布在  Java
关注(0)|答案(4)|浏览(143)

我实现了这个逻辑,我从Moment.js中获取2个日期和时间之间的差异作为秒,我需要将这些秒转换为1 Year 3 Months 10 Days 5 Hours 45 Minutes

var numyears = Math.floor(seconds / 31536000);
var nummonths = Math.floor((seconds % 31536000) / 2628000);
var numdays = Math.floor(((seconds % 31536000) % 2628000) / 86400);
var numhours = Math.floor((((seconds % 31536000) % 2628000) % 86400)/3600);
var numminutes = Math.floor((((seconds % 31536000) % 86400) % 3600) / 60);

我得到了正确的值,但从几个小时开始就有问题了。

umuewwlo

umuewwlo1#

日期和日历是政治性的,并不总是符合简单的算术。我使用过的大多数语言都能为你解决这个问题,据我所知,JavaScript可以做到这一点。(注意,我们将在2015年6月30日在日历中添加另一个闰秒-http://en.wikipedia.org/wiki/Leap_second。)
考虑到所有这些,这个答案在很大程度上是一个近似值,因为它没有考虑月份、闰日或闰秒的长度,但是,它是一个东西:

var x = new Date('2015-02-26 12:32:54-0600'); // or if you have milliseconds, use that instead
var y = new Date('2015-03-19 01:22:09-0600');
var z = new Date(y-z);
z;
// returns "Wed Jan 21 1970 06:49:15 GMT-0600 (CST)"
// now compare this with epoch
var epoch = new Date('1970-01-01 00:00:00-0600');
var diff_years = z.getYear() - epoch.getYear();
var diff_month = z.getMonth() - epoch.getMonth();
var diff_days = z.getDate() - epoch.getDate();
var diff_hours = z.getHours() - epoch.getHours();
var diff_minutes = z.getMinutes() - epoch.getMinutes();
ffvjumwh

ffvjumwh2#

function getDateDiff(expires_in) {
let result = {
    years_diff: 0,
    months_diff: 0,
    days_diff: 0,
    hours_diff: 0,
}
if (expires_in) {
    result.years_diff = Math.floor(expires_in / 31536000);
    result.months_diff = Math.floor((expires_in % 31536000) / 2628000);
    result.days_diff = Math.floor(((expires_in % 31536000) % 2628000) / 86400);
    result.hours_diff = Math.floor((((expires_in % 31536000) % 2628000) % 86400) / 3600);
}
return result }

我是这样处理的,运行了一些单元测试,我发现@Aun Shanbaz Awan解决方案给出了错误的时差

gcxthw6b

gcxthw6b3#

function TimeCalculator(seconds) {
  let y = Math.floor(seconds / 31536000);
  let mo = Math.floor((seconds % 31536000) / 2628000);
  let d = Math.floor(((seconds % 31536000) % 2628000) / 86400);
  let h = Math.floor((seconds % (3600 * 24)) / 3600);
  let m = Math.floor((seconds % 3600) / 60);
  let s = Math.floor(seconds % 60);

  let yDisplay = y > 0 ? y + (y === 1 ? " year, " : " years, ") : "";
  let moDisplay = mo > 0 ? mo + (mo === 1 ? " month, " : " months, ") : "";
  let dDisplay = d > 0 ? d + (d === 1 ? " day, " : " days, ") : "";
  let hDisplay = h > 0 ? h + (h === 1 ? " hour, " : " hours, ") : "";
  let mDisplay = m > 0 ? m + (m === 1 ? " minute " : " minutes, ") : "";
  let sDisplay = s > 0 ? s + (s === 1 ? " second" : " seconds ") : "";
  return yDisplay + moDisplay + dDisplay + hDisplay + mDisplay + sDisplay;
}
acruukt9

acruukt94#

const year = Math.floor(second / (60*60*24*30*12));
const month = Math.floor((second % (60*60*24*30*12))/(60*60*24*30));
const day = Math.floor(((second %(60*60*24*30*12)) % (60*60*24*30)) / (60*60*24));
const hour = Math.floor((((second %(60*60*24*30*12)) % (60*60*24*30)) % (60*60*24)) / (60*60));
const minute = Math.floor(((((second %(60*60*24*30*12)) % (60*60*24*30)) % (60*60*24)) % (60*60)) / 60);

相关问题