# How do I calculate relative time?

Page 1 of 1

## 2 Replies - 690 Views - Last Post: 26 February 2015 - 09:27 AM

### #1 methuselah90

Reputation: 0
• Posts: 19
• Joined: 15-December 14

# How do I calculate relative time?

Posted 26 February 2015 - 12:54 AM

Given an array of DateTime values that have been randomly selected between now and the next 7 days, how do I display relative time values, like:

In 2 hours
In 3 days
In month

My program generates a random dateArray, with items in the following format:

"Mon Mar 02 2015 20:22:18 GMT+0000 (GMT Standard Time)",
"Mon Mar 02 2015 13:51:42 GMT+0000 (GMT Standard Time)",
"Mon Mar 02 2015 00:54:51 GMT+0000 (GMT Standard Time),
"Mon Mar 02 2015 00:00:29 GMT+0000 (GMT Standard Time)",
"Sun Mar 01 2015 00:59:56 GMT+0000 (GMT Standard Time)",
"Thu Feb 26 2015 11:28:27 GMT+0000 (GMT Standard Time)"

Code so far:

```var timelineItemCount = getRandomInt(0, 19);
var dateArray = new Array();

for(i=0;i<timelineItemCount;i++)
{
var today = new Date();
var randomDates = randomDate(today, new Date(today.getFullYear(), today.getMonth(), today.getDate()+7));
dateArray.push(randomDates);
}
dateArray.sort(function compare(a, B)/> {
return (a < B)/> - (a > B)/>;
});
console.log(dateArray);

function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

function randomDate(start, end) {
return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
}
```

I've seen examples on how to calculate relative time in the past, but not so much with values selected between now and the next 7 days:

```const int SECOND = 1;
const int MINUTE = 60 * SECOND;
const int HOUR = 60 * MINUTE;
const int DAY = 24 * HOUR;
const int MONTH = 30 * DAY;

var ts = new TimeSpan(DateTime.UtcNow.Ticks - yourDate.Ticks);
double delta = Math.Abs(ts.TotalSeconds);

if (delta < 0)
{
return "not yet";
}
if (delta < 1 * MINUTE)
{
return ts.Seconds == 1 ? "one second ago" : ts.Seconds + " seconds ago";
}
if (delta < 2 * MINUTE)
{
return "a minute ago";
}
if (delta < 45 * MINUTE)
{
return ts.Minutes + " minutes ago";
}
if (delta < 90 * MINUTE)
{
return "an hour ago";
}
if (delta < 24 * HOUR)
{
return ts.Hours + " hours ago";
}
if (delta < 48 * HOUR)
{
return "yesterday";
}
if (delta < 30 * DAY)
{
return ts.Days + " days ago";
}
if (delta < 12 * MONTH)
{
int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30));
return months <= 1 ? "one month ago" : months + " months ago";
}
else
{
int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365));
return years <= 1 ? "one year ago" : years + " years ago";
}

```

Would it be possible to convert it to a flexible function, that will simply convert an array item into relative time format passed into it and then return it into a string?

This post has been edited by methuselah90: 26 February 2015 - 12:58 AM

Is This A Good Question/Topic? 0

## Replies To: How do I calculate relative time?

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: How do I calculate relative time?

Posted 26 February 2015 - 05:36 AM

You might consider using moment.js, a very useful library.

### #3 ArtificialSoldier

• D.I.C Lover

Reputation: 2040
• Posts: 6,261
• Joined: 15-January 14

## Re: How do I calculate relative time?

Posted 26 February 2015 - 09:27 AM

Quote

I've seen examples on how to calculate relative time in the past

It's exactly the same. How you calculate the difference between 2 dates does not matter when the dates are. I think the easiest way is to convert both dates to Unix timestamps (the date object has a valueOf method that will return the timestamp), and then you divide and mod. The timestamps are in milliseconds, so it's just math. One day has 86400000 milliseconds. You would divide by that number and round down to get the number of days, then mod by that number to get the remainder. One hour has 3600000 milliseconds, so you divide by that and round down to get the number of hours, etc.