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

发布于 2024-12-01 22:29:10 字数 155 浏览 0 评论 0原文

我正在寻找某种方法来制作相对文本时间戳,但使用未来而不是过去(不是“2 天前”而是“2 天后”)。

我正在为我的个人用途制作一个个人任务管理器,我希望它告诉我“此任务将在 2 天内到期”。但我似乎找不到任何东西可以将 DateTime 转换为那种时间戳。

I'm looking for some way to make a relative text timestamp but using future instead of past (not "2 days ago" but "in 2 days").

I'm making a personal task manager for my personal usages and I'd like it to tell me "this task is due in 2 days". But I can't seem to find nothing to convert a DateTime to that kind of timestamp.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

请恋爱 2024-12-08 22:29:10

这对你不起作用?...

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.现在);

我希望有帮助。

This doesn't work for you?...

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

Update

As Reed pointed out in the comment box below, the OP might also be looking for a way to tell the time until the task is due or the time the task has been past due. I think something like this will work (note that I have not compiled this code, but it should give you a good idea):

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;
}

So just call it by specifying the task time and the current time: PrintTimeDiff(taskTime, DateTime.Now);

I hope that helps.

小猫一只 2024-12-08 22:29:10

如果您在 DateTime 中有到期日期,则可以使用 TimeSpan 获取到期时间。例如:

TimeSpan dueDuration = dueDate - DateTime.Now;

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

If you have the date that it's due in a DateTime, then you can use a TimeSpan to get the time until due. For example:

TimeSpan dueDuration = dueDate - DateTime.Now;

Console.WriteLine("Due in {0} days and {1} hours.", dueDuration.TotalDays, dueDurations.Hours);
も让我眼熟你 2024-12-08 22:29:10

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

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

有一些运算符重载可以使它们彼此良好地交互,例如,

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

The DateTime type is used to represent specific points in time. For example, DateTime.Now.

The TimeSpan is used to represent specific durations of time. For example, TimeSpan.FromDays(2).

There are operator overloads that allow them to interact nicely with one another, For example,

DateTime dueDate = DateTime.Now + TimeSpan.FromDays(2);
深海夜未眠 2024-12-08 22:29:10

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

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 * / 部分很乏味并且需要创造力,但这取决于你。

A future relative timestamp is just like a past, but with the sign different.

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   */

}

The / * etc * / part is tedious and requires creativity, but that's up to you.

感情废物 2024-12-08 22:29:10

根据所有回复,我终于自己做了一个满足我所有要求的:

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;
}

Based on all the replies I finally made one myself which meets all my requierements:

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;
}
走走停停 2024-12-08 22:29:10

这是我根据詹姆斯的回答整理的一些内容。支持过去、现在和未来:)

注意:它可能可以再缩短一点。

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;
}

Here's something I put together based on James' answer. Supports past, present, and future :)

Note: It can probably be shortened a bit more.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文