Advertisement
rodMorMTL

date-format-tofte.js

Nov 10th, 2018
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function arrayExists(array, x) {
  2.     for (var i = 0; i < array.length; i++) {
  3.         if (array[i] == x) return true;
  4.     }
  5.     return false;
  6. }
  7.  
  8. Date.prototype.formatDate = function (input,time) {
  9.     // formatDate :
  10.     // a PHP date like function, for formatting date strings
  11.     // See: http://www.php.net/date
  12.     //
  13.     // input : format string
  14.     // time : epoch time (seconds, and optional)
  15.     //
  16.     // if time is not passed, formatting is based on
  17.     // the current "this" date object's set time.
  18.     //
  19.     // supported:
  20.     // a, A, B, d, D, F, g, G, h, H, i, j, l (lowercase L), L,
  21.     // m, M, n, O, r, s, S, t, U, w, W, y, Y, z
  22.     //
  23.     // unsupported:
  24.     // I (capital i), T, Z    
  25.  
  26.     var switches =    ["a", "A", "B", "d", "D", "F", "g", "G", "h", "H",
  27.                        "i", "j", "l", "L", "m", "M", "n", "O", "r", "s",
  28.                        "S", "t", "U", "w", "W", "y", "Y", "z"];
  29.     var daysLong =    ["Sunday", "Monday", "Tuesday", "Wednesday",
  30.                        "Thursday", "Friday", "Saturday"];
  31.     var daysShort =   ["Sun", "Mon", "Tue", "Wed",
  32.                        "Thu", "Fri", "Sat"];
  33.     var monthsShort = ["Jan", "Feb", "Mar", "Apr",
  34.                        "May", "Jun", "Jul", "Aug", "Sep",
  35.                        "Oct", "Nov", "Dec"];
  36.     var monthsLong =  ["January", "February", "March", "April",
  37.                        "May", "June", "July", "August", "September",
  38.                        "October", "November", "December"];
  39.     var daysSuffix = ["st", "nd", "rd", "th", "th", "th", "th", // 1st - 7th
  40.                       "th", "th", "th", "th", "th", "th", "th", // 8th - 14th
  41.                       "th", "th", "th", "th", "th", "th", "st", // 15th - 21st
  42.                       "nd", "rd", "th", "th", "th", "th", "th", // 22nd - 28th
  43.                       "th", "th", "st"];                        // 29th - 31st
  44.  
  45.     function a() {
  46.         // Lowercase Ante meridiem and Post meridiem
  47.         return self.getHours() > 11? "pm" : "am";
  48.     }
  49.     function A() {
  50.         // Uppercase Ante meridiem and Post meridiem
  51.         return self.getHours() > 11? "PM" : "AM";
  52.     }
  53.  
  54.     function B(){
  55.         // Swatch internet time. code simply grabbed from ppk,
  56.         // since I was feeling lazy:
  57.         // http://www.xs4all.nl/~ppk/js/beat.html
  58.         var off = (self.getTimezoneOffset() + 60)*60;
  59.         var theSeconds = (self.getHours() * 3600) +
  60.                          (self.getMinutes() * 60) +
  61.                           self.getSeconds() + off;
  62.         var beat = Math.floor(theSeconds/86.4);
  63.         if (beat > 1000) beat -= 1000;
  64.         if (beat < 0) beat += 1000;
  65.         if ((""+beat).length == 1) beat = "00"+beat;
  66.         if ((""+beat).length == 2) beat = "0"+beat;
  67.         return beat;
  68.     }
  69.    
  70.     function d() {
  71.         // Day of the month, 2 digits with leading zeros
  72.         return new String(self.getDate()).length == 1?
  73.         "0"+self.getDate() : self.getDate();
  74.     }
  75.     function D() {
  76.         // A textual representation of a day, three letters
  77.         return daysShort[self.getDay()];
  78.     }
  79.     function F() {
  80.         // A full textual representation of a month
  81.         return monthsLong[self.getMonth()];
  82.     }
  83.     function g() {
  84.         // 12-hour format of an hour without leading zeros
  85.         return self.getHours() > 12? self.getHours()-12 : self.getHours();
  86.     }
  87.     function G() {
  88.         // 24-hour format of an hour without leading zeros
  89.         return self.getHours();
  90.     }
  91.     function h() {
  92.         // 12-hour format of an hour with leading zeros
  93.         if (self.getHours() > 12) {
  94.           var s = new String(self.getHours()-12);
  95.           return s.length == 1?
  96.           "0"+ (self.getHours()-12) : self.getHours()-12;
  97.         } else {
  98.           var s = new String(self.getHours());
  99.           return s.length == 1?
  100.           "0"+self.getHours() : self.getHours();
  101.         }  
  102.     }
  103.     function H() {
  104.         // 24-hour format of an hour with leading zeros
  105.         return new String(self.getHours()).length == 1?
  106.         "0"+self.getHours() : self.getHours();
  107.     }
  108.     function i() {
  109.         // Minutes with leading zeros
  110.         return new String(self.getMinutes()).length == 1?
  111.         "0"+self.getMinutes() : self.getMinutes();
  112.     }
  113.     function j() {
  114.         // Day of the month without leading zeros
  115.         return self.getDate();
  116.     }    
  117.     function l() {
  118.         // A full textual representation of the day of the week
  119.         return daysLong[self.getDay()];
  120.     }
  121.     function L() {
  122.         // leap year or not. 1 if leap year, 0 if not.
  123.         // the logic should match iso's 8601 standard.
  124.         var y_ = Y();
  125.         if (        
  126.             (y_ % 4 == 0 && y_ % 100 != 0) ||
  127.             (y_ % 4 == 0 && y_ % 100 == 0 && y_ % 400 == 0)
  128.             ) {
  129.             return 1;
  130.         } else {
  131.             return 0;
  132.         }
  133.     }
  134.     function m() {
  135.         // Numeric representation of a month, with leading zeros
  136.         return self.getMonth() < 9?
  137.         "0"+(self.getMonth()+1) :
  138.         self.getMonth()+1;
  139.     }
  140.     function M() {
  141.         // A short textual representation of a month, three letters
  142.         return monthsShort[self.getMonth()];
  143.     }
  144.     function n() {
  145.         // Numeric representation of a month, without leading zeros
  146.         return self.getMonth()+1;
  147.     }
  148.     function O() {
  149.         // Difference to Greenwich time (GMT) in hours
  150.         var os = Math.abs(self.getTimezoneOffset());
  151.         var h = ""+Math.floor(os/60);
  152.         var m = ""+(os%60);
  153.         h.length == 1? h = "0"+h:1;
  154.         m.length == 1? m = "0"+m:1;
  155.         return self.getTimezoneOffset() < 0 ? "+"+h+m : "-"+h+m;
  156.     }
  157.     function r() {
  158.         // RFC 822 formatted date
  159.         var r; // result
  160.         //  Thu    ,     21          Dec         2000
  161.         r = D() + ", " + j() + " " + M() + " " + Y() +
  162.         //        16     :    01     :    07          +0200
  163.             " " + H() + ":" + i() + ":" + s() + " " + O();
  164.         return r;
  165.     }
  166.     function S() {
  167.         // English ordinal suffix for the day of the month, 2 characters
  168.         return daysSuffix[self.getDate()-1];
  169.     }
  170.     function s() {
  171.         // Seconds, with leading zeros
  172.         return new String(self.getSeconds()).length == 1?
  173.         "0"+self.getSeconds() : self.getSeconds();
  174.     }
  175.     function t() {
  176.  
  177.         // thanks to Matt Bannon for some much needed code-fixes here!
  178.         var daysinmonths = [null,31,28,31,30,31,30,31,31,30,31,30,31];
  179.         if (L()==1 && n()==2) return 29; // leap day
  180.         return daysinmonths[n()];
  181.     }
  182.     function U() {
  183.         // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
  184.         return Math.round(self.getTime()/1000);
  185.     }
  186.     function W() {
  187.         // Weeknumber, as per ISO specification:
  188.         // http://www.cl.cam.ac.uk/~mgk25/iso-time.html
  189.        
  190.         // if the day is three days before newyears eve,
  191.         // there's a chance it's "week 1" of next year.
  192.         // here we check for that.
  193.         var beforeNY = 364+L() - z();
  194.         var afterNY  = z();
  195.         var weekday = w()!=0?w()-1:6; // makes sunday (0), into 6.
  196.         if (beforeNY <= 2 && weekday <= 2-beforeNY) {
  197.             return 1;
  198.         }
  199.         // similarly, if the day is within threedays of newyears
  200.         // there's a chance it belongs in the old year.
  201.         var ny = new Date("January 1 " + Y() + " 00:00:00");
  202.         var nyDay = ny.getDay()!=0?ny.getDay()-1:6;
  203.         if (
  204.             (afterNY <= 2) &&
  205.             (nyDay >=4)  &&
  206.             (afterNY >= (6-nyDay))
  207.             ) {
  208.             // Since I'm not sure we can just always return 53,
  209.             // i call the function here again, using the last day
  210.             // of the previous year, as the date, and then just
  211.             // return that week.
  212.             var prevNY = new Date("December 31 " + (Y()-1) + " 00:00:00");
  213.             return prevNY.formatDate("W");
  214.         }
  215.        
  216.         // week 1, is the week that has the first thursday in it.
  217.         // note that this value is not zero index.
  218.         if (nyDay <= 3) {
  219.             // first day of the year fell on a thursday, or earlier.
  220.             return 1 + Math.floor( ( z() + nyDay ) / 7 );
  221.         } else {
  222.             // first day of the year fell on a friday, or later.
  223.             return 1 + Math.floor( ( z() - ( 7 - nyDay ) ) / 7 );
  224.         }
  225.     }
  226.     function w() {
  227.         // Numeric representation of the day of the week
  228.         return self.getDay();
  229.     }
  230.    
  231.     function Y() {
  232.         // A full numeric representation of a year, 4 digits
  233.  
  234.         // we first check, if getFullYear is supported. if it
  235.         // is, we just use that. ppks code is nice, but wont
  236.         // work with dates outside 1900-2038, or something like that
  237.         if (self.getFullYear) {
  238.             var newDate = new Date("January 1 2001 00:00:00 +0000");
  239.             var x = newDate .getFullYear();
  240.             if (x == 2001) {              
  241.                 // i trust the method now
  242.                 return self.getFullYear();
  243.             }
  244.         }
  245.         // else, do this:
  246.         // codes thanks to ppk:
  247.         // http://www.xs4all.nl/~ppk/js/introdate.html
  248.         var x = self.getYear();
  249.         var y = x % 100;
  250.         y += (y < 38) ? 2000 : 1900;
  251.         return y;
  252.     }
  253.     function y() {
  254.         // A two-digit representation of a year
  255.         var y = Y()+"";
  256.         return y.substring(y.length-2,y.length);
  257.     }
  258.     function z() {
  259.         // The day of the year, zero indexed! 0 through 366
  260.         var t = new Date("January 1 " + Y() + " 00:00:00");
  261.         var diff = self.getTime() - t.getTime();
  262.         return Math.floor(diff/1000/60/60/24);
  263.     }
  264.        
  265.     var self = this;
  266.     if (time) {
  267.         // save time
  268.         var prevTime = self.getTime();
  269.         self.setTime(time);
  270.     }
  271.    
  272.     var ia = input.split("");
  273.     var ij = 0;
  274.     while (ia[ij]) {
  275.         if (ia[ij] == "\\") {
  276.             // this is our way of allowing users to escape stuff
  277.             ia.splice(ij,1);
  278.         } else {
  279.             if (arrayExists(switches,ia[ij])) {
  280.                 ia[ij] = eval(ia[ij] + "()");
  281.             }
  282.         }
  283.         ij++;
  284.     }
  285.     // reset time, back to what it was
  286.     if (prevTime) {
  287.         self.setTime(prevTime);
  288.     }
  289.     return ia.join("");
  290. }
  291.  
  292. var date = new Date("1/1/2007 1:11:11");
  293.  
  294. for (i = 0; i < 500; ++i) {
  295.     var shortFormat = date.formatDate("Y-m-d");
  296.     var longFormat = date.formatDate("l, F d, Y g:i:s A");
  297.     date.setTime(date.getTime() + 84266956);
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement