How to Convert a Date Time to “X minutes ago” in C# z
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的更多相关文章
- Unable to convert MySQL date/time value to System.DateTime 错误
C#读取MySql时,如果存在字段类型为date/datetime时的可能会出现以下问题“Unable to convert MySQL date/time value to System.DateT ...
- unable to convert MySQL date/time value to System.DateTime
今天 用C# MySql做项目的时候 遇到了 unable to convert MySQL date/time value to System.DateTime 这样的异常错误,这个原因是因为:表里 ...
- 异常-----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 ...
- 每日踩坑 2018-12-25 【Unable to convert MySQL date/time value to System.DateTime】异常
之前的公司一直是用 ORM ,所以底层的事情完全不用在意, 到了这家全是似乎全是 SQL 硬编码 所以也有机会重新去补补以前也没怎么写过的基本 ADO. 不过这两天总有自己写个简易ORM的想法在蠢蠢欲 ...
- 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 ...
- Unable to convert MySQL date/time value to System.DateTime
当使用.NET connector 连接MYSQL数据库,检索某些数据的时候,你可能会得到一个错误信息: "Unable to convert MySQL date/time value t ...
- 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 ...
- 【.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 ...
- 解决 Unable to convert MySQL date/time value to System.DateTime
C#读取MySql时,如果存在字段类型为date/datetime时的可能会出现以下问题“Unable to convert MySQL date/time value to System.DateT ...
随机推荐
- C++ 字符串各种处理
要想使用标准C++中string类,必须要包含 #include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件 ...
- visio
为您的产品密钥: 7DNWX-MRX4G-QCGX2-DG6BP-DC8RP http://technet.microsoft.com/zh-cn/evalcenter/hh973399.aspx ...
- Delphi XE5 android 黑屏的临时解决办法
下载style 然后在deployment里添加进去 http://files.cnblogs.com/nywh2008/styles.rar 在AndroidManifest.tem ...
- 搭建 Android 开发环境,初试HelloWorld (win7) (上) (转)
搭建Android开发环境主要有以下几步要做: 1.JDK安装 2.Eclipse安装 3.Android SDK安装 4.ADT安装 5.创建AVD 1.JDK(Java Development K ...
- 如何编写敏捷开发中的user story
http://blog.csdn.net/chengyb74/article/details/4762247 对于敏捷开发来说,User Story是开发的基础,它不同于传统的瀑布式开发方式,而是把原 ...
- Samza文档翻译 : Concepts
此页介绍啊Samza的一些高层级概念. Streams Samza处理Streams(流).流由同一类型的不可变的消息组成.例如,一个流可以是对一个网站的所有点击,或者对一个数据库表的所有更新,或者一 ...
- Cloud Insight 和 BearyChat 第一次合体,好紧张!
说到 ChatOps 我们可能立刻想到是 Slack(啥?没听过?哦!),但是由于国内网络和语言的问题你可能无法拥有很好的体验了.那就把目光转回国内吧,国内的话就不得不提到 BearyChat 等 C ...
- 利用CCProxy管理小型企业的上网行为
本实验以实例方式,从操作条件.背景.需求.以及具体要求的几个部分进行说明. 1. 操作条件: 装有Windows Server 2003系统,安装了代理服务程序的虚拟机一台 2. 背景: 为了提高员工 ...
- JavaScript动态操作style
1.易错:修改元素的样式不是设置class属性,而是className属性.class是JS的一个保留关键字. 2.易错:单独修改样式的属性使用"style.属性名"3.注意在cs ...
- [译]再次对比TCP与UDP
免责声明:和往常一样,此文章的观点都属于‘No Bugs’Hare(译注:一个网站) ,也许不一定和翻译者或者Overload编辑的意见一致.同时,翻译者从Lapine翻译到英语也具有一定的难度.除此 ...