x-date
Version:
JsDK of Date Class
312 lines (278 loc) • 10.2 kB
JavaScript
((_D,metaClass)=>{
var dateFormat = function () {
var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
timezoneClip = /[^-+\dA-Z]/g,
pad = function (val, len) {
val = String(val);
len = len || 2;
while (val.length < len) val = "0" + val;
return val;
};
// Regexes and supporting functions are cached through closure
return function (date, mask, utc) {
var dF = dateFormat;
// You can't provide utc if you skip other args (use the "UTC:" mask prefix)
if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) {
mask = date;
date = undefined;
}
// Passing date through Date applies Date.parse, if necessary
date = date ? new Date(date) : new Date;
if (isNaN(date)) throw SyntaxError("invalid date");
mask = String(dF.masks[mask] || mask || dF.masks["default"]);
// Allow setting the utc argument via the mask
if (mask.slice(0, 4) == "UTC:") {
mask = mask.slice(4);
utc = true;
}
var _ = utc ? "getUTC" : "get",
d = date[_ + "Date"](),
D = date[_ + "Day"](),
m = date[_ + "Month"](),
y = date[_ + "FullYear"](),
H = date[_ + "Hours"](),
M = date[_ + "Minutes"](),
s = date[_ + "Seconds"](),
L = date[_ + "Milliseconds"](),
o = utc ? 0 : date.getTimezoneOffset(),
flags = {
d: d,
dd: pad(d),
ddd: dF.i18n.dayNames[D],
dddd: dF.i18n.dayNames[D + 7],
m: m + 1,
mm: pad(m + 1),
mmm: dF.i18n.monthNames[m],
mmmm: dF.i18n.monthNames[m + 12],
yy: String(y).slice(2),
yyyy: y,
h: H % 12 || 12,
hh: pad(H % 12 || 12),
H: H,
HH: pad(H),
M: M,
MM: pad(M),
s: s,
ss: pad(s),
l: pad(L, 3),
L: pad(L > 99 ? Math.round(L / 10) : L),
t: H < 12 ? "a" : "p",
tt: H < 12 ? "am" : "pm",
T: H < 12 ? "A" : "P",
TT: H < 12 ? "AM" : "PM",
Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
};
return mask.replace(token, function ($0) {
return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
});
};
}();
// Some common format strings
dateFormat.masks = {
"default": "ddd mmm dd yyyy HH:MM:ss",
shortDate: "m/d/yy",
mediumDate: "mmm d, yyyy",
longDate: "mmmm d, yyyy",
fullDate: "dddd, mmmm d, yyyy",
shortTime: "h:MM TT",
mediumTime: "h:MM:ss TT",
longTime: "h:MM:ss TT Z",
isoDate: "yyyy-mm-dd",
isoTime: "HH:MM:ss",
isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
};
// Internationalization strings
dateFormat.i18n = {
dayNames: [
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
],
monthNames: [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
]
};
// For convenience...
_D.gmod=function(n,m){
return ((n%m)+m)%m;
}
_D.kuwaiticalendar=function(adjust){
var today = new _D();
if(adjust) {
adjustmili = 1000*60*60*24*adjust;
todaymili = today.getTime()+adjustmili;
today = new Date(todaymili);
}
day = today.getDate();
month = today.getMonth();
year = today.getFullYear();
m = month+1;
y = year;
if(m<3) {
y -= 1;
m += 12;
}
a = Math.floor(y/100.);
b = 2-a+Math.floor(a/4.);
if(y<1583) b = 0;
if(y==1582) {
if(m>10) b = -10;
if(m==10) {
b = 0;
if(day>4) b = -10;
}
}
jd = Math.floor(365.25*(y+4716))+Math.floor(30.6001*(m+1))+day+b-1524;
b = 0;
if(jd>2299160){
a = Math.floor((jd-1867216.25)/36524.25);
b = 1+a-Math.floor(a/4.);
}
bb = jd+b+1524;
cc = Math.floor((bb-122.1)/365.25);
dd = Math.floor(365.25*cc);
ee = Math.floor((bb-dd)/30.6001);
day =(bb-dd)-Math.floor(30.6001*ee);
month = ee-1;
if(ee>13) {
cc += 1;
month = ee-13;
}
year = cc-4716;
wd = _D.gmod(jd+1,7)+1;
iyear = 10631./30.;
epochastro = 1948084;
epochcivil = 1948085;
shift1 = 8.01/60.;
z = jd-epochastro;
cyc = Math.floor(z/10631.);
z = z-10631*cyc;
j = Math.floor((z-shift1)/iyear);
iy = 30*cyc+j;
z = z-Math.floor(j*iyear+shift1);
im = Math.floor((z+28.5001)/29.5);
if(im==13) im = 12;
id = z-Math.floor(29.5001*im-29);
var myRes = new Array(8);
myRes[0] = day; //calculated day (CE)
myRes[1] = month-1; //calculated month (CE)
myRes[2] = year; //calculated year (CE)
myRes[3] = jd-1; //julian day number
myRes[4] = wd-1; //weekday number
myRes[5] = id; //islamic date
myRes[6] = im-1; //islamic month
myRes[7] = iy; //islamic year
return myRes;
}
_D.writeIslamicDate=function(adjustment) {
var wdNames = new Array("Ahad","Ithnin","Thulatha","Arbaa","Khams","Jumuah","Sabt");
var iMonthNames = new Array("Muharram","Safar","Rabi'ul Awwal","Rabi'ul Akhir",
"Jumadal Ula","Jumadal Akhira","Rajab","Sha'ban",
"Ramadan","Shawwal","Dhul Qa'ada","Dhul Hijja");
var iDate = _D.kuwaiticalendar(adjustment);
var outputIslamicDate = wdNames[iDate[4]] + ", "
+ iDate[5] + " " + iMonthNames[iDate[6]] + " " + iDate[7] + " AH";
return outputIslamicDate;
}
metaClass.format = function (mask, utc) {
return dateFormat(this, mask, utc);
};
metaClass.next=function(arr,target){
var indx=-1;
if(!target){
arr.push(this)
}else{
var obj={};obj[target]=this;
arr.push(obj)
}
arr=arr.sort(function(a, b) {
if(!target){
return a - b;
}else{
return a[target] - b[target]
}
});
if(!target){
indx=arr.indexOf(this)
}else{
indx=arr.map(function(e){return e.target}).indexOf(this)
}
if(indx+1==arr.length){
return arr[0]
}else{
return arr[indx+1]
}
};
/**
* get duration between two dates (this & from ) in format
*/
metaClass.toHHMMSS=function(from){
return (((this.getTime()-from.getTime())/1000)+"").toHHMMSS()
// return (((from.getTime()-this.getTime())/1000)+"").toHHMMSS()
};
metaClass.clone=function(){
return new Date(this.getTime());
};
/**
* new Date().range({times:12,period:7,format:'dd/mm/yyyy'})
*
* --> [new Date()-7*12,new Date()-7*11,....,new Date()-7,new Date()]
*
* @param options={times:Length of Range,period: between each two elements ,format:Date formatting,asc: True if this is the beginning ,unit:DAYS or MIN or MONTH or YEAR..}
*/
metaClass.range=function(options){
var that=this;
return (1)['..'](options.times).map(function(e,i){
return that.addDays(-i*options.period).format(options.format)
}).reverse();
};
metaClass.addHours = function(h) {
return new Date(this.getTime() + (h*60*60*1000));
};
metaClass.addDays = function(d) {
return new Date(this.getTime()+(1000*60*60*24)*d);
};
metaClass.addWeeks = function(w) {
return this.addDays(w*7);
};
metaClass.addMonths = function(m) {
this.setMonth( this.getMonth( ) + m )
return this;
};
metaClass.addYears = function(y) {
this.setFullYear( this.getFullYear() + y )
return this;
};
metaClass.daysNext=function(nb){
if(!nb){nb=0};
return new Date(this.getTime() + (24 * 60 * 60 * 1000*nb));
};
metaClass.daysAgo=function(nb){
if(!nb){nb=0};
return _D.daysNext(-1 *nb);
};
metaClass.tomorrow=function(){
return this.daysNext(1);
};
metaClass.yesterday=function(){
return this.daysAgo(1);
};
_D.tomorrow=function(){
return _D.daysNext(1);
};
_D.yesterday=function(){
return _D.daysAgo(1);
};
_D.daysNext=function(nb){
if(!nb){nb=0};
return new _D(new _D().getTime() + (24 * 60 * 60 * 1000*nb));
};
_D.daysAgo=function(nb){
if(!nb){nb=0};
return _D.daysNext(-1 *nb);
};
})(Date,Date.prototype)