you are in: codestackercodes [RSS] → tag: date [RSS]

parse relative date Delicious Email

show/hide lines
   1  var d = Date.parse("Fri, 4 Dec 2008 15:13:00 +0000");
   2  var dateFunc = new Date();
   3  var timeSince = dateFunc.getTime() - d;
   4  var inSeconds = timeSince / 1000;
   5  var inMinutes = timeSince / 1000 / 60;
   6  var inHours = timeSince / 1000 / 60 / 60;
   7  var inDays = timeSince / 1000 / 60 / 60 / 24;
   8  var inYears = timeSince / 1000 / 60 / 60 / 24 / 365;
   9  
  10  if(Math.round(inSeconds) == 1){
  11  	document.write("1 second ago");
  12  }
  13  else if(inMinutes < 1.01){
  14  	document.write(Math.round(inSeconds) + " seconds ago");
  15  }
  16  
  17  else if(Math.round(inMinutes) == 1){
  18  	document.write("1 minute ago");
  19  }
  20  else if(inHours < 1.01){
  21  	document.write(Math.round(inMinutes) + " minutes ago");
  22  }
  23  
  24  else if(Math.round(inHours) == 1){
  25  	document.write("1 hour ago");
  26  }
  27  else if(inDays < 1.01){
  28  	document.write(Math.round(inHours) + " hours ago");
  29  }
  30  
  31  else if(Math.round(inDays) == 1){
  32  	document.write("1 day ago");
  33  }
  34  else if(inYears < 1.01){
  35  	document.write(Math.round(inDays) + " days ago");
  36  }
  37  
  38  else if(Math.round(inYears) == 1){
  39  	document.write("1 year ago");
  40  }
  41  else {
  42  document.write(Math.round(inYears) + " years ago");
  43  }
created by leozera — 20 April 2009 — get a short url — tags: date javascript parse embed

Chronic-like in Portuguese Delicious Email

show/hide lines
   1  require "rubygems"
   2  require "activesupport"
   3  require "unicode"
   4  
   5  class String
   6    def as_time
   7      str = self.dup
   8      str.downcase!
   9      str.squish!
  10      str = Unicode.normalize_KD(self).gsub(/[^\x00-\x7F]/n,'')
  11      
  12      now = Time.now
  13  
  14      if str == 'hoje'
  15        Date.today.to_time
  16      elsif str == 'agora'
  17        Time.now
  18      elsif str == 'amanha'
  19        Date.tomorrow.to_time
  20      elsif str == 'ontem'
  21        Date.yesterday.to_time
  22      elsif str == 'anteontem'
  23        2.days.ago
  24      elsif str == 'depois de amanha'
  25        2.days.from_now
  26      elsif str =~ /^proxim[oa] (semana|dia|mes|ano)$/
  27        case $1
  28          when 'semana' then now.next_week
  29          when 'dia' then Date.tomorrow.to_time
  30          when 'mes' then now.next_month
  31          when 'ano' then now.next_year
  32        end
  33      elsif str =~ /^(mes|semana|ano) que vem/
  34        case $1
  35          when 'mes' then now.next_month
  36          when 'semana' then now.next_week
  37          when 'ano' then now.next_year
  38        end
  39      elsif str =~ /^(mes|semana|ano) passad[ao]/
  40        case $1
  41          when 'mes' then now.last_month
  42          when 'semana' then now.beginning_of_week
  43          when 'ano' then now.last_year
  44        end
  45      elsif str =~ /^de (tarde|manha|noite)/
  46        case $1
  47          when 'noite' then now.beginning_of_day + 20.hours
  48          when 'tarde' then now.beginning_of_day + 15.hours
  49          when 'manha' then now.beginning_of_day + 9.hours
  50        end
  51      elsif str =~ /^daqui h?a pouco/
  52        1.hour.from_now
  53      elsif str =~ /^daqui(?: h?a)? ([0-9]+) (dias?|mes(?:es)?|semanas?|anos?|horas?|minutos?)/
  54        num = $1.to_i
  55        interval = $2.gsub(/e?s$/, '')
  56  
  57        names = {
  58          'dia' => :day,
  59          'mes' => :month,
  60          'semana' => :week,
  61          'ano' => :year,
  62          'hora' => :hour,
  63          'minuto' => :minute
  64        }
  65        
  66        num.send(names[interval]).from_now
  67      end
  68    end
  69  end
created by fnando — 02 July 2008 — get a short url — tags: activesupport chronic date normalization time embed