I implemented this logic where I am getting difference between 2 dates and times from Moment.js as seconds, and I need to convert these seconds into 1 Year 3 Months 10 Days 5 Hours 45 Minutes
format.
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);
I am getting the correct values up to days, but it's having problem from hours onwards.
I implemented this logic where I am getting difference between 2 dates and times from Moment.js as seconds, and I need to convert these seconds into 1 Year 3 Months 10 Days 5 Hours 45 Minutes
format.
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);
I am getting the correct values up to days, but it's having problem from hours onwards.
Share edited May 27, 2017 at 20:57 Badacadabra 8,5177 gold badges31 silver badges54 bronze badges asked Jun 6, 2015 at 5:31 Chanky MallickChanky Mallick 5872 gold badges10 silver badges31 bronze badges 4- Too many tags!! Subtle difference in APIs in each environment. Be specific... – Brett Walker Commented Jun 6, 2015 at 5:33
- 1 Doesn't moment already have support for this extraction? (There is humanize, and I think there might be a way to either get at another representation or use most of what is present.) – user2864740 Commented Jun 6, 2015 at 5:41
- You can use moment-duration-format . – Indranil Mondal Commented Jun 6, 2015 at 6:00
- stackoverflow./questions/14157341/… – mplungjan Commented Jun 6, 2015 at 7:17
3 Answers
Reset to default 3Dates and calendars are political, and don't always match up to simple arithmetic. Most languages that I have worked in solve this problem for you, and to my knowledge, JavaScript does this. (Note that we will have another leap second added to our calendar on June 30, 2015 - http://en.wikipedia/wiki/Leap_second.)
Given all of that, this answer is very much an approximation, as it does not take into account lengths of months, leap days, or leap seconds. But, it is something:
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 pare 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();
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 }
Here's how i approached this, running some unit tests i discovered that @Aun Shanbaz Awan solution gives wrong hours difference
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;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1741594936a4355548.html
评论列表(0条)