native-class-ext
Version:
js原生对象增加常用方法
372 lines (347 loc) • 9.96 kB
JavaScript
(function(){
require('./NumberExt.js')
const defineProperty = require('./defineProperty.js')
/**
* 对Date类进行扩展
* @author gwl
* @since 2020年4月29日13:06:27
* */
function init(){
/**
* 日期格式化
* @param {String} reg 格式,默认yyyy-MM-dd HH:mm:ss
* @example new Date().$format('yyyy-MM-dd HH:mm:ss ww');
* */
defineProperty(Date.prototype, '$format', format)
/**
* 格式秒
* */
defineProperty(Date, '$formatSeconds', formatSeconds)
//本地时间与服务器时间的差异,服务器时间-本地时间,单位毫秒
defineProperty(Date, '$serverTimeOffset', 0, true);
defineProperty(Date, '$newServerDate', newServerDate)
defineProperty(Date, '$nowServer', nowServer)
/**
* 返回10位时间戳
* */
defineProperty(Date, '$now10', now10)
/**
* 时间单位转换
* @example Date.$UNIT.HOURS.toMinutes(1)//小时转分钟
* */
defineProperty(Date, '$UNIT', UNIT)
/**
* date转成Date对象
* @param {String|Number|Date} date 需要转的值
* @param {Object} def 不能转时返回默认值,默认null
* @return {Date} 返回Date或def
* */
defineProperty(Date, '$parseDate', parseDate)
/**
* 复制一份副本
* */
defineProperty(Date.prototype, '$copy', copy)
/**
* 获取月最后一天
* @return {Date}
* */
defineProperty(Date.prototype, '$getMonthLastDay', getMonthLastDay)
/**
* 获取0点日期
* @return {Date} new Date(yyyy,mm,dd);
* */
defineProperty(Date.prototype, '$getZeroDate', getZeroDate)
/**
* 判断是否是今天
* @param {Date} date 指定基准日期,默认new Date()
* @param {Boolean} zeroDate date是否是0点
* */
defineProperty(Date.prototype, '$isToday', isToday)
/**
* 两个日期相差几天
* @param {Date} date
* @return {Number} this-date
* */
defineProperty(Date.prototype, '$differDay', differDay)
/**
* 下一个半小时,1:01=1:30、1:31=2:00
* */
defineProperty(Date.prototype, '$next30', next30)
/**
* 添加年
* @param {Nunber} num 添加年数,可负
* @param {Boolean} copy 是否在新的Date对象上添加,默认修改当前对象
* */
defineProperty(Date.prototype, '$addYear', addYear)
/**
* 添加月
* @param {Nunber} num 添加月数,可负
* @param {Boolean} copy 是否在新的Date对象上添加,默认修改当前对象
* */
defineProperty(Date.prototype, '$addMonth', addMonth)
/**
* 添加天
* @param {Nunber} day 添加天数,可负
* @param {Boolean} copy 是否在新的Date对象上添加,默认修改当前对象
* */
defineProperty(Date.prototype, '$addDay', addDay)
/**
* 添加小时
* @param {Nunber} num 添加数,可负
* @param {Boolean} copy 是否在新的Date对象上添加,默认修改当前对象
* */
defineProperty(Date.prototype, '$addHours', addHours)
/**
* 添加分钟
* @param {Nunber} num 添加数,可负
* @param {Boolean} copy 是否在新的Date对象上添加,默认修改当前对象
* */
defineProperty(Date.prototype, '$addMinutes', addMinutes)
/**
* 测试此日期是否在指定日期之前
* @param {Date} when 要比较的日期
* @return {Boolean} this<when
* */
defineProperty(Date.prototype, '$before', before)
/**
* 测试此日期是否在指定日期之后
* @param {Date} when 要比较的日期
* @return {Boolean} this>when
* */
defineProperty(Date.prototype, '$after', after)
}
function parseDate(date, def=null){
if(!date){
return def;
} else if(date.constructor.name == 'Date'){
return date;
} else if(date.constructor.name == 'Number'){
// if(date.toString().length == 10){
// date = date*1000;
// }
return new Date(date);
} else if(date.constructor.name == 'String'){
//"2020-08-11T13:43:32.011Z"
let d = new Date(date.replace(/-/g, '/').replace(/T/g, ' '));
if(!/Invalid|NaN/.test(d.toString())){
return d;
}
}
return def;
}
function copy(){
return new Date(this.getTime())
}
function format(reg='yyyy-MM-dd HH:mm:ss'){
return reg.replace(/\byyyy|yy|MM|M|dd|d|HH|H|mm|m|ss|s|S|ww\b/g, $1 => {
switch($1) {
case 'yyyy': return this.getFullYear();
case 'MM': return (this.getMonth() + 1).toString().padStart(2,0)
case 'dd': return this.getDate().toString().padStart(2,0)
case 'HH': return this.getHours().toString().padStart(2,0)
case 'mm': return this.getMinutes().toString().padStart(2,0)
case 'ss': return this.getSeconds().toString().padStart(2,0)
case 'ww': return ["周日","周一","周二","周三","周四","周五","周六"][this.getDay()];
case 'yy': return this.getFullYear().toString().substr(2);
case 'M': return this.getMonth() + 1;
case 'd': return this.getDate()
case 'H': return this.getHours()
case 'm': return this.getMinutes()
case 's': return this.getSeconds()
case 'S': return this.getMilliseconds()
}
})
};
function newServerDate(){
return new Date(nowServer());
}
function nowServer(){
return Date.now() + Date.$serverTimeOffset;
}
function now10(){
return parseInt(nowServer()/1000);
}
/**
* 获取月最后一天
* @return {Date}
* */
function getMonthLastDay(){
return new Date(this.getFullYear(), this.getMonth() + 1, 0)
}
/**
* 获取0点日期
* @return {Date} new Date(yyyy,mm,dd);
* */
function getZeroDate(){
return new Date(this.getFullYear(), this.getMonth(), this.getDate())
}
/**
* 判断是否是今天
* @param {Date} date 指定基准日期,默认new Date()
* @param {Boolean} zeroDate date是否是0点
* */
function isToday(date, zeroDate=false){
if(!date) date = Date.$newServerDate();
if(!zeroDate) {
date = date.$getZeroDate();
}
return this.$getZeroDate().getTime() == date.getTime();
}
/**
* 两个日期相差几天
* @param {Date} date
* @return {Number} this-date
* */
function differDay(date){
let differ = this.getTime()-date.getTime();
return Date.$UNIT.MILLISECONDS.toDays(differ)
}
/**
* 下一个半小时
* @param {Boolean} copy 是否在新的Date对象上添加,默认修改当前对象
* */
function next30(copy=false){
let date = this;
if(copy) {
date = this.$copy();
}
date.setSeconds(0);
if(date.getMinutes() < 30) {
date.setMinutes(30)
} else {
date.$addHours(1);
date.setMinutes(0)
}
return date;
}
/**
* 添加天
* @param {Nunber} num 添加天数,可负
* @param {Boolean} copy 是否在新的Date对象上添加,默认修改当前对象
* */
function addDay(num, copy=false){
let date = this;
if(copy) {
date = this.$copy();
}
date.setDate(this.getDate()+num);
return date;
}
/**
* 添加年
* @param {Nunber} num 添加年数,可负
* @param {Boolean} copy 是否在新的Date对象上添加,默认修改当前对象
* */
function addYear(num, copy=false){
let date = this;
if(copy) {
date = this.$copy();
}
date.setFullYear(this.getFullYear()+num);
return date;
}
/**
* 添加月
* @param {Nunber} num 添加月数,可负
* @param {Boolean} copy 是否在新的Date对象上添加,默认修改当前对象
* */
function addMonth(num, copy=false){
let date = this;
if(copy) {
date = this.$copy();
}
date.setMonth(this.getMonth()+num);
return date;
}
/**
* 添加小时
* @param {Nunber} num 添加月数,可负
* @param {Boolean} copy 是否在新的Date对象上添加,默认修改当前对象
* */
function addHours(num, copy=false){
let date = this;
if(copy) {
date = this.$copy();
}
date.setHours(this.getHours()+num);
return date;
}
/**
* 添加分钟
* @param {Nunber} num 添加月数,可负
* @param {Boolean} copy 是否在新的Date对象上添加,默认修改当前对象
* */
function addMinutes(num, copy=false){
let date = this;
if(copy) {
date = this.$copy();
}
date.setMinutes(this.getMinutes()+num);
return date;
}
/**
* 格式化秒,formatSeconds(61) = "1分1秒"
* */
function formatSeconds(seconds){
if(seconds<60) {
return seconds+'秒'
}
let str = '';
let temp = seconds;
str = temp%60 + '秒'
temp = parseInt(temp/60);
if(temp < 60) {
return temp+'分'+str
}
str = temp%60 + '分' + str
temp = parseInt(temp/60);
if(temp < 24) {
return temp+'小时'+str
}
str = temp%24 + '小时' + str
temp = parseInt(temp/24);
return temp+'天'+str
}
function getTimeUnit(val, convertName){
let num = new Number(val);
num.toMillis = function(d){
return d.$multiply(this);
};
num.toSeconds = function(d){
return this.toMillis(d).$divide(SECONDS)
}
num.toMinutes = function(d){
return this.toMillis(d).$divide(MINUTES)
}
num.toHours = function(d){
return this.toMillis(d).$divide(HOURS)
}
num.toDays = function(d){
return this.toMillis(d).$divide(DAYS)
}
// 从 u 转当前单位
num.convert = function(d, u){
return u[convertName](d);
}
return num;
}
const MILLISECONDS = getTimeUnit(1, 'toMillis');
const SECONDS = getTimeUnit(1000, 'toSeconds');
const MINUTES = getTimeUnit(60*SECONDS, 'toMinutes');
const HOURS = getTimeUnit(60*MINUTES, 'toHours');
const DAYS = getTimeUnit(24*HOURS, 'toDays');
const UNIT = {
MILLISECONDS,
SECONDS,
MINUTES,
HOURS,
DAYS
}
function before(when){
return this.getTime() < when.getTime();
}
function after(when){
return this.getTime() > when.getTime();
}
init();
})()