2010-04-20 11 views
5

क्या कोई रूबी मणि है जो वर्तमान समय के सापेक्ष तिथियों को प्रारूपित करेगा? मुझे "कल 5 बजे अपराह्न", "गुरुवार को अगले सप्ताह 5:15 बजे" आउटपुट चाहिए, मैं सटीक आउटपुट के बारे में बहुत चिंतित नहीं हूं, जब तक कि प्राकृतिक भाषाप्रारूप सापेक्ष तिथियां

उत्तर

7

यदि आपके पास रेल हैं, मुझे लगता है कि ActionView::Helpers::DateHelper#distance_of_time_in_words इसकी मदद करता है।

require 'rubygems' 
require 'action_view' 
include ActionView::Helpers::DateHelper 

from_time = Time.now 
distance_of_time_in_words(from_time, from_time + 50.minutes)  # => about 1 hour 
distance_of_time_in_words(from_time, 50.minutes.from_now)   # => about 1 hour 
distance_of_time_in_words(from_time, from_time + 15.seconds)  # => less than a minute 
distance_of_time_in_words(from_time, from_time + 15.seconds, true) # => less than 20 seconds 
distance_of_time_in_words(from_time, 3.years.from_now)    # => over 3 years 
distance_of_time_in_words(from_time, from_time + 60.hours)   # => about 3 days 
distance_of_time_in_words(from_time, from_time + 45.seconds, true) # => less than a minute 
distance_of_time_in_words(from_time, from_time - 45.seconds, true) # => less than a minute 
distance_of_time_in_words(from_time, 76.seconds.from_now)   # => 1 minute 
distance_of_time_in_words(from_time, from_time + 1.year + 3.days) # => about 1 year 
distance_of_time_in_words(from_time, from_time + 4.years + 9.days + 30.minutes + 5.seconds) # => over 4 years 

to_time = Time.now + 6.years + 19.days 
distance_of_time_in_words(from_time, to_time, true)  # => over 6 years 
distance_of_time_in_words(to_time, from_time, true)  # => over 6 years 
distance_of_time_in_words(Time.now, Time.now)   # => less than a minute 

वर्तमान समय के सापेक्ष के मामले में, distance_of_time_in_words_to_now बजाय distance_of_time_in_words का उपयोग करें।

यदि आपका ऐप रेल आधारित है, तो बस distance_of_time_in_words, distance_of_time_in_words_to_now, time_ago_in_words देखें।

+0

बस मुझे क्या चाहिए, लेकिन रेल का उपयोग नहीं करना :(कोई विचार अगर रेल के घटक रेल के बाहर उपलब्ध कराए जाते हैं? –

+0

मुझे लगता है कि उपरोक्त कोड एक्शन पैक (मणि इंस्टॉल एक्शनपैक) स्थापित करने के बाद रेल के बिना काम करेगा, लेकिन नहीं सुनिश्चित करें। – unsouled

+0

@unsouled - पुष्टि की गई। – steenslag

-3

मणि 'पुरानी'

यह वास्तव में आप क्या पूछना करता है की कोशिश करो।

+1

पुरानी विपरीत विपरीत प्रतीत होती है: यह बार बार पाती है, यह उन्हें प्रारूपित नहीं करती है। –

संबंधित मुद्दे