日期时间系列年月周分钟前 [英] DateTime series of Years months weeks and mins ago

查看:156
本文介绍了日期时间系列年月周分钟前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道挺exatly在我的例子做什么即时通讯,但我需要我的函数返回是重新psented这样的$ P $字符串

  1年,2个月
1年或
2个月或
2个月2周或
3mins前

如果有人知道如何待办事项这则请留下答案

 私人字符串GetTimeSpan(DateTime的creationDate)
{
    字符串时间跨度=;
    如果(Math.Floor(DateTime.Today.Subtract(creationDate).TotalDays / 365.25)GT; = 1)
    {
        时间跨度+ =((int)的Math.Floor(DateTime.Today.Subtract(creationDate).TotalDays / 365.25))的ToString()+年。
    }
    否则如果(Math.Floor(DateTime.Today.Subtract(creationDate).TotalDays / 365.25)。1)
    {
        时间跨度+ =((int)的Math.Floor(DateTime.Today.Subtract(creationDate).TotalDays / 365.25))的ToString()。
    }    返回的时间跨度;
}


解决方案

的System.DateTime 具有的 - 运营商超载,这需要两个的DateTime S(因为它是一个二元运算符),并返回一个时间跨度实例:

 时间跨度跨度= DateTime.Now  -  someOtherTime;

这应该让你一个时间跨度从而重新presents两个的DateTime 实例之间的时间。要打印出你想要的,你可以这样做一个字符串,通过扩展方法:

 公共静态字符串打印(此时间跨度P)
{
  VAR SB =新的StringBuilder();
  如果(p.Days> 365)
    sb.AppendFormat({0}年,p.Years / 365);
  如果(p.Days 365%→30)//硬code为30个月的间隔...
    sb.AppendFormat({0}个月,(p.Days%365)/ 30);
  如果(p.Days%365%30大于7)
    sb.AppendFormat({0}周,p.Days%365%30/7);
  如果(p.Days%365%30%7 0)
    sb.AppendFormat({0}天,p.Days%365%30%7);
  如果(p.Hours大于0)
    sb.AppendFormat({0}小时,p.Hours);
  // ... 等等 ...
  sb.Remove(sb.Length - 2,2); //删除最后一个,部分。
  返回sb.ToString();
}

然后你使用它像:

 字符串跨度=(DateTime.Now  -  creationDate).PRINT();

I don't know quite exatly what im doing in my example but what i need my function to return is a string that is represented like this

1yr, 2 months or
1yr or
2months or
2months 2weeks or
3mins ago

if someone knows how todo this then please leave a answer

private string GetTimeSpan(DateTime creationDate)
{
    string timespan = "";
    if (Math.Floor(DateTime.Today.Subtract(creationDate).TotalDays / 365.25) >= 1)
    {
        timespan += ((int)Math.Floor(DateTime.Today.Subtract(creationDate).TotalDays / 365.25)).ToString() + "yr, ";
    }
    else if (Math.Floor(DateTime.Today.Subtract(creationDate).TotalDays / 365.25) < 1)
    {
        timespan += ((int)Math.Floor(DateTime.Today.Subtract(creationDate).TotalDays / 365.25)).ToString();
    }

    return timespan;
}

解决方案

System.DateTime has the - operator overloaded, which takes two DateTimes (since it's a binary operator) and returns a TimeSpan instance:

TimeSpan span = DateTime.Now - someOtherTime;

That should get you a TimeSpan which represents the time between the two DateTime instances. To print out a string that you want, you can do something like this, via an extension method:

public static string Print(this TimeSpan p) 
{
  var sb = new StringBuilder();
  if(p.Days > 365)
    sb.AppendFormat("{0}yr, ", p.Years / 365);
  if(p.Days % 365 > 30) // hard-code 30 as month interval...
    sb.AppendFormat("{0}months, ", ( p.Days % 365 ) /30);
  if(p.Days % 365 % 30 > 7) 
    sb.AppendFormat("{0}weeks, ", p.Days % 365 % 30 / 7);
  if(p.Days % 365 % 30 % 7 > 0)
    sb.AppendFormat("{0}days, ", p.Days % 365 % 30 % 7);
  if(p.Hours > 0)
    sb.AppendFormat("{0}hr, ", p.Hours);
  // ... and so on ...
  sb.Remove(sb.Length - 2, 2); // remove the last ", " part.
  return sb.ToString();
}

Then you'd use it like:

string span = (DateTime.Now - creationDate).Print();

这篇关于日期时间系列年月周分钟前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆