Advertisement
menixator

formatTime.js

Jul 22nd, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * zeroPad the given number to a provided length.
  3.  * @param  {Number} n The number to be padded.
  4.  * @param  {Number} l Length to pad the number
  5.  * @return {String}   The padded string
  6.  */
  7. var zeroPad = function(n, l) {
  8.     str = n.toString();
  9.     for (; str.length < l;) {
  10.         str = "0" + str;
  11.     }
  12.     return str;
  13. };
  14.  
  15. var FULL_REGEX = /#\{(h{1,2}|H{1,2}|m{1,2}|M{1,4}|d{1,4}|t{1,2}|s{1,2}|n{1,2}|y{1,2}|y{4})\}/g;
  16.  
  17. var MONTH_NAMES = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  18. var DAY_NAMES = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  19. var simplify = function(method, padding, next) {
  20.     return function(query, timeStamp) {
  21.         var h = timeStamp[method]();
  22.         h = h.toString();
  23.         return query.length == 2 ? zeroPad(h, padding) : query.length === 1 ? h : next(query, timeStamp, h);
  24.     };
  25. };
  26. var replacer = {
  27.     h: function(query, timeStamp) {
  28.         var h = timeStamp.getHours() % 12;
  29.         h = h.toString();
  30.         return query.length == 2 ? zeroPad(h, 2) : h;
  31.     },
  32.     H: simplify("getHours", 2),
  33.     m: simplify("getMinutes", 2),
  34.     s: simplify("getSeconds", 2),
  35.     n: simplify("getMilliseconds", 3),
  36.     M: function(query, timeStamp) {
  37.         var v = timeStamp.getMonth();
  38.         switch (query.length) {
  39.             case 1:
  40.                 return (v + 1).toString();
  41.             case 2:
  42.                 return zeroPad((v + 1).toString(), 2);
  43.             case 3:
  44.                 return MONTH_NAMES[v].substr(0, 3);
  45.             case 4:
  46.                 return MONTH_NAMES[v];
  47.         }
  48.     },
  49.     d: simplify("getDate", 2, function(query, timeStamp) {
  50.         var v = timeStamp.getDay();
  51.         if (query.length === 3) {
  52.             return DAY_NAMES[v];
  53.         }
  54.         return DAY_NAMES[v].substr(0, 3);
  55.     }),
  56.     t: function(query, timeStamp) {
  57.         return timeStamp.getHours() >= 12 ? "PM" : "AM";
  58.     },
  59.     y: function(query, timeStamp) {
  60.         var withoutCentury = (timeStamp.getFullYear() % 100).toString();
  61.         var v = timeStamp.getFullYear();
  62.         return query.length === 1 ? withoutCentury : query.length === 2 ? zeroPad(withoutCentury, 2) : v.toString();
  63.     }
  64.  
  65. };
  66. /**
  67.  * Returns a formatted date.
  68.  * @param  {String/Date/Number} timeStamp
  69.  * @param  {String} formatString
  70.  * @return {String}              The formatted date string.
  71.  */
  72. /*
  73. formatString Syntax
  74. ===================
  75. #{v} is will be replaced by the appropriate value for v.
  76. v can be:
  77.     h, hh: Hour in 12 hour format, with and without leading zero.
  78.     H, HH: Hour in 24 hour format with & without leading zero.
  79.     m, mm: minutes with and without leading zero.
  80.     s, ss: seconds with and without leading zero.
  81.     n, nn: milliseconds, with and without leading zero.
  82.     M, MM: Month month with and without leading zero.
  83.     MMM, MMMM: Name of the day of the month. MMM is the short name(eg: Jan, Feb) MMMM is the full name.
  84.     d, dd: Day of the Month with and without the leading zero.
  85.     ddd, dddd: Name of the day of the week. ddd is the short name and dddd is the full name.
  86.     y,yy: year without century. with and without leading zero.
  87.     yyyy: the full year.
  88.  
  89. examples:
  90.     formatTime(null, "#{dd}-#{MM}-#{yyyy}")
  91.     formatTime("23 March 2012 12:30:00", "#{dd}-#{MM}-#{yyyy}")
  92.  */
  93. var formatTime = function(timeStamp, formatString) {
  94.     timeStamp = timeStamp || new Date();
  95.     if (!(timeStamp instanceof Date)) {
  96.         timeStamp = new Date(timeStamp);
  97.     }
  98.     return formatString.replace(FULL_REGEX, function() {
  99.         var match = arguments;
  100.         return replacer[match[1].substr(0, 1)](match[1], timeStamp);
  101.     });
  102. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement