1001

static P2P wikisoft.
Log | Files | Refs | README

desamber.js (1951B)


      1 function Desamber(str)
      2 {
      3   this.str = str.match(/\d\d[a-z\+]\d\d/i) ? str : "01+01";
      4 
      5   this.y = str.substr(0,2);
      6   this.m = str.substr(2,1).toUpperCase(); 
      7   this.d = str.substr(3,2);
      8 
      9   this.year = parseInt(`20${this.y}`);
     10   this.month = this.m == "+" ? 26 : this.m.charCodeAt(0) - 65;
     11   this.doty = (parseInt(this.month) * 14) + parseInt(this.d);
     12 
     13   this.date = new Date(this.year, 0).setDate(this.doty)
     14   this.offset = parseInt((this.date - new Date())/86400000)
     15 
     16   this.to_gregorian = function()
     17   {
     18     var d = this.to_date()
     19     return `${d.getFullYear()}-${prepend(d.getMonth()+1,2)}-${prepend(d.getDate(),2)}`;
     20   }
     21 
     22   this.to_date = function()
     23   {
     24     return new Date(this.date);
     25   }
     26 
     27   this.ago = function(cap = 9999)
     28   {
     29     var days = this.offset;
     30 
     31     if(-days > cap)  { return `${this.toString()}`; }
     32 
     33     if(days == -1)   { return `yesterday`; }
     34     if(days == 1)    { return "tomorrow"; }
     35     if(days == 0)    { return "today"; }
     36     if(days < -365)  { return `${parseInt(days/-365)} years ago`; }
     37     if(days < 1)     { return `${days*-1} days ago`; }
     38     return `in ${days} days`;
     39   }
     40 
     41   this.toString = function()
     42   {
     43     return this.str.toUpperCase();
     44   }
     45 
     46   function prepend(s,l,c="0"){ while(`${s}`.length < l){ s = `${c}${s}`; }; return s; }
     47 }
     48 
     49 Date.prototype.desamber = function()
     50 {
     51   var year = this.getFullYear()
     52   var start = new Date(year, 0, 0);
     53   var diff = (this - start) + ((start.getTimezoneOffset() - this.getTimezoneOffset()) * 60 * 1000);
     54   var doty = Math.floor(diff/86400000);
     55   var leap = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)
     56   var days = leap ? 366 : 365
     57 
     58   var y = year.toString().substr(2,2);
     59   var m = String.fromCharCode(97 + Math.floor(((doty)/days) * 26)).toUpperCase(); m = doty == 365 || doty == 366 ? "+" : m;
     60   var d = (doty % 14); d = d < 10 ? `0${d}` : d; d = d == "00" ? "14" : d; d = doty == 365 ? "01" : (doty == 366 ? "02" : d);
     61   
     62   return new Desamber(`${y}${m}${d}`);
     63 }