AS3 Class for formatting date like PHP function date


/ Published in: ActionScript 3
Save to your folder(s)

A custom class for formatting dates, similar to PHP bult-in function "date". The class also has some other date utils functions, like: dayOfYear, numberOfDaysInMonth, etc..


Copy this code and paste it in your HTML
  1. package
  2. {
  3. public class DateFormat
  4. {
  5. public static var MONTHS:Array = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  6. public static var DAYS:Array = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  7.  
  8. /**
  9. * Receives a string with specific caracters and replace them with date information based on the Date Object passed.
  10. * @param formatStyle a string with or more of the caracters bellow (starting at line 15). <br />
  11. * @param date the Date object with the date you want to format
  12. *
  13. * The folowing caracters are the ones to be replaced:
  14. * <ul>
  15. * <li><strong>d:</strong> Day of the month, 2 digits with leading zeros. Ex.: 01 to 31</li>
  16. * <li><strong>D:</strong> A textual representation of a day, three letters. Ex.: Mon through Sun</li>
  17. * <li><strong>j:</strong> Day of the month without leading zeros. Ex.: 1 to 31</li>
  18. * <li><strong>l:</strong> A full textual representation of the day of the week. Ex.: Sunday through Saturday</li>
  19. * <li><strong>w:</strong> Numeric representation of the day of the week. Ex.: 0 (for Sunday) through 6 (for Saturday)</li>
  20. * <li><strong>z:</strong> The day of the year (starting from 0). Ex.: 0 through 365</li>
  21. * <li><strong>F:</strong> A full textual representation of a month, such as January or March. Ex.: January through December</li>
  22. * <li><strong>m:</strong> Numeric representation of a month, with leading zeros. Ex.: 01 through 12</li>
  23. * <li><strong>M:</strong> A short textual representation of a month, three letters. Ex.: Jan through Dec</li>
  24. * <li><strong>n:</strong> Numeric representation of a month, without leading zeros. Ex.: 1 through 12</li>
  25. * <li><strong>t:</strong> Number of days in the given month. Ex.: 28 through 31</li>
  26. * <li><strong>L:</strong> Whether it's a leap year. Ex.: 1 if it is a leap year, 0 otherwise.</li>
  27. * <li><strong>Y:</strong> A full numeric representation of a year, 4 digits. Ex.: 1999 or 2003.</li>
  28. * <li><strong>y:</strong> A two digit representation of a year. Ex.: 99 or 03.</li>
  29. * <li><strong>a:</strong> Lowercase Ante meridiem and Post meridiem. Ex.: am or pm.</li>
  30. * <li><strong>A:</strong> Uppercase Ante meridiem and Post meridiem. Ex.: AM or PM.</li>
  31. * <li><strong>g:</strong> 12-hour format of an hour without leading zeros. Ex.: 1 through 12.</li>
  32. * <li><strong>G:</strong> 24-hour format of an hour without leading zeros. Ex.: 0 through 23.</li>
  33. * <li><strong>h:</strong> 12-hour format of an hour with leading zeros. Ex.: 01 through 12.</li>
  34. * <li><strong>H:</strong> 24-hour format of an hour with leading zeros. Ex.: 00 through 23.</li>
  35. * <li><strong>i:</strong> Minutes with leading zeros. Ex.: 00 to 59.</li>
  36. * <li><strong>s:</strong> Seconds, with leading zeros. Ex.: 00 through 59.</li>
  37. * <li><strong>u:</strong> Milliseconds. Ex.: 654.</li>
  38. * <li><strong>U:</strong> Milliseconds since the Unix Epoch (January 1 1970 00:00:00 GMT). Ex.: 1309986224468.</li>
  39. * </ul>
  40. * @example
  41. * <pre>
  42. * var d:Date = new Date(2011, 7, 6, 14, 1, 58, 220);
  43. * trace(DateUtil.formatDate("z - l - d/m/Y = h:i:s:u A", d)); // outputs: "186 - Wednesday - 06/07/2011 - 02:01:58:220 PM"
  44. * </pre>
  45. * @see http://php.net/manual/en/function.date.php
  46. */
  47. public static function formatDate(formatStyle:String, date:Date):String
  48. {
  49. var dateFormated:String = formatStyle;
  50. dateFormated = dateFormated.replace(/d/g, leadZero(date.date));
  51. dateFormated = dateFormated.replace(/j/g, String(date.date));
  52. dateFormated = dateFormated.replace(/w/g, String(date.day));
  53. if(dateFormated.search("z") != -1) dateFormated = dateFormated.replace(/z/g, String(DateFormat.dayOfYear(date.month, date.date)));
  54. dateFormated = dateFormated.replace(/m/g, leadZero(date.month + 1));
  55. dateFormated = dateFormated.replace(/n/g, String(date.month + 1));
  56. if(dateFormated.search("t") != -1) dateFormated = dateFormated.replace(/t/g, String(DateFormat.numberOfDaysInMonth(date.month, DateFormat.leapYear(date.fullYear))));
  57. dateFormated = dateFormated.replace(/L/g, DateFormat.leapYear(date.fullYear) ? "1" : "0");
  58. dateFormated = dateFormated.replace(/Y/g, String(date.fullYear));
  59. dateFormated = dateFormated.replace(/y/g, String(date.fullYear).substr(2));
  60. dateFormated = dateFormated.replace(/g/g, date.hours <= 12 ? date.hours : date.hours - 12);
  61. dateFormated = dateFormated.replace(/G/g, date.hours);
  62. dateFormated = dateFormated.replace(/h/g, leadZero(date.hours <= 12 ? date.hours : date.hours - 12));
  63. dateFormated = dateFormated.replace(/H/g, leadZero(date.hours));
  64. dateFormated = dateFormated.replace(/i/g, leadZero(date.minutes));
  65. dateFormated = dateFormated.replace(/s/g, leadZero(date.seconds));
  66. dateFormated = dateFormated.replace(/u/g, leadZero(date.milliseconds));
  67. dateFormated = dateFormated.replace(/U/g, String(date.time));
  68.  
  69. dateFormated = dateFormated.replace(/a/g, "[{(a)}]");
  70. dateFormated = dateFormated.replace(/A/g, "[{(A)}]");
  71. dateFormated = dateFormated.replace(/D/g, "[{(D)}]");
  72. dateFormated = dateFormated.replace(/l/g, "[{(l)}]");
  73. dateFormated = dateFormated.replace(/M/g, "[{(M)}]");
  74. dateFormated = dateFormated.replace(/F/g, "[{(F)}]");
  75. dateFormated = dateFormated.replace(/\[\{\(a\)\}\]/g, date.hours < 12 ? "am" : "pm");
  76. dateFormated = dateFormated.replace(/\[\{\(A\)\}\]/g, date.hours < 12 ? "AM" : "PM");
  77. dateFormated = dateFormated.replace(/\[\{\(D\)\}\]/g, String(DateFormat.DAYS[date.day]).substr(0, 3));
  78. dateFormated = dateFormated.replace(/\[\{\(l\)\}\]/g, DateFormat.DAYS[date.day]);
  79. dateFormated = dateFormated.replace(/\[\{\(M\)\}\]/g, String(DateFormat.MONTHS[date.month]).substr(0, 3));
  80. dateFormated = dateFormated.replace(/\[\{\(F\)\}\]/g, DateFormat.MONTHS[date.month]);
  81.  
  82. function leadZero(num:int):String
  83. {
  84. return (num < 10) ? "0" + String(num) : String(num);
  85. }
  86. return dateFormated;
  87. }
  88. public static function dayOfYear(month:uint, day:uint):uint
  89. {
  90. if(month == 0) return day - 1;
  91. var total:uint = 0;
  92. for(var i:uint = 0; i < month; i++)
  93. {
  94. total += DateFormat.numberOfDaysInMonth(i);
  95. }
  96. return total + day - 1;
  97. }
  98. public static function leapYear(year:uint):Boolean
  99. {
  100. return (year % 4 == 0 && year % 100 != 0) || year % 100 == 0;
  101. }
  102. public static function numberOfDaysInMonth(month:uint, isLeapYear:Boolean = false):uint
  103. {
  104. if(month == 1)
  105. {
  106. return isLeapYear ? 29 : 28;
  107. }
  108. else if(month <= 6 && (month & 1) == 1)
  109. {
  110. return 30;
  111. }
  112. else if(month > 6 && (month & 1) == 0)
  113. {
  114. return 30;
  115. }
  116. return 31;
  117. }
  118. }
  119. }
  120.  
  121.  
  122.  
  123. //How to use
  124. var d:Date = new Date();//Now Date
  125.  
  126. //Outputs something like: 262 - Tuesday - 20/09/2011 - 02:58:05:236 PM
  127. trace(DateFormat.formatDate("z - l - d/m/Y - h:i:s:u A", d));
  128.  
  129. //Outputs something like: 09/20/2011 - 14:58:05
  130. trace(DateFormat.formatDate("m/d/Y - H:i:s", d));
  131.  
  132. //Outputs something like: Tuesday - 20th September 2011
  133. trace(DateFormat.formatDate("l - d", d) + "th " + DateFormat.formatDate("F Y", d));

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.