最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is there a method to convert miliseconds to years,months,days,minutes,seconds in JavaScript? - Stack Overflow

programmeradmin2浏览0评论

I am trying to pare 2 dates and represent the difference( converted to miliseconds) via years,months,days,minutes and seconds.I am new in JS,I looked up for a ready method to convert miliseconds to years,months,days, minutes, seconds but didn't find.Tell me please is there such method? If not how can I do that? Simply by dividing the difference and using reminder?Thank you in advance for help.

I am trying to pare 2 dates and represent the difference( converted to miliseconds) via years,months,days,minutes and seconds.I am new in JS,I looked up for a ready method to convert miliseconds to years,months,days, minutes, seconds but didn't find.Tell me please is there such method? If not how can I do that? Simply by dividing the difference and using reminder?Thank you in advance for help.

Share Improve this question asked Jul 13, 2016 at 14:54 ArmineArmine 1151 silver badge8 bronze badges 6
  • Possible duplicate of Converting milliseconds to a date (jQuery/JS) – Yoplaboom Commented Jul 13, 2016 at 14:56
  • I thibk you can just pass it to the Date object as argumebt, then get the time – Bálint Commented Jul 13, 2016 at 14:57
  • The best you can do is be approximate, because the definition of a year/month is not static. There're leap years and months have different lengths. – castletheperson Commented Jul 13, 2016 at 14:59
  • Given the description, it's more likely a duplicate of Difference between two dates in years, months, days in JavaScript – Arnauld Commented Jul 13, 2016 at 15:00
  • You could just use maths? E.g. x/100 = y (seconds), y/60 = m (minutes), m/60 = h (hrs), h/24 = d (days) etc.. (maths is probably way off, but you get the picture) – Gavin Thomas Commented Jul 13, 2016 at 15:01
 |  Show 1 more ment

3 Answers 3

Reset to default 9

Without having a calendar and knowing the input dates, the best you can do is be approximate.

Here is a script that shows the time elapsed since midnight last night.

var diff = Date.now() - Date.parse("July 13, 2016");

var seconds = Math.floor(diff / 1000),
    minutes = Math.floor(seconds / 60),
    hours   = Math.floor(minutes / 60),
    days    = Math.floor(hours / 24),
    months  = Math.floor(days / 30),
    years   = Math.floor(days / 365);

seconds %= 60;
minutes %= 60;
hours %= 24;
days %= 30;
months %= 12;

console.log("Years:", years);
console.log("Months:", months);
console.log("Days:", days);
console.log("Hours:", hours);
console.log("Minutes:", minutes);
console.log("Seconds:", seconds);

There is no inbuilt method to convert given millisecond to equivalent seconds, minutes, hours, days, months or years.

You will have to use math. Although you will only be able to convert accurately up to days. Months and years will vary as months are either 28, 29, 30 or 31 and there are leap years.

Consider having a look at http://momentjs./ before you write anything on your own, as you can do a lot more with this library like adding and subtracting days or hours etc

Here is a solution similar to other answers but with a different approach, I started from years instead of millis, I think is more accurate this way.

    const date = new Date(startDate).getTime()
    const today = new Date().getTime()
    
    // 1000 millis => sec , 86400 sec => day , 365 day => year
    let years = (today - date) / (1000 * 86400 * 365)
    let months = (years - Math.floor(years)) * 12
    let days = (months - Math.floor(months)) * 30
    let hours = (days - Math.floor(days)) * 24
    let minutes = (hours - Math.floor(hours)) * 60
    let sec = (minutes - Math.floor(minutes)) * 60
    let milli = (sec - Math.floor(sec)) * 1000
    
    years = Math.floor(years)
    months = Math.floor(months)
    days = Math.floor(days)
    hours = Math.floor(hours)
    minutes = Math.floor(minutes)
    sec = Math.floor(sec)
    milli = Math.floor(milli)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论