AS3: Date / Time converter


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

Handy Date/Time converter: Convert Date Object to MySQL date format, Translate minutes into an English phrase, Convert MySQL date to Actionscript Date object, Get Day name from Date, Get name of Month from a Date Object, Input the seconds and return a string of the form: hours:mins:secs, Input the seconds and return a string of the form: mins:sec, Input seconds and return a string of the form: hours:min,


Copy this code and paste it in your HTML
  1. package com.polyGeek.utils {
  2. /*
  3. * Dan Florio, aka polyGeek
  4. */
  5. public class TronTime {
  6.  
  7. public function TronTime(){}
  8.  
  9. /**
  10. * Convert numeric time to English
  11. * Example: 71 --> 1 hour and 21 minutes
  12. */
  13. public static function convertTime_minutesToHoursMin( minutes:int ):String {
  14. var _hours:int = Math.floor( minutes / 60 );
  15. var _min:int = minutes % 60;
  16.  
  17. if( minutes == 0 ) {
  18. return "less than a minute";
  19. }
  20.  
  21. var _time:String;
  22. if( _hours > 0 ) { // get hours and mintutes
  23. _time = ( _hours > 1 ) ? _hours + " hours " : _hours + " hour ";
  24. if( _min > 0 ) {
  25. _time += ( _min > 1 ) ? "and " + _min + " minutes" : "and 1 minute";
  26. }
  27. } else { // just get minutes
  28. _time = ( _min > 1 ) ? _min + " minutes" : "1 minute";
  29. }
  30. return _time;
  31. }
  32.  
  33. /**
  34. * Get the name of the day from the date
  35. */
  36. public static function getDayNameFromDate( date:Date ):String {
  37. switch( date.day ) {
  38. case 0 :
  39. return 'Sunday';
  40. case 1 :
  41. return 'Monday';
  42. case 2 :
  43. return 'Tuesday';
  44. case 3 :
  45. return 'Wednesday';
  46. case 4 :
  47. return 'Thursday';
  48. case 5 :
  49. return 'Friday';
  50. case 6 :
  51. return 'Saturday';
  52. default :
  53. return '';
  54. }
  55. }
  56.  
  57. /**
  58. * Get the month name from the date
  59. */
  60. public static function getMonthNameFromDate( date:Date ):String {
  61. switch( date.month ) {
  62. case 0 :
  63. return 'January';
  64. case 1 :
  65. return 'February';
  66. case 2 :
  67. return 'March';
  68. case 3 :
  69. return 'April';
  70. case 4 :
  71. return 'May';
  72. case 5 :
  73. return 'June';
  74. case 6 :
  75. return 'July';
  76. case 7 :
  77. return 'August';
  78. case 8 :
  79. return 'September';
  80. case 9 :
  81. return 'October';
  82. case 10 :
  83. return 'November';
  84. case 11 :
  85. return 'December';
  86. default :
  87. return '';
  88. }
  89. }
  90.  
  91. /**
  92. * Get MySQL string from date
  93. */
  94. public static function getMySQLDate( date:Date ):String {
  95. var s:String = date.fullYear + '-';
  96.  
  97. // add the month
  98. if( date.month < 10 ) {
  99. s += '0' + ( date.month + 1 ) + '-';
  100. } else {
  101. s += ( date.month + 1 ) + '-';
  102. }
  103.  
  104. // add the day
  105. if( date.date < 10 ) {
  106. s += '0' + date.date;
  107. } else {
  108. s += date.date;
  109. }
  110.  
  111. return s;
  112. }
  113.  
  114. /**
  115. * Make a Date object from a MySQL date string
  116. */
  117. public static function convertMySQLDateToActionscript( s:String ):Date {
  118. var a:Array = s.split( '-' );
  119. return new Date( a[0], a[1] - 1, a[2] );
  120. }
  121.  
  122. /**
  123. * Convert an MySQL Timestamp to an Actionscript Date
  124. * Thanks to Pascal Brewing [email protected] for the beautiful simplicity.
  125. */
  126. public static function convertMySQLTimeStampToASDate( time:String ):Date{
  127. var pattern:RegExp = /[: -]/g;
  128. time = time.replace( pattern, ',' );
  129. var timeArray:Array = time.split( ',' );
  130. var date:Date = new Date( timeArray[0], timeArray[1]-1, timeArray[2],
  131. timeArray[3], timeArray[4], timeArray[5] );
  132. return date as Date;
  133. }
  134.  
  135. /**
  136. * Convert an MySQL Timestamp to an Actionscript Date
  137. * Thanks to Pascal Brewing [email protected] for the beautiful simplicity.
  138. */
  139. public static function convertASDateToMySQLTimestamp( d:Date ):String {
  140. var s:String = d.fullYear + '-';
  141. s += prependZero( d.month + 1 ) + '-';
  142. s += prependZero( d.day ) + ' ';
  143.  
  144. s += prependZero( d.hours ) + ':';
  145. s += prependZero( d.minutes ) + ':';
  146. s += prependZero( d.seconds );
  147.  
  148. return s;
  149. }
  150.  
  151. private static function prependZero( n:Number ):String {
  152. var s:String = ( n < 10 ) ? '0' + n : n.toString();
  153. return s;
  154. }
  155.  
  156. /********************************
  157. * The following three methods are useful for video player time displays
  158. ********************************/
  159.  
  160. /**
  161. * Input the seconds and return a string of the form: hours:mins:secs
  162. */
  163. public static function convertSecondsTo_HoursMinsSec( seconds:int ):String {
  164. var timeOut:String;
  165. var hours:int = int( seconds / 3600 );
  166. var mins:int = int( ( seconds - ( hours * 3600 ) ) / 60 )
  167. var secs:int = seconds % 60;
  168.  
  169. if( isNaN( hours ) || isNaN( mins ) || isNaN( secs ) ) {
  170. return "--:--:--";
  171. }
  172.  
  173. var minS:String = ( mins < 10 ) ? "0" + mins : String( mins );
  174.  
  175. var secS:String = ( secs < 10 ) ? "0" + secs : String( secs );
  176.  
  177. var hourS:String = String( hours );
  178. timeOut = hourS + ":" + minS + ":" + secS;
  179. return timeOut;
  180. }
  181.  
  182. /**
  183. * Input the seconds and return a string of the form: mins:sec
  184. */
  185. public static function convertSecondsTo_MinsSec( seconds:int ):String {
  186. var timeOut:String;
  187. var mins:int = int( seconds / 60 )
  188. var secs:int = seconds % 60;
  189.  
  190. if( isNaN( mins ) || isNaN( secs ) ) {
  191. return "--:--";
  192. }
  193.  
  194. var minS:String = ( mins < 10 ) ? "0" + mins : String( mins );
  195. var secS:String = ( secs < 10 ) ? "0" + secs : String( secs );
  196.  
  197. timeOut = minS + ":" + secS;
  198. return timeOut;
  199. }
  200.  
  201. /**
  202. * Input seconds and return a string of the form: hours:min
  203. */
  204. public static function getHoursMinutes( seconds:int ):String {
  205. var timeOut:String;
  206. var hours:int = int( seconds / 3600 );
  207. var mins:int = int( ( seconds - ( hours * 3600 ) ) / 60 )
  208. var secs:int = seconds % 60;
  209.  
  210. if( isNaN( hours ) || isNaN( mins ) || isNaN( secs ) ) {
  211. return "--:--";
  212. }
  213.  
  214. var minS:String = ( mins < 10 ) ? "0" + mins : String( mins );
  215.  
  216. var hourS:String = String( hours );
  217. timeOut = hourS + ":" + minS;
  218. return timeOut;
  219. }
  220. }
  221. }

URL: http://polygeek.com/2144_flex_actionscript-date-time-conversion-utilities

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.