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. python学习笔记1(语法)

    语法 从"Hello,world"开始看吧,我们学的很多语言都是从helloworld开始的. >>> 1 + 1 2 >>> print 'H ...

  2. 用in判断input中的placeholder属性是否在这个对象里

    <input id="test"> var ele = document.getElementById("test"); if("plac ...

  3. replace()替换文字

    var test = text.innerHTML; b = test.replace(/任晓强/g,"你好"); console.log(b); html: <div id ...

  4. [转载]在網頁上加入HTML5 的Video Tag,直接播放MP4、OGG…等

    在之前有一篇文章提到HTML5(為何iPhone,iPod,iPad不支援Flash,HTML5將更普及於網路世界!!)的重要性,而Html 5的主要革新是在他的語意標籤,像是<video> ...

  5. uva 10892

    试了一下纯暴力  结果过了 无话可说  应该有更好的方法...... /**************************************************************** ...

  6. [Firefly引擎][学习笔记四][已完结]服务器端与客户端的通讯

    原地址:http://www.9miao.com/question-15-54981.html 传送门:学习笔记一学习笔记二学习笔记三 前言:学习笔记三是模块封装,这个在持续开发中会不断更新, 因为写 ...

  7. CI框架大纲总结

    一.大纲 1.codeigniter框架的授课内容安排 2.codeigniter框架的简介 |-----关于框架的概念 |-----使用CI框架的好处 |-----为什么选择CI框架 3.codei ...

  8. poj The Clocks 高斯消元

    由于数据量不大,所以这题有很多解法. 我用的是高斯消元化为逆矩阵解决的…… 代码如下: #include<stdio.h> #include<iostream> using n ...

  9. 【转】win7如何设置共享目录,并且访问不需要输入用户名和密码。

    1.打开guest帐号,guest帐号默认情况下是不启用的 进入控制面板->用户帐户->管理其他帐户->激活Gust用户 2,右击共享目录,属性->共享->共享-> ...

  10. DAO是什么技术

    DAO是Data Access Object数据访问接口,数据访问:故名思义就是与数据库打交道.夹在业务逻辑与数据库资源中间. 在核心J2EE模式中是这样介绍DAO模式的:为了建立一个健壮的J2EE应 ...