1  /**
     2   * com.sekati.utils.DateUtils
     3   * @version 1.0.7
     4   * @author jason m horwitz | sekati.com
     5   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     6   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     7   */
     8   
     9  import com.sekati.convert.TimeConversion;
    10  import com.sekati.math.Range;
    11  
    12  /**
    13   * Static function for handling dates from db and converting them into readable strings. Note that days & months are 0-indexed.
    14   * 
    15   * {@code Usage:
    16   * var d = DateUtils.dateFromDB("2006-06-01 12:10:45");
    17   * trace(DateUtils.days[d.getDay()] + ", " + DateUtils.months[d.getMonth()] + " " + d.getDate() + ", " + (d.getHours()%12) + ":" + DateUtils.padTime(d.getMinutes()) + ((d.getHours() > 12) ? "pm" :"am"));
    18   * }
    19   */
    20  class com.sekati.utils.DateUtils {
    21  
    22  	/**
    23  	 * Gets 0 indexed array of months for use with Date.getMonth()
    24  	 * @return Array
    25  	 */
    26  	public static function get months():Array {
    27  		return new Array( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" );
    28  	}
    29  
    30  	/**
    31  	 * Gets 0 indexed array of short months for use with Date.getMonth()
    32  	 * @return Array
    33  	 */
    34  	public static function get shortmonths():Array {
    35  		return new Array( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" );	
    36  	}
    37  
    38  	/**
    39  	 * Gets 0 indexed array of days for use with Date.getDay()
    40  	 * @return Array
    41  	 */
    42  	public static function get days():Array {
    43  		return new Array( "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" );
    44  	}
    45  
    46  	/**
    47  	 * Get 0 indexed array of days for use with Date.getDay();
    48  	 * @return Array
    49  	 */
    50  	public static function get shortdays():Array {
    51  		return new Array( "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" );
    52  	}
    53  
    54  	/**
    55  	 * Get the month name by month number.
    56  	 * @param n (Number)
    57  	 * @return String
    58  	 * {@code Usage:
    59  	 * 	trace(DateUtils.getMonth(0); // returns "January"
    60  	 * }
    61  	 */
    62  	public static function getMonth(n:Number):String {
    63  		return months[n];	
    64  	}
    65  
    66  	/**
    67  	 * Get the short month name by month number.
    68  	 * @param n (Number)
    69  	 * @return String
    70  	 * {@code Usage:
    71  	 * 	trace(DateUtils.getShortMonth(0); // returns "Jan"
    72  	 * }
    73  	 */
    74  	public static function getShortMonth(n:Number):String {
    75  		return shortmonths[n];	
    76  	}
    77  
    78  	/**
    79  	 * Get the day name by day number.
    80  	 * @param n (Number)
    81  	 * @return String
    82  	 * {@code Usage:
    83  	 * 	trace(DateUtils.getDay(0); // returns "Sunday"
    84  	 * }
    85  	 */	
    86  	public static function getDay(n:Number):String {
    87  		return days[n];	
    88  	}
    89  
    90  	/**
    91  	 * Get the short day name by day number.
    92  	 * @param n (Number)
    93  	 * @return String
    94  	 * {@code Usage:
    95  	 * 	trace(DateUtils.getShortDay(0); // returns "Sun"
    96  	 * }
    97  	 */	
    98  	public static function getShortDay(n:Number):String {
    99  		return shortdays[n];
   100  	}
   101  
   102  	/**
   103  	 * Pads hours, Minutes or Seconds with a leading 0, 12:01 doesn't end up 12:1
   104  	 * @param n (Number)
   105  	 * @return String
   106  	 */
   107  	public static function padTime(n:Number):String {
   108  		return (String( n ).length < 2) ? ("0" + n) : n;
   109  	}
   110  
   111  	/**
   112  	 * converts a DB formatted date string into a Flash Date Object.
   113  	 * @param dbDate (String) date in YYYY-MM-DD HH:MM:SS format
   114  	 * @return Date
   115  	 */
   116  	public static function dateFromDB(dbdate:String):Date {
   117  		var tmp:Array = dbdate.split( " " );
   118  		var dates:Array = tmp[0].split( "-" );
   119  		var hours:Array = tmp[1].split( ":" );
   120  		var d:Date = new Date( dates[0], dates[1] - 1, dates[2], hours[0], hours[1], hours[2] );
   121  		return d;
   122  	}
   123  
   124  	/**
   125  	 * Takes 24hr hours and converts to 12 hour with am/pm.
   126  	 * @param hour24 (Number)
   127  	 * @return Object
   128  	 */
   129  	public static function getHoursAmPm(hour24:Number):Object {
   130  		var returnObj:Object = new Object( );
   131  		returnObj.ampm = (hour24 < 12) ? "am" : "pm";
   132  		var hour12:Number = hour24 % 12;
   133  		if (hour12 == 0) {
   134  			hour12 = 12;
   135  		}
   136  		returnObj.hours = hour12;
   137  		return returnObj;
   138  	}
   139  
   140  	/**
   141  	 * Get the differences between two Dates in milliseconds.
   142  	 * @param d1 (Date) 
   143  	 * @param d2 (Date) optional [default: now]
   144  	 * @return Number - difference between two dates in ms
   145  	 */
   146  	 
   147  	public static function dateDiff(d1:Date, d2:Date):Number {
   148  		if(d2 == null) d2 = new Date( );
   149  		return d2.getTime( ) - d1.getTime( );
   150  	}
   151  
   152  	/**
   153  	 * Check if birthdate entered meets required age.
   154  	 * @param year (Number)
   155  	 * @param month (Number)
   156  	 * @param day (Number)
   157  	 * @param requiredAge (Number)
   158  	 * @return Boolean
   159  	 */
   160  	public static function isValidAge(year:Number, month:Number, day:Number, requiredAge:Number):Boolean {
   161  		if (!isValidDate( year, month, day, true )) return false;
   162  		var now:Date = new Date( );
   163  		var bd:Date = new Date( year + requiredAge, month, day );
   164  		return (now.getTime( ) > bd.getTime( ));
   165  	}
   166  
   167  	/**
   168  	 * Check if a valid date can be created with inputs.
   169  	 * @param year (Number)
   170  	 * @param month (Number)
   171  	 * @param day (Number)
   172  	 * @param mustBeInPast (Boolean)
   173  	 * @return Boolean
   174  	 */
   175  	public static function isValidDate(year:Number, month:Number, day:Number, mustBeInPast:Boolean):Boolean {
   176  		if(!Range.isInRange( year, 1800, 3000 ) || isNaN( year )) return false;
   177  		if(!Range.isInRange( month, 0, 11 ) || isNaN( month )) return false;
   178  		if(!Range.isInRange( day, 1, 31 ) || isNaN( day )) return false;
   179  		if(day > getTotalDaysInMonth( year, month )) return false;
   180  		if(mustBeInPast && dateDiff( new Date( year, month, day ) ) < 0) return false;
   181  		return true;
   182  	}	
   183  
   184  	/**
   185  	 * Return the number of dates in a specific month.
   186  	 * @param year (Number)
   187  	 * @param month (Number)
   188  	 * @return Number
   189  	 */
   190  	public static function getTotalDaysInMonth(year:Number, month:Number):Number {
   191  		return TimeConversion.ms2days( dateDiff( new Date( year, month, 1 ), new Date( year, month + 1, 1 ) ) );
   192  	}
   193  
   194  	/**
   195  	 * Returns the number of days in a specific year.
   196  	 * @param year (Number)
   197  	 * @return Number
   198  	 */
   199  	public static function getTotalDaysInYear(year:Number):Number {
   200  		return TimeConversion.ms2days( dateDiff( new Date( year, 0, 1 ), new Date( year + 1, 0, 1 ) ) );
   201  	}
   202  
   203  	private function DateUtils() {
   204  	}
   205  }