I tried to ask this question but some users thought it was a duplicate and closed the question. I explained it in a comment but in any case, I wrote an extension method in C# and it will appear in an answer.
Asked
Active
Viewed 630 times
0
-
2So whats exactly is your question? – J3soon Dec 28 '15 at 13:02
-
If you have client generated web pages that uses moment.js fromNow() or toNow() methods to display a time ago string (e.g. "3 minutes ago") and you also have server generated fragments or complete pages using C# (e.g. Razor view engine) then you'll need these same functions in C#. One could use javascript to replace the time string but that would be inconvenient or impossible (html generated for an email) in some situations. – howardlo Dec 28 '15 at 13:18
-
It's still a duplicate. Take a look at the linked question. Your suggested code belongs as an answer there, along with the many other answers that have already been proposed. – Matt Johnson-Pint Dec 28 '15 at 18:02
1 Answers
2
Here is the extension for replicating moment.js fromNow() and toNow(). It's useful if some of your pages are server generated (e.g. Razor view engine) and you want the same time ago verbiage.
public static class Extensions
{
// For from_now and to_now
static Dictionary<string, string> relative_time = new Dictionary<string, string>()
{
{ "future" , "in {0}" },
{ "past" , "in {0}" },
{ "s" , "a few seconds ago" },
{ "m" , "a minute ago" },
{ "mm" , "{0} minutes ago" },
{ "h" , "an hour ago" },
{ "hh" , "{0} hours ago" },
{ "d" , "a day ago" },
{ "dd" , "{0} days ago" },
{ "M" , "a month ago" },
{ "MM" , "{0} months ago" },
{ "Y" , "a year ago" },
{ "YY" , "{0} years ago" },
{ "-s" , "in seconds" },
{ "-m" , "in a minute" },
{ "-mm" , "in {0} minutes" },
{ "-h" , "in an hour" },
{ "-hh" , "in {0} hours" },
{ "-d" , "in a day" },
{ "-dd" , "in {0} days" },
{ "-M" , "in a month" },
{ "-MM" , "in {0} months" },
{ "-Y" , "in a year" },
{ "-YY" , "in {0} years" },
};
/// <summary>
/// replicate moment.js fromNow()
/// </summary>
/// <returns></returns>
public static string from_now(this DateTime time_gmt)
{
// http://momentjs.com/docs/#/displaying/fromnow : moment.js fromNow() doc
var diff = DateTime.UtcNow - time_gmt;
if (diff.TotalSeconds < 0)
{
return time_gmt.to_now();
}
else if (diff.TotalSeconds <= 45)
{
return relative_time["s"];
}
else if (diff.TotalSeconds <= 90)
{
return relative_time["m"];
}
else if (diff.TotalMinutes <= 45)
{
return string.Format(relative_time["mm"], Convert.ToInt32( diff.TotalMinutes));
}
else if (diff.TotalMinutes <= 90)
{
return relative_time["h"];
}
else if (diff.TotalHours <= 22)
{
return string.Format(relative_time["hh"], Convert.ToInt32(diff.TotalHours));
}
else if (diff.TotalHours <= 36)
{
return relative_time["d"];
}
else if (diff.TotalDays <= 25)
{
return string.Format(relative_time["dd"], Convert.ToInt32(diff.TotalDays));
}
else if (diff.TotalDays <= 45)
{
return relative_time["M"];
}
else if (diff.TotalDays <= 345)
{
return string.Format(relative_time["MM"], Convert.ToInt32(diff.TotalDays/(365.0/12.0)));
}
else if (diff.TotalDays <= 545)
{
return relative_time["Y"];
}
else if (diff.TotalDays > 545)
{
return string.Format(relative_time["YY"], Convert.ToInt32(diff.TotalDays/365.0));
}
return "invalid";
}
/// <summary>
/// replicate moment.js toNow()
/// </summary>
/// <param name="time_gmt"></param>
/// <returns></returns>
public static string to_now(this DateTime time_gmt)
{
// http://momentjs.com/docs/#/displaying/tonow : moment.js toNow() doc
var diff = time_gmt - DateTime.UtcNow;
if (diff.TotalSeconds < 0)
{
return time_gmt.from_now();
}
else if (diff.TotalSeconds <= 45)
{
return relative_time["-s"];
}
else if (diff.TotalSeconds <= 90)
{
return relative_time["-m"];
}
else if (diff.TotalMinutes <= 45)
{
return string.Format(relative_time["-mm"], Convert.ToInt32( diff.TotalMinutes));
}
else if (diff.TotalMinutes <= 90)
{
return relative_time["-h"];
}
else if (diff.TotalHours <= 22)
{
return string.Format(relative_time["-hh"], Convert.ToInt32(diff.TotalHours));
}
else if (diff.TotalHours <= 36)
{
return relative_time["-d"];
}
else if (diff.TotalDays <= 25)
{
return string.Format(relative_time["-dd"], Convert.ToInt32(diff.TotalDays));
}
else if (diff.TotalDays <= 45)
{
return relative_time["-M"];
}
else if (diff.TotalDays <= 345)
{
return string.Format(relative_time["-MM"], Convert.ToInt32(diff.TotalDays/(365.0/12.0)));
}
else if (diff.TotalDays <= 545)
{
return relative_time["-Y"];
}
else if (diff.TotalDays > 545)
{
return string.Format(relative_time["-YY"], Convert.ToInt32(diff.TotalDays/365.0));
}
return "invalid";
}
}

howardlo
- 1,401
- 1
- 15
- 20
-
1Note that this isn't *precisely* the same as moment's function, as it is English only. It also doesn't handle leap years correctly. – Matt Johnson-Pint Dec 28 '15 at 18:10
-
My downvote to question because it's not a question and to this answer because code is *suboptimal* at best, no need to post a raw erroneous *translation*. Sorry to be so direct. However it may be a nice post for Code Review! – Adriano Repetti Dec 28 '15 at 20:50