admin管理员组文章数量:1487745
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
3 Answers
Reset to default 9Without 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)
本文标签:
版权声明:本文标题:Is there a method to convert miliseconds to years,months,days,minutes,seconds in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741506101a2382329.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论