开发者

Elegant Algorithm for conversational date strings? [duplicate]

开发者 https://www.devze.com 2022-12-08 08:03 出处:网络
This question already has answers here: Closed 13 years ago. Possible Duplicate: How do I calculate relative time?
This question already has answers here: Closed 13 years ago.

Possible Duplicate:

How do I calculate relative time?

Given a DateTime object, what's the most elegant way you've found of converting it to a human friendly string. Something like this:

(Future)开发者_如何学C

"Tomorrow"

"[1|2|3|4|5|6] days from now."

"Next week"

"Next month"

(Past)

"[1|2|3|4|5|6] days ago."

"Last week"

"Last month"

Thanks.


It's not necessarily the most elegant solution, but this blog article, as well as the the first comment, solve the problem as it pertains to dates in the past. (Copying and pasting the first comment requires replacing the fancy double quotes with plain-old double quotes.)


Stack Overflow just uses cascading if-thens I believe. Can't find the post right now (it's either here or on meta).

EDIT: Jason found it. Link in the comments on the question.

0

精彩评论

暂无评论...
验证码 换一张
取 消