模糊日期算法 [英] Fuzzy date algorithm

查看:181
本文介绍了模糊日期算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种模糊日期算法。我刚开始写作时并意识到这是一个乏味的任务。这所有的一切迅速蜕变成了很多可怕code,以应付特殊情况类似的昨天的区别,最后一周和上月底可以(在某些情况下)指的是同一天,但分别正确的基础上今天的日期。

I'm looking for a fuzzy date algorithm. I just started writing one and realised what a tedious task it is. It quickly degenerated into a lot of horrid code to cope with special cases like the difference between "yesterday", "last week" and "late last month" all of which can (in some cases) refer to the same day but are individually correct based on today's date.

我觉得肯定必须有一个开放源码模糊日期格式化,但我不能找到它。理想情况下,我想的东西用的NSDate(OSX / iPhone)和其格式化,但事实并非难以位。有谁知道一个模糊的日期格式,现在服用的时间相对于任何和返回像一串(但不限于):

I feel sure there must be an open source fuzzy date formatter but I can't find it. Ideally I'd like something using NSDate (OSX/iPhone) and its formatters but that isn't the difficult bit. Does anyone know of a fuzzy date formatter taking any time period relative to now and returning a string like (but not limited to):

  • 在几分钟前
  • 在最后五分钟
  • 在今天早些时候
  • 今早
  • 昨晚
  • 在上周
  • 在上周三
  • 在上月初
  • 在去年六月
  • 在几年前
  • a few moments ago
  • in the last five minutes
  • earlier today
  • this morning
  • last night
  • last week
  • last wednesday
  • early last month
  • june last year
  • a couple of years ago

在一个理想的世界,我想该字符串是尽可能丰富(即返回随机变量的就在刚才,如刚才)。

In an ideal world I'd like the string to be as rich as possible (i.e. returning random variants on "Just a moment ago" such as "just now").

澄清。我在找的东西比基本buckts和字符串更加微妙。我想要的东西,知道昨天和上周三既可以指同一时期,但只有一个是正确的,当今天是星期四。

Clarification. I'm looking for something more subtle than basic buckts and strings. I want something that knows "yesterday" and "last wednesday" can both refer to the same period but only one is correct when today is Thursday.

推荐答案

这个问题应该让你开始。它具有code这个网站用来计算其相对时间。它可能没有你想要的特定范围,但他们很容易增加,一旦你得到了它的设置。

This question should get you started. It has the code this very site uses to calculate its relative time. It may not have the specific ranges you want, but they are easy enough to add once you got it setup.

这篇关于模糊日期算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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