C#/ .NET未来相对时间戳?

时间:2011-08-30 18:32:01

标签: c# .net datetime timestamp

我正在寻找一些制作相对文字时间戳的方法,但使用的是未来而不是过去(不是“2天前”,而是“2天内”)。

我正在为我的个人用法做个人任务经理,我希望它告诉我“这项任务将在2天内完成”。但我似乎无法找到将DateTime转换为那种时间戳的任何内容。

6 个答案:

答案 0 :(得分:4)

这对你不起作用?...

DateTime myTask = DateTime.Now.AddDays(2.0);

更新

正如里德在下面的评论框中指出的那样,OP也可能正在寻找一种方法来告诉任务到期之前的时间或任务已到期的时间。我觉得这样的东西会起作用(注意我没有编译这段代码,但它应该给你一个好主意):

public string PrintTaskDueTime(DateTime taskTime, DateTime currTime)
{
    string result = string.Empty;
    TimeSpan timeDiff = TimeSpan.Zero;
    if(taskTime > currTime)
    {
        timeDiff = taskTime-currTime;
        result = String.Format("Your task is due in {0} days and {1} hours.", timeDiff.TotalDays, timeDiff.Hours);
    }
    else if(taskTime == currTime)
    {
        result = "Your task is due now!";
    }
    else
    {
        timeDiff = currTime-taskTime;
        result = String.Format("Your task is {0} days and {1} hours past due!", timeDiff.TotalDays, timeDiff.Hours);
    }

    return result;
}

所以只需通过指定任务时间和当前时间来调用它:PrintTimeDiff(taskTime, DateTime.Now);

我希望有所帮助。

答案 1 :(得分:2)

如果您的日期是DateTime,那么您可以使用TimeSpan来获取到期日期。例如:

TimeSpan dueDuration = dueDate - DateTime.Now;

Console.WriteLine("Due in {0} days and {1} hours.", dueDuration.TotalDays, dueDurations.Hours);

答案 2 :(得分:1)

未来的相对时间戳就像过去一样,但标志不同。

string RelativeTime(DateTime when)
{
     TimeSpan diff = when - DateTime.Now;
     var minutes = (int) diff.TotalMinutes;
     if (minutes == 1)
          return "A minute from now";

     if (minute == 2)
          return "In a couple minutes";

     if (minutes < 10)
          return "In not 10 minutes";

     if (minutes < 40)
          return "In about half an hour";

     /*  etc   */

}

/ * etc * / part是乏味的,需要创造力,但这取决于你。

答案 3 :(得分:0)

DateTime 类型用于表示特定时间点。例如, DateTime.Now

TimeSpan 用于表示特定的持续时间。例如, TimeSpan.FromDays(2)

有些运算符重载允许它们彼此很好地交互,例如,

DateTime dueDate = DateTime.Now + TimeSpan.FromDays(2);

答案 4 :(得分:0)

根据所有回复,我最终自己做了一个符合我所有要求的回复:

public static string RemainingTimeBeforeDateTime(DateTime dateTime, int level = 1)
{
    int howDeep = 0;
    string result = "";

    TimeSpan dueDuration = dateTime - DateTime.Now;

    double days = dueDuration.Days;
    double hours = dueDuration.Hours;
    double minutes = dueDuration.Minutes;

    if (days > 0)
    {
        howDeep++;
        result = days + "d ";
    }

    if (((howDeep != level) && (days != 0)) || ((days == 0) && (hours > 0)))
    {
        howDeep++;
        result = result + hours + "h ";
    }

    if (((howDeep != level) && (hours != 0)) || ((hours == 0) && (minutes > 0)))
    {
        result = result + minutes + "m ";
    }

    return result;
}

答案 5 :(得分:0)

这是我根据詹姆斯的回答放在一起的东西。支持过去,现在和未来:)

注意:它可能会缩短一点。

public static string RelativeTime(DateTime Date, string NowText = "Now")
{
    const int SECOND = 1;
    const int MINUTE = 60 * SECOND;
    const int HOUR = 60 * MINUTE;
    const int DAY = 24 * HOUR;
    const int MONTH = 30 * DAY;

    TimeSpan TimeSpan;
    double delta = 0d;

    //It's in the future
    if (Date > DateTime.UtcNow)
    {
        TimeSpan = new TimeSpan(Date.Ticks - DateTime.UtcNow.Ticks);
        delta = Math.Abs(TimeSpan.TotalSeconds);
        if (delta < 1 * MINUTE)
        {
            if (TimeSpan.Seconds == 0)
                return NowText;
            else
                return TimeSpan.Seconds == 1 ? "A second from now" : TimeSpan.Seconds + " seconds from now";
        }
        if (delta < 2 * MINUTE)
            return "A minute from now";
        if (delta < 45 * MINUTE)
            return TimeSpan.Minutes + " minutes from now";
        if (delta < 90 * MINUTE)
            return "An hour from now";
        if (delta < 24 * HOUR)
            return TimeSpan.Hours + " hours from now";
        if (delta < 48 * HOUR)
            return "Tomorrow";
        if (delta < 30 * DAY)
            return TimeSpan.Days + " days from now";
        if (delta < 12 * MONTH)
        {
            int months = Convert.ToInt32(Math.Floor((double)TimeSpan.Days / 30));
            return months <= 1 ? "A month from now" : months + " months from now";
        }
        else
        {
            int years = Convert.ToInt32(Math.Floor((double)TimeSpan.Days / 365));
            return years <= 1 ? "A year from now" : years + " years from now";
        }
    }
    //It's in the past
    else if (Date < DateTime.UtcNow)
    {
        TimeSpan = new TimeSpan(DateTime.UtcNow.Ticks - Date.Ticks);
        delta = Math.Abs(TimeSpan.TotalSeconds);
        if (delta < 1 * MINUTE)
        {
            if (TimeSpan.Seconds == 0)
                return NowText;
            else
                return TimeSpan.Seconds == 1 ? "A second ago" : TimeSpan.Seconds + " seconds ago";
        }
        if (delta < 2 * MINUTE)
            return "A minute ago";
        if (delta < 45 * MINUTE)
            return TimeSpan.Minutes + " minutes ago";
        if (delta < 90 * MINUTE)
            return "An hour ago";
        if (delta < 24 * HOUR)
            return TimeSpan.Hours + " hours ago";
        if (delta < 48 * HOUR)
            return "Yesterday";
        if (delta < 30 * DAY)
            return TimeSpan.Days + " days ago";
        if (delta < 12 * MONTH)
        {
            int months = Convert.ToInt32(Math.Floor((double)TimeSpan.Days / 30));
            return months <= 1 ? "A month ago" : months + " months ago";
        }
        else
        {
            int years = Convert.ToInt32(Math.Floor((double)TimeSpan.Days / 365));
            return years <= 1 ? "A year ago" : years + " years ago";
        }
    }
    //It's now
    else
        return NowText;
}