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. Mapped Statements collection does not contain value for TaskMapper.selectByPrimaryKey

    Mapped Statements collection does not contain value for后面是什么类什么方法之类的: 错误原因有几种: 1.mapper.xml中没有加入name ...

  2. OPTICS光学算法

    package com.my.optics; public class DataPoint { private String name;//样本的名字 private double dimensioi ...

  3. js获取当前浏览器页面高度及宽度信息的方法

    var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft); var scroll ...

  4. 多智能体仿真环境 NetLogo

    http://ccl.northwestern.edu/netlogo/ 创建agentcreate-turtles 10 动一动ask turtle 0 [forward 10 right 90 l ...

  5. springmvc 数据对象回绑

    springmvc中,由页面 post到 controller,对象可以在form里面设置modelAttribute达到回绑的目的. 但是如果对象里面有复杂的非String,int的对象,则要在co ...

  6. 【数学/扩展欧几里得/线性求逆元】[Sdoi2008]沙拉公主的困惑

    Description 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M!互质的钞票.房地产第一大户沙拉公主决定预测一下大富翁国现 ...

  7. [转载]C#导入XLS数据到数据库

    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...

  8. EasyUI Datagrid 取编辑修改后的内容

    <script type="text/javascript"> $(function () { $('#tt').datagrid({ iconCls: 'icon-e ...

  9. spoj 62

    看了题解  自己好水   ...... #include <cstdio> #include <cstdlib> struct node { int x,y; }; node ...

  10. lib和dll文件的区别和联系

    什么是lib文件,lib和dll的关系如何 (2008-04-18 19:44:37)    (1)lib是编译时需要的,dll是运行时需要的. 如果要完成源代码的编译,有lib就够了. 如果也使动态 ...