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. MongoDB安装,启动,注册为windows系统服务

    MongoDB安装与启动 周建旭 2014-08-10 解压完后配置环境变量 下载Windows 32-bit或64-bit版本并解压缩,程序文件都在bin目录中,其它两个目录分别是C++调用是的头文 ...

  2. iOS 支付宝应用(备用参考)

    1:先与支付宝签约,获得商户ID(partner)和账号ID(seller) 2:下载相应的公钥私钥文件(加密签名用) 3:下载支付宝SDK 4:生成订单信息5:调用支付宝客户端,由支付宝客户端跟支付 ...

  3. HDU1411+四面体的体积

    用cos sin各种乱搞之后 求出一个公式.. 但是怕精度损失厉害,还是暂且贴个公式的,copy别人的.. #include<stdio.h> #include<math.h> ...

  4. CodeForce 339:A+B+C

    A题:水题.. #include<stdio.h> #include<string.h> ; char s[ maxn ]; int main(){ //freopen(&qu ...

  5. 【前端学习】【jQuery选择器】

    jQuery选择器     jQuery选择器 本文内容引自于单东林<锋利的jQuery>,未经原作者准许,禁止以商业目的转载发布! 选择器是jQuery的根基,在jQuery中,对事件处 ...

  6. linux配置防火墙详细步骤(iptables命令使用方法)

    通过本教程操作,请确认您能使用linux本机.如果您使用的是ssh远程,而又不能直接操作本机,那么建议您慎重,慎重,再慎重! 通过iptables我们可以为我们的Linux服务器配置有动态的防火墙,能 ...

  7. 191. Number of 1 Bits

    题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...

  8. Hoax or what

    Hoax or what 题意是询问一个动态序列的最小值和最大值. 可以用multiset来实现. #include <stdio.h> #include <set> usin ...

  9. js控制元素的显示与隐藏

    <body class="easyui-layout"> <div id = "centerId" data-options="re ...

  10. poj3034Whac-a-Mole(dp)

    链接 状态转移好想 不过有坑 大家都犯的错误 我也会犯 很正常 就是锤子可以移到n*n以外  要命的是我只加了5 以为最多不会超过5  WA了N久  才想到 上下两方向都可以到5 所以最多加10 以时 ...