C#/.NET 未来相对时间戳?
我正在寻找某种方法来制作相对文本时间戳,但使用未来而不是过去(不是“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这对你不起作用?...
更新
正如里德在下面的评论框中指出的那样,OP也可能正在寻找一种方法来告诉任务到期之前的时间或任务已经过期的时间。我认为这样的东西会起作用(请注意,我还没有编译此代码,但它应该给您一个好主意):
所以只需通过指定任务时间和当前时间来调用它:
PrintTimeDiff(taskTime, DateTime.现在);
我希望有帮助。
This doesn't work for you?...
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):
So just call it by specifying the task time and the current time:
PrintTimeDiff(taskTime, DateTime.Now);
I hope that helps.
如果您在
DateTime
中有到期日期,则可以使用TimeSpan
获取到期时间。例如:If you have the date that it's due in a
DateTime
, then you can use aTimeSpan
to get the time until due. For example:DateTime 类型用于表示特定的时间点。例如,DateTime.Now。
TimeSpan 用于表示特定的时间长度。例如,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,
未来的相对时间戳就像过去一样,但符号不同。
/ * etc * / 部分很乏味并且需要创造力,但这取决于你。
A future relative timestamp is just like a past, but with the sign different.
The / * etc * / part is tedious and requires creativity, but that's up to you.
根据所有回复,我终于自己做了一个满足我所有要求的:
Based on all the replies I finally made one myself which meets all my requierements:
这是我根据詹姆斯的回答整理的一些内容。支持过去、现在和未来:)
注意:它可能可以再缩短一点。
Here's something I put together based on James' answer. Supports past, present, and future :)
Note: It can probably be shortened a bit more.