Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function formatDate(date,format) {
- /**
- *
- * ex: formatDate(new date(), 'l, F S @ g:ia')
- * -> Tuesday, January 3rd @ 2:30pm
- *
- */
- format = (format ? format : 'F d');
- var unformatted = new Date(date);
- function addZero(n) {
- return n<10 ? '0'+n : n;
- }
- function ordinal(d) {
- if(d>3 && d<21) return 'th';
- switch (d % 10) {
- case 1: return "st";
- case 2: return "nd";
- case 3: return "rd";
- default: return "th";
- }
- }
- function ampm(h){
- return (h >= 12 ? "PM" : "AM");
- }
- function twelveHour(h){
- return (h > 12 ? (h-12) : ((h == 0 ? 12 : h)));
- }
- var days_full = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
- var days_3only = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
- var month_full = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
- var month_3only = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
- var date_constructs = {
- /**
- *
- * Time
- *
- */
- // 12-hour format without leading zeroes
- 'g' : twelveHour(unformatted.getHours()),
- // 23-hour format without leading zeroes
- 'G' : unformatted.getHours(),
- // 12-hour format with leading zeroes
- 'h' : addZero(twelveHour(unformatted.getHours())),
- // 23-hour format with leading zeroes
- 'H' : addZero(unformatted.getHours()),
- // minutes with leading zeroes
- 'i' : addZero(unformatted.getMinutes()),
- // am or pm
- 'a' : ampm(unformatted.getHours()).toLowerCase(),
- // AM or PM
- 'A' : ampm(unformatted.getHours()),
- /**
- *
- * Days
- *
- */
- // day of the week (number) - 0 is Sunday, 6 is Saturday
- 'w' : unformatted.getDay(),
- // day of the week (text, full) (Monday)
- 'l' : days_full[unformatted.getDay()],
- // day of the week (text, 3 letters) (Mon)
- 'D' : days_3only[unformatted.getDay()],
- // day of the month. 2 digits (01)
- 'd' : addZero(unformatted.getDate()),
- // day of the month. no leading zero (1)
- 'j' : unformatted.getDate(),
- // day of the month, ordinal. no leading zero, with suffix. (3rd)
- 'S' : unformatted.getDate() + ordinal(unformatted.getDate()),
- /**
- *
- * Months
- *
- */
- // month as names (January)
- 'F' : month_full[unformatted.getMonth()],
- // month - numeric with leading zeros (01)
- 'm' : addZero(unformatted.getMonth() + 1),
- // month - numeries without leading zeros (1)
- 'n' : (unformatted.getMonth() + 1),
- // month (text, 3 letters) (Jan)
- 'M' : month_3only[unformatted.getMonth()],
- /**
- *
- * Year
- *
- */
- // year (4 digits)
- 'Y' : unformatted.getFullYear(),
- // year (2 digits)
- 'y' : unformatted.getFullYear().toString().substr(-2),
- };
- var formatted = '';
- for (var i = 0; i <= format.length - 1; i++) {
- if(date_constructs[format[i]]===undefined){
- formatted+=format[i];
- } else {
- formatted+=date_constructs[format[i]];
- }
- }
- return formatted;
- }
Add Comment
Please, Sign In to add comment