I'm using this script to start a timeinterval from a given timestamp.
function startCounter (el, myStartTimestamp) {
var startTimestamp = parseInt(myStartTimestamp);
setInterval(function() {
startTimestamp++;
var timeStampMoment = moment.unix(startTimestamp);
var
years = timeStampMoment.get('years'),
months = timeStampMoment.get('month'),
weeks = timeStampMoment.get('week'),
days = timeStampMoment.get('day'),
hours = timeStampMoment.get('hour'),
minutes = timeStampMoment.get('minute'),
seconds = timeStampMoment.get('second'),
arr = [];
if (years > 0) {
arr.push(years == 1 ? '1 years ' : years + ' years');
}
if (months > 0) {
arr.push(months == 1 ? '1 month ' : months + ' months');
}
if (weeks > 0) {
arr.push(weeks == 1 ? '1 week ' : weeks + ' weeks');
}
if (days > 0) {
arr.push(days == 1 ? '1 day ' : days + ' days');
}
if (hours > 0) {
arr.push(hours == 1 ? '1 hr ' : hours + ' hrs');
}
if (minutes > 0) {
arr.push(minutes > 1 ? minutes + ' mins' : minutes + ' min');
}
if (seconds > 0) {
arr.push(seconds > 1 ? seconds + ' secs' : seconds + ' sec');
}
el.html(arr.join(' '));
}, 1000);
}
Now I want to include months and years too. But how do I do that since the total days per month can vary?
I have updated the question with a moment.js solution but it is not quite accurate.
This timestamp 1390896484 produces this result 2014 years 5 weeks 2 days 9 hrs 10 mins 46 secs (with a few seconds added). What do I miss ?
The timestamp equals 01 / 28 / 14 @ 8:08:04am UTC so I would like an output that would say "2 weeks 3 days" followed by number of "hours minutes and seconds".