Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * zeroPad the given number to a provided length.
- * @param {Number} n The number to be padded.
- * @param {Number} l Length to pad the number
- * @return {String} The padded string
- */
- var zeroPad = function(n, l) {
- str = n.toString();
- for (; str.length < l;) {
- str = "0" + str;
- }
- return str;
- };
- 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;
- var MONTH_NAMES = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
- var DAY_NAMES = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
- var simplify = function(method, padding, next) {
- return function(query, timeStamp) {
- var h = timeStamp[method]();
- h = h.toString();
- return query.length == 2 ? zeroPad(h, padding) : query.length === 1 ? h : next(query, timeStamp, h);
- };
- };
- var replacer = {
- h: function(query, timeStamp) {
- var h = timeStamp.getHours() % 12;
- h = h.toString();
- return query.length == 2 ? zeroPad(h, 2) : h;
- },
- H: simplify("getHours", 2),
- m: simplify("getMinutes", 2),
- s: simplify("getSeconds", 2),
- n: simplify("getMilliseconds", 3),
- M: function(query, timeStamp) {
- var v = timeStamp.getMonth();
- switch (query.length) {
- case 1:
- return (v + 1).toString();
- case 2:
- return zeroPad((v + 1).toString(), 2);
- case 3:
- return MONTH_NAMES[v].substr(0, 3);
- case 4:
- return MONTH_NAMES[v];
- }
- },
- d: simplify("getDate", 2, function(query, timeStamp) {
- var v = timeStamp.getDay();
- if (query.length === 3) {
- return DAY_NAMES[v];
- }
- return DAY_NAMES[v].substr(0, 3);
- }),
- t: function(query, timeStamp) {
- return timeStamp.getHours() >= 12 ? "PM" : "AM";
- },
- y: function(query, timeStamp) {
- var withoutCentury = (timeStamp.getFullYear() % 100).toString();
- var v = timeStamp.getFullYear();
- return query.length === 1 ? withoutCentury : query.length === 2 ? zeroPad(withoutCentury, 2) : v.toString();
- }
- };
- /**
- * Returns a formatted date.
- * @param {String/Date/Number} timeStamp
- * @param {String} formatString
- * @return {String} The formatted date string.
- */
- /*
- formatString Syntax
- ===================
- #{v} is will be replaced by the appropriate value for v.
- v can be:
- h, hh: Hour in 12 hour format, with and without leading zero.
- H, HH: Hour in 24 hour format with & without leading zero.
- m, mm: minutes with and without leading zero.
- s, ss: seconds with and without leading zero.
- n, nn: milliseconds, with and without leading zero.
- M, MM: Month month with and without leading zero.
- MMM, MMMM: Name of the day of the month. MMM is the short name(eg: Jan, Feb) MMMM is the full name.
- d, dd: Day of the Month with and without the leading zero.
- ddd, dddd: Name of the day of the week. ddd is the short name and dddd is the full name.
- y,yy: year without century. with and without leading zero.
- yyyy: the full year.
- examples:
- formatTime(null, "#{dd}-#{MM}-#{yyyy}")
- formatTime("23 March 2012 12:30:00", "#{dd}-#{MM}-#{yyyy}")
- */
- var formatTime = function(timeStamp, formatString) {
- timeStamp = timeStamp || new Date();
- if (!(timeStamp instanceof Date)) {
- timeStamp = new Date(timeStamp);
- }
- return formatString.replace(FULL_REGEX, function() {
- var match = arguments;
- return replacer[match[1].substr(0, 1)](match[1], timeStamp);
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement