http://www.codeproject.com/Articles/770323/How-to-Convert-a-Date-Time-to-X-minutes-ago-in-Csh

In one of our previous posts, we saw how can we convert a Date Time value to “X Minutes Ago” feature using jQuery. Today, in this post, we will see how we can achieve the same functionality in C#. In this post, we will write a C# function that will take a DateTime as a parameter and return the appropriate string.

The function to convert DateTime to a “Time Ago” string is as below:

public static string TimeAgo(DateTime dt)
{
TimeSpan span = DateTime.Now - dt;
if (span.Days > )
{
int years = (span.Days / );
if (span.Days % != )
years += ;
return String.Format("about {0} {1} ago",
years, years == ? "year" : "years");
}
if (span.Days > )
{
int months = (span.Days / );
if (span.Days % != )
months += ;
return String.Format("about {0} {1} ago",
months, months == ? "month" : "months");
}
if (span.Days > )
return String.Format("about {0} {1} ago",
span.Days, span.Days == ? "day" : "days");
if (span.Hours > )
return String.Format("about {0} {1} ago",
span.Hours, span.Hours == ? "hour" : "hours");
if (span.Minutes > )
return String.Format("about {0} {1} ago",
span.Minutes, span.Minutes == ? "minute" : "minutes");
if (span.Seconds > )
return String.Format("about {0} seconds ago", span.Seconds);
if (span.Seconds <= )
return "just now";
return string.Empty;
}

You can call the function something like below:

Console.WriteLine(TimeAgo(DateTime.Now));
Console.WriteLine(TimeAgo(DateTime.Now.AddMinutes(-5)));
Console.WriteLine(TimeAgo(DateTime.Now.AddMinutes(-59)));
Console.WriteLine(TimeAgo(DateTime.Now.AddHours(-2)));
Console.WriteLine(TimeAgo(DateTime.Now.AddDays(-5)));
Console.WriteLine(TimeAgo(DateTime.Now.AddMonths(-3)));

The output looks something like below:

Hope this post helps you. Keep learning and sharing!

How to Convert a Date Time to “X minutes ago” in C# z的更多相关文章

  1. Unable to convert MySQL date/time value to System.DateTime 错误

    C#读取MySql时,如果存在字段类型为date/datetime时的可能会出现以下问题“Unable to convert MySQL date/time value to System.DateT ...

  2. unable to convert MySQL date/time value to System.DateTime

    今天 用C# MySql做项目的时候 遇到了 unable to convert MySQL date/time value to System.DateTime 这样的异常错误,这个原因是因为:表里 ...

  3. 异常-----Can't convert the date to string, because it is not known which parts of the date variable are in use. Use ?date, ?time or ?datetime built-in, or ?string.\u003Cformat> or ?string(format) built-

    1.错误描述 五月 27, 2014 12:07:05 上午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template proc ...

  4. 每日踩坑 2018-12-25 【Unable to convert MySQL date/time value to System.DateTime】异常

    之前的公司一直是用 ORM ,所以底层的事情完全不用在意, 到了这家全是似乎全是 SQL 硬编码 所以也有机会重新去补补以前也没怎么写过的基本 ADO. 不过这两天总有自己写个简易ORM的想法在蠢蠢欲 ...

  5. ISO 8601: Delphi way to convert XML date and time to TDateTime and back (via: Stack Overflow)

    Recently I needed a way of concerting back and forth ISO 8601 DateTime values used in XML from Delph ...

  6. Unable to convert MySQL date/time value to System.DateTime

    当使用.NET connector 连接MYSQL数据库,检索某些数据的时候,你可能会得到一个错误信息: "Unable to convert MySQL date/time value t ...

  7. EF出错:Unable to convert MySQL date/time value to System.DateTime

    环境: .Net 4.5 EF6 MySQL 错误提示: MySql.Data.Types.MySqlConversionException : Unable to convert MySQL dat ...

  8. 【.Net+数据库】Unable to convert MySQL date/time value to System.DateTime

    C#读取MySql时,如果存在字段类型为date/datetime时的可能会出现以下问题“Unable to convert MySQL date/time value to System.DateT ...

  9. 解决 Unable to convert MySQL date/time value to System.DateTime

    C#读取MySql时,如果存在字段类型为date/datetime时的可能会出现以下问题“Unable to convert MySQL date/time value to System.DateT ...

随机推荐

  1. easy ui datagrid 动态绑定数据并绑定链接,进行操作

    ①.绑定datagrid,formatter { field: 'ShopId', title: '操作', width: 200, align: 'left', formatter: showSho ...

  2. SQL 跨服务器数据库增、删、改、查(二)

    --创建链接服务器 exec sp_addlinkedserver 'jx3xxiednr3ucidf', ' ', 'SQLOLEDB', 'jx3xxiednr3ucidf' exec sp_ad ...

  3. Sambar,实现Linux和Windows共享

    我下载的是tar的jar包,不是rpm,rpm就不多说了.目的是让Windows能够共享Linux系统的文件夹 1.进入到source文件夹: 2../configure->make->m ...

  4. POJ 2411 压缩状态DP

    这个题目非常赞! 给定一个矩形,要求用1*2 的格子进行覆盖,有多少种覆盖方法呢? dp[i][j] 当状态为j,且第i行已经完全铺满的情况下的种类数有多少种?j中1表示占了,0表示没有被占. 很显然 ...

  5. (转载)Cocos2dx-OpenGL ES2.0教程:你的第一个立方体(5)

    在上篇文章中,我们介绍了VBO索引的使用,使用VBO索引可以有效地减少顶点个数,优化内存,提高程序效率. 本教程将带领大家一起走进3D–绘制一个立方体.其实画立方体本质上和画三角形没什么区别,所有的模 ...

  6. (转)GDT与LDT

    网址:http://blog.csdn.net/billpig/article/details/5833980 保护模式下的段寄存器 由 16位的选择器 与 64位的段描述符寄存器 构成段描述符寄存器 ...

  7. 关于Oracle数据库中SQL空值排序的问题

    在Oracle中进行查询排序时,如果排序字段里面有空值的情况下,排序结果可能会达不到自己想要的结果.   如 select * from tableTest order by VISITS desc ...

  8. VS2008的默认打开重置为VS2008

  9. <context:component-scan>配置解析(转)

    在xml配置了这个标签后,spring可以自动去扫描base-pack下和其子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注 ...

  10. codeforces #305 D Mike and Fish

    正解貌似是大暴搜? 首先我们考虑这是一个二分图,建立网络流模型后很容易得出一个算法 S->行 容量为Num[X]/2; 行->列 容量为1 且要求(x,y)这个点存在 列->T 容量 ...