qualitativeDate()

qualitativeDate()

Version: 1.0 build 20040503
Requires: Adobe ColdFusion 5.0 or greater
Total size: 2.40 KB
Download time: 0.43 seconds at 56kbps
Edition: Freeware

Description

This function describes a date in qualitative terms. This can be useful for creative human-friendly interfaces. It can also be useful as a way of grouping data sorted by date.

Returns

A string.

Category

Date/Time

Parameters


Parameter Type Required? Default Description
date date Yes The date

UDF source


 
<cfscript>
  /**
  * Describe a date in qualitative terms relative to the present date
  * 
  * @param date             date (Required)
  * @return string
  * @author Matthew Walker, WWW.eswsoftware.com
  * @version 1, 2004-05-03
  */

  function qualitativeDate(date) {
  
    var today = Now();
    var yearsDifference = 0;
    var monthsDifference = 0;
    var weeksDifference = 0;
    var daysDifference = 0;
    
    if ( not structKeyExists(request, "eswsoftware") )
      request.eswsoftware = structNew();
    if ( not structKeyExists(request.eswsoftware, "qualitativeDate") )
      request.eswsoftware.qualitativeDate = structNew();  
    request.eswsoftware.qualitativeDate.version = 1;
	
    if ( not isDate(date) )
      return "";
      
    yearsDifference = year(today) - year(date);
    monthsDifference = month(today) - month(date);
    weeksDifference = week(today) - week(date);
    daysDifference = dayOfWeek(today) - dayOfWeek(date);
    
    if ( dateCompare(date, now()) lt 1 ) {
      if ( yearsDifference gt 2 )
        return "A long time ago";
      if ( yearsDifference eq 2 )
        return "Two years ago";
      if ( yearsDifference eq 1 )
        return "Last year";
      if ( monthsDifference gt 2 )
        return "Earlier this year";
      if ( monthsDifference eq 2 )
        return "Two months ago";
      if ( monthsDifference eq 1 )
        return "Last month";
      if ( weeksDifference gt 2 )
        return "Earlier this month";
      if ( weeksDifference eq 2 )
        return "Two weeks ago";
      if ( weeksDifference eq 1 )
        return "Last week";  
      if ( daysDifference gt 1 )
        return "Earlier this week";
      if ( daysDifference eq 1 )
        return "Yesterday";  
      return "Today";
    }    
    else {
      if ( yearsDifference lt -1 )
        return "Sometime in the future";
      if ( yearsDifference eq -1 )
        return "Next year";
      if ( monthsDifference lt -1 )
        return "Later this year";
      if ( monthsDifference eq -1 )
        return "Next month";
      if ( weeksDifference lt 0 )
        return "Later this month";
      if ( daysDifference lt -2 )
        return "Later this week";
      if ( daysDifference eq -2 )
        return "Two days from now";
      if ( daysDifference eq -1 )
        return "Tomorrow";  
      return "Today";  
    }    
  }    
  
</cfscript>
World Wide Web