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

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.