Date Util Class

 

Here I am going to post some common date utility functions.
These are most common functions which is required while playing with date Object
. fingerscrossed
1:  public class DateUtil    
2:  {     
3:  public static const millisecondsPerDay:int = 1000 * 60 * 60 * 24;    
4:  public static function parseDate(date:Date,dateFormat:String):String{   
5:      
6:      var fr:DateFormatter=new DateFormatter();   
7:      fr.formatString=dateFormat;   
8:      return fr.format(date);   
9:  }   
10:     
11:  public static function parseDateString(dateString:String):Date{   
12:      return new Date(Date.parse(dateString));   
13:  }   
14:     
15:     
16:  public static function compare (date1 : Date, date2 : Date) : Number{   
17:      var date1Timestamp : Number = date1.getTime ();   
18:      var date2Timestamp : Number = date2.getTime ();   
19:      var result : Number = -1;   
20:      if (date1Timestamp == date2Timestamp){    
21:         result = 0;   
22:      }  else if (date1Timestamp > date2Timestamp){   
23:         result = 1;   
24:      }   
25:     
26:      return result;   
27:  }   
28:     
29:  public static function getDaysDifference(startDate:Date,endDate:Date ):int {   
30:         var daysInMilliseconds:int = 1000*60*60*24;   
31:         return ( (endDate.time - startDate.time) / daysInMilliseconds );   
32:  }   
33:     
34:  public static function getDaysBetweenDates(date1:Date,date2:Date):int{   
35:        var count:int = 0;   
36:        if(date1 > date2) {   
37:        return count;   
38:  }   
39:  while(date1.toDateString() != date2.toDateString()){   
40:      date1 = addDays(date1,1);   
41:      //count only week days   
42:     if ( date1.day > 0 && date1.day < 6 ) {   
43:     count++;   
44:    }   
45:  }   
46:  return count;   
47:  }   
48:     
49:  // Add number of days in date   
50:  public static function addDays(date:Date,days:Number):Date{   
51:       if(date!=null){   
52:      var time:Number = date.getTime();   
53:      var offsetInMill:Number = days * millisecondsPerDay;   
54:      var newDate:Date = new Date();   
55:      newDate.setTime(time+offsetInMill);   
56:      return newDate;   
57:     }   
58:     return null;   
59:  }   
60:     
61:  // Subtract number of days in date   
62:  public static function subtractDays(date:Date,days:Number):Date{   
63:       if(date!=null){   
64:           var time:Number = date.getTime();   
65:           var offsetInMill:Number = days * millisecondsPerDay;   
66:           var newDate:Date = new Date();   
67:           newDate.setTime(time-offsetInMill);   
68:           return newDate;   
69:      }   
70:        return null;   
71:  }   
72:  // convert date object into string format separated by -   
73:  // dd-mm-yyyy   
74:  public static function convertDateToString(date:Date):String {   
75:     var month:Number = date.month + 1;   
76:     var dateString:String = date.date.toString() + '-'+month.toString()+'-'+date.fullYear.toString();   
77:     return dateString;   
78:  }   
79:     
80:  public static function daysInMonth(mon:Number):Number {   
81:      switch (mon) {   
82:      case 0:   
83:          return 31;   
84:      break;   
85:      case 1:   
86:          return 28;   
87:      break;   
88:      case 2:   
89:         return 31;   
90:      break;   
91:     case 3:   
92:        return 30;   
93:     break;   
94:    case 4:   
95:       return 31;   
96:    break;   
97:    case 5:   
98:       return 30;   
99:    break;  
100:    case 6:  
101:       return 31;  
102:    break;  
103:    case 7:  
104:        return 31;  
105:    break;  
106:    case 8:  
107:         return 30;  
108:    break;  
109:    case 9:  
110:        return 31;  
111:    break;  
112:    case 10:  
113:        return 30;  
114:    break;  
115:     case 11:  
116:         return 31;  
117:    break;  
118:   }  
119:       return 1;  
120:    }  
121:  }