Skip to content

Instantly share code, notes, and snippets.

@jamlfy
Last active June 10, 2020 21:51
Show Gist options
  • Save jamlfy/2e4a0e9accc77a25c646349b1122c528 to your computer and use it in GitHub Desktop.
Save jamlfy/2e4a0e9accc77a25c646349b1122c528 to your computer and use it in GitHub Desktop.
Minimal Date
class Muyian extends Date {
to (date, locale) {
return this.diff(date, true);
}
form (date, locale) {
return this.diff(date, true);
}
to (date, locale) {
return this.diff(date, true);
}
form (date, locale) {
return this.diff(date, true);
}
isValid () {
return Muyian.isDate(this) && this.toString() !== "Invalid Date";
}
toObject () {
return Date.TIME.slice(0)
.reduce((res, e) => res = { ...res, [e] : this.get(e) }, {})
}
toArray () {
return Date.TIME.of.slice(0)
.map(e => this.get(e) );
}
toDate () {
return new Muyian(this.valueOf());
}
unix () {
return this / Muyian.TIME.second;
}
set (type, num) {
const str = 'year' == type ? 'fullYear' : type;
return new Muyian((Muyian.prototype['set' + str[0].toUpperCase() + str.slice(1) ]
|| Muyian.prototype['set' + str[0].toUpperCase() + str.slice(1) + 's' ])
.call(this, num));
}
get (type) {
const str = 'year' == type ? 'fullYear' : type;
return (Muyian.prototype['get' + str[0].toUpperCase() + str.slice(1) ]
|| Muyian.prototype['get' + str[0].toUpperCase() + str.slice(1) + 's' ]).call(this);
}
add (type, num) {
return this.set(type, this.get(type) + num );
}
subtract (type, num) {
return this.add(type, num * -1);
}
date (num) {
return !isNaN(parseFloat(num)) && isFinite(num)
? new Muyian( this.setDate(num) )
: this.getDate();
}
isoWeekday (num) {
return this.day(num + 1);
}
isoYear (num) {
return this.startOf('year').day(Muyian.TIME.weekStart).get('year');
}
week (num) {
const frist = this.startOf('year').date(Muyian.TIME.week - 1).day(Muyian.TIME.weekStart);
return !isNaN(parseFloat(num)) && isFinite(num)
? this.startOf('year')
.date(Muyian.TIME.week * (num - ( frist > Muyian.TIME.weekStart ? 1 : 0 )))
: Math.round(this.dayOfYear() / Muyian.TIME.week ) + ( frist > Muyian.TIME.weekStart ? 1 : 0 );
}
setWeek (num) {
return this.week(num);
}
getWeek (num) {
return this.week();
}
day (num) {
return !isNaN(parseFloat(num)) && isFinite(num)
? new Muyian( this.setDate( ( this.getDate() - this.getDay() ) + num ) )
: this.getDay();
}
setDay (num) {
return this.day(num);
}
dayOfYear (num) {
return !isNaN(parseFloat(num)) && isFinite(num)
? this.startOf('year').date(num)
: this.diff(this.startOf('year'), 'day');
}
quarter (num) {
const daysInYear = this.startOf('year').diff(this.endOf('year'), 'day');
return !isNaN(parseFloat(num)) && isFinite(num)
? this.dayOfYear(Math.round(daysInYear - (daysInYear / num) ))
: Math.round(this.dayOfYear() / ( daysInYear / 4 ), 'day');
}
getQuarter () {
return this.quarter();
}
setQuarter (num) {
return this.quarter(num);
}
daysInMonth () {
return new Muyian(this.getFullYear(), this.getMonth(), 0, 0, 0 ).date(0).getDate();
}
isLeapYear () {
return new Muyian(this.getFullYear(), 2, 0, 0, 0).date(0) === 29;
}
of (type, array) {
return Muyian.of.slice(0)
.slice(Muyian.of.slice(0).indexOf(type) + 1 ).reverse()
.reduce((res, e, i) => res.set(e,
!isNaN(parseFloat(array[i])) && isFinite(array[i])
? array[i]
: array[i](res)), new Muyian(this.valueOf()))
}
startOf (type) {
return this.of(type, [ 0, 0, 1, 0, 0, 0, 0 ].reverse());
}
endOf (type) {
return this.of(type, [ 3000, 11, ((e) => e.daysInMonth()) , 23, 59, 59, 999 ].reverse());
}
getTimezone () {
return this.getTimezoneOffset() / Muyian.TIME.minute;
}
getTimezoneGTM (sep) {
const P = this.getTimezoneOffset();
return Math.sign(P) + ''
+ (parseInt(P / Muyian.TIME.minute, 10) + 100).toString().substr(1)
+ ( !sep ? ':' : '' )
+ (parseInt(P % Muyian.TIME.minute, 10) + 100).toString().substr(1);
}
getNameTimezone () {
var tz = this.toLocaleTimeString(navigator.language, {timeZoneName: 'short'}).split(' ')
return tz[tz.length - 1];
}
diff (date, typeMax, notRound) {
let ty = '';
let diff = Muyian.of.slice(0)
.slice(Muyian.of.slice(0).indexOf(type) + 1).reverse()
.reduce(((res, e, i, arr) => ( ty = e, ( typeMax === true
&& Math.round( diff / Muyian.TIME[arr[i - 1]] ) <= 0 && res )
|| res / Muyian.TIME[e] ) ), new Muyian(date.valueOf()) - this );
diff = ( !notRound ? Math.round(diff) : diff ) * Math.sign(diff);
return typeMax === true
? diff + ' %' + ty
: diff;
}
format (format, locale) {
return format.replace(Muyian.format,
chr => ( Muyian.chr[chr[1]] && Muyian.chr[chr[1]](this, Muyian.LOCALE[locale] || locale || Muyian.LOCALE.default ) ) || chr );
}
}
Muyian.max = function (array) {
return new Muyian(Math.max.apply(null, array));
};
Muyian.min = function (array) {
return new Muyian(Math.min.apply(null, array));
};
Muyian.isDate = function (any) {
return any instanceof Date;
};
Muyian.isMuyian = function (any) {
return Muyian.isDate(any)
&& any instanceof Muyian;
};
Muyian.duration = function (num, type) {
return Muyian.of
.slice(0).reverse()
.slice(Muyian.of.slice(0).reverse().indexOf(type) )
.reduce(((res, e) => ((res || 1) * 1) * Muyian.TIME[e]), num )
};
Muyian.format = /\%./g;
Muyian.of = [ 'date', 'hour', 'minute', 'second', 'millisecond' ];
Muyian.chr = {
Z : (time) => time.getTimezone(),
P : (time) => time.getTimezoneGTM(),
T : (time) => time.getNameTimezone(),
j : (time) => time.getDate(),
w : (time) => time.getDay(),
z : (time) => time.dayOfYear(),
W : (time) => time.getWeek(),
t : (time) => time.daysInMonth(),
L : (time) => time.isLeapYear(),
r : (time) => time.toString(),
U : (time) => time.unix(),
G : (time) => time.getHours(),
Y : (time) => time.getFullYear(),
Q : (time) => time.getQueater(),
o : (time) => time.isoYear(),
c : (time) => time.format('%Y-%m-%d T%H:%i:%s%P'),
n : (time) => time.get('month') + 1,
N : (time) => time.day() || 7,
g : (time) => time.get('hour') % 12 || 12,
O : (time) => time.getTimezoneGTM(true),
e : (time) => Intl.DateTimeFormat().resolvedOptions().timeZone,
y : (time) => ('' + time.get('year')).substr(2),
m : (time) => (parseInt(time.get('month'), 10) + 101).toString().substr(1),
d : (time) => (parseInt(time.date(), 10) + 100).toString().substr(1),
h : (time) => (parseInt(time.g(), 10) + 100).toString().substr(1),
H : (time) => (parseInt(time.get('hour'), 10) + 100).toString().substr(1),
i : (time) => (parseInt(time.get('minute'), 10) + 100).toString().substr(1),
s : (time) => (parseInt(time.get('second'), 10) + 100).toString().substr(1),
v : (time) => (parseInt(time.get('millisecond'), 10) + 1000).toString().substr(1),
D : (time, locale) => locale.daysShort[time.day()],
l : (time, locale) => locale.daysLong[time.day()],
F : (time, locale) => locale.monthLong[time.get('month')],
M : (time, locale) => locale.monthShort[time.get('month')],
a : (time, locale) => locale.time[ (( time.get('hour') < 12 ) + 1 ) - 1 ].toLowerCase(),
A : (time, locale) => locale.time[ (( time.get('hour') < 12 ) + 1 ) - 1 ].toUpperCase(),
S : (time, locale) => {
var S = time.getDate();
return ( S % 10 <= 3 && !( 13 >= S && S >= 11 ) && locale.day[ ( S % 10 ) - 1 ] )
|| locale.day[ locale.day.length - 1 ];
}
};
Muyian.LOCALE = {
default : {
time : ['AM', 'PM'],
monthShort : Array.from(Array(12), (_, index) => (new Date((new Date).setMonth(index))).toLocaleString(navigator.language, { month : "long" })),
monthLong : Array.from(Array(12), (_, index) => (new Date((new Date).setMonth(index))).toLocaleString(navigator.language, { month : "short" })),
daysLong : Array.from(Array(7), (_, index) => (new Date((new Date).setDate(index))).toLocaleString(navigator.language, { weekday : "long" })),
daysShort : Array.from(Array(7), (_, index) => (new Date((new Date).setDate(index))).toLocaleString(navigator.language, { weekday : "short" })),
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment