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

simple date validation

function dateValidation() {

	var obj = document.getElementById("my_field"); // my_field: 12/12/2011

	var day = obj.value.split("/")[0];

	var month = obj.value.split("/")[1];
	var year = obj.value.split("/")[2];

	if ((day<1 || day >31) || (month<1&&month>12)&&(year.length != 4)) {
		alert("Invalid format");
		return false;
	} else {

		var dt = new Date(year, month-1, day);
		var today = new Date();

		if((dt.getDate() != day) || (dt.getMonth() != month-1) || (dt.getFullYear()!=year) || (dt>today)) {
			alert("Invalid date");
			return false;
		}
	}
}

parse relative date

var d = Date.parse("Fri, 4 Dec 2008 15:13:00 +0000");
var dateFunc = new Date();
var timeSince = dateFunc.getTime() - d;
var inSeconds = timeSince / 1000;
var inMinutes = timeSince / 1000 / 60;
var inHours = timeSince / 1000 / 60 / 60;
var inDays = timeSince / 1000 / 60 / 60 / 24;
var inYears = timeSince / 1000 / 60 / 60 / 24 / 365;

if(Math.round(inSeconds) == 1){
	document.write("1 second ago");
}
else if(inMinutes < 1.01){
	document.write(Math.round(inSeconds) + " seconds ago");
}

else if(Math.round(inMinutes) == 1){
	document.write("1 minute ago");
}
else if(inHours < 1.01){
	document.write(Math.round(inMinutes) + " minutes ago");
}

else if(Math.round(inHours) == 1){
	document.write("1 hour ago");
}
else if(inDays < 1.01){
	document.write(Math.round(inHours) + " hours ago");
}

else if(Math.round(inDays) == 1){
	document.write("1 day ago");
}
else if(inYears < 1.01){
	document.write(Math.round(inDays) + " days ago");
}

else if(Math.round(inYears) == 1){
	document.write("1 year ago");
}
else {
document.write(Math.round(inYears) + " years ago");
}

Chronic-like in Portuguese

require "rubygems"
require "activesupport"
require "unicode"

class String
  def as_time
    str = self.dup
    str.downcase!
    str.squish!
    str = Unicode.normalize_KD(self).gsub(/[^\x00-\x7F]/n,'')
    
    now = Time.now

    if str == 'hoje'
      Date.today.to_time
    elsif str == 'agora'
      Time.now
    elsif str == 'amanha'
      Date.tomorrow.to_time
    elsif str == 'ontem'
      Date.yesterday.to_time
    elsif str == 'anteontem'
      2.days.ago
    elsif str == 'depois de amanha'
      2.days.from_now
    elsif str =~ /^proxim[oa] (semana|dia|mes|ano)$/
      case $1
        when 'semana' then now.next_week
        when 'dia' then Date.tomorrow.to_time
        when 'mes' then now.next_month
        when 'ano' then now.next_year
      end
    elsif str =~ /^(mes|semana|ano) que vem/
      case $1
        when 'mes' then now.next_month
        when 'semana' then now.next_week
        when 'ano' then now.next_year
      end
    elsif str =~ /^(mes|semana|ano) passad[ao]/
      case $1
        when 'mes' then now.last_month
        when 'semana' then now.beginning_of_week
        when 'ano' then now.last_year
      end
    elsif str =~ /^de (tarde|manha|noite)/
      case $1
        when 'noite' then now.beginning_of_day + 20.hours
        when 'tarde' then now.beginning_of_day + 15.hours
        when 'manha' then now.beginning_of_day + 9.hours
      end
    elsif str =~ /^daqui h?a pouco/
      1.hour.from_now
    elsif str =~ /^daqui(?: h?a)? ([0-9]+) (dias?|mes(?:es)?|semanas?|anos?|horas?|minutos?)/
      num = $1.to_i
      interval = $2.gsub(/e?s$/, '')

      names = {
        'dia' => :day,
        'mes' => :month,
        'semana' => :week,
        'ano' => :year,
        'hora' => :hour,
        'minuto' => :minute
      }
      
      num.send(names[interval]).from_now
    end
  end
end
Displaying all 3 Code