提交 d7b7b1f4 编写于 作者: C Catouse

* refactor date and string helpers.

上级 c56faadb
......@@ -5,242 +5,411 @@
* ========================================================================
* Copyright 2014-2020 cnezsoft.com; Licensed MIT
* ======================================================================== */
(function() {
(function(window) {
'use strict';
/**
* Ticks of a whole day
* @type {number}
*/
Date.ONEDAY_TICKS = 24 * 3600 * 1000;
const ONEDAY_TICKS = 24 * 3600 * 1000;
/**
* Format date to a string
*
* @param string format
* @return string
*/
if(!Date.prototype.format) {
Date.prototype.format = function(format) {
var date = {
'M+': this.getMonth() + 1,
'd+': this.getDate(),
'h+': this.getHours(),
'm+': this.getMinutes(),
's+': this.getSeconds(),
'q+': Math.floor((this.getMonth() + 3) / 3),
'S+': this.getMilliseconds()
};
if(/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for(var k in date) {
if(new RegExp('(' + k + ')').test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ('00' + date[k]).substr(('' + date[k]).length));
}
* Create a Date instance
* @param {Date|String|Number} date Date expression
* @return {Date}
*/
const createDate = function(date) {
if (!(date instanceof Date)) {
if (typeof date === 'number' && date < 10000000000) {
date *= 1000;
}
return format;
};
}
date = new Date(date);
}
return date;
};
/**
* Add milliseconds to the date
* @param {number} value
* Get timestamp of a Date
* @param {Date|String|Number} date Date expression
* @return {number}
*/
if(!Date.prototype.addMilliseconds) {
Date.prototype.addMilliseconds = function(value) {
this.setTime(this.getTime() + value);
return this;
};
}
const getTimestamp = function(date) {
return createDate(date).getTime();
};
/**
* Add days to the date
* @param {number} days
* Format date to a string
*
* @param {Date|String|Number} Date date expression
* @param {string} [format='yyyy-MM-dd hh:mm:ss'] Date format string
* @return {string}
*/
if(!Date.prototype.addDays) {
Date.prototype.addDays = function(days) {
this.addMilliseconds(days * Date.ONEDAY_TICKS);
return this;
const formatDate = function(date, format) {
date = createDate(date);
if (format === undefined) {
format = 'yyyy-MM-dd hh:mm:ss';
}
var map = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds(),
'q+': Math.floor((date.getMonth() + 3) / 3),
'S+': date.getMilliseconds()
};
}
if(/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for(var k in map) {
if(new RegExp('(' + k + ')').test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? map[k] : ('00' + map[k]).substr(('' + map[k]).length));
}
}
return format;
};
/**
* Add milliseconds to the date
* @param {Date} Date date
* @param {number} milliseconds milliseconds value
* @return {Date}
*/
const addMilliseconds = function(date, milliseconds) {
date.setTime(date.getTime() + milliseconds);
return date;
};
/**
* Clone a new date instane from the date
* Add milliseconds to the date
* @param {Date} date date
* @param {number} days days value
* @return {Date}
*/
if(!Date.prototype.clone) {
Date.prototype.clone = function() {
var date = new Date();
date.setTime(this.getTime());
return date;
};
}
const addDays = function(date, days) {
return addMilliseconds(date, days * ONEDAY_TICKS);
};
/**
* Clone date to a new instance
* @param {Date|String|Number} date date expression
*/
const cloneDate = function(date) {
return new Date(createDate(date).getTime());
};
/**
* Judge the year is in a leap year
* @param {integer} year
* @return {Boolean}
* @param {number} year
* @return {boolean}
*/
if(!Date.isLeapYear) {
Date.isLeapYear = function(year) {
return(((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
};
}
if(!Date.getDaysInMonth) {
/**
* Get days number of the date
* @param {integer} year
* @param {integer} month
* @return {integer}
*/
Date.getDaysInMonth = function(year, month) {
return [31, (Date.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
};
}
const isLeapYear = function(year) {
return ((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0);
};
/**
* Judge the date is in a leap year
* @return {Boolean}
* Get days number of the date
* @param {number} year
* @param {number} month
* @return {number}
*/
if(!Date.prototype.isLeapYear) {
Date.prototype.isLeapYear = function() {
return Date.isLeapYear(this.getFullYear());
};
}
const getDaysInMonth = function(year, month) {
return [31, (isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
};
/**
* Clear time part of the date
* @return {date}
*/
if(!Date.prototype.clearTime) {
Date.prototype.clearTime = function() {
this.setHours(0);
this.setMinutes(0);
this.setSeconds(0);
this.setMilliseconds(0);
return this;
};
}
* Get days number of the date
* @param {Date} date date
* @return {number}
*/
const getDaysOfThisMonth = function(date) {
return getDaysInMonth(date.getFullYear(), date.getMonth());
};
/**
* Get days of this month of the date
* @return {integer}
* Clear time part of the date
* @param {Date} date date
* @return {Date}
*/
if(!Date.prototype.getDaysInMonth) {
Date.prototype.getDaysInMonth = function() {
return Date.getDaysInMonth(this.getFullYear(), this.getMonth());
};
}
const clearTime = function(date) {
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
return date;
};
/**
* Add months to the date
* @param {date} value
*/
if(!Date.prototype.addMonths) {
Date.prototype.addMonths = function(value) {
var n = this.getDate();
this.setDate(1);
this.setMonth(this.getMonth() + value);
this.setDate(Math.min(n, this.getDaysInMonth()));
return this;
};
}
* @param {Date} date date
* @param {number} monthsCount
* @return {Date}
*/
const addMonths = function(date, monthsCount) {
var n = date.getDate();
date.setDate(1);
date.setMonth(date.getMonth() + monthsCount);
date.setDate(Math.min(n, getDaysOfThisMonth(date)));
return date;
};
/**
* Get last week day of the date
* @param {integer} day
* @return {date}
* @param {Date} date date
* @param {number} [day=1] 1 ~ 7
* @return {Date}
*/
if(!Date.prototype.getLastWeekday) {
Date.prototype.getLastWeekday = function(day) {
day = day || 1;
var d = this.clone();
while(d.getDay() != day) {
d.addDays(-1);
}
d.clearTime();
return d;
};
}
const getLastWeekday = function(date, day) {
day = day || 1;
var d = new Date(date.getTime());
while(d.getDay() != day) {
d = addDays(d, -1);
}
return clearTime(d);
};
/**
* Judge the date is same day as another date
* @param {date} date
* @return {Boolean}
* Judge the date is same day with another date
* @param {Date} date1
* @param {Date} date2
* @return {boolean}
*/
if(!Date.prototype.isSameDay) {
Date.prototype.isSameDay = function(date) {
return date.toDateString() === this.toDateString();
};
}
const isSameDay = function(date1, date2) {
return date1.toDateString() === date2.toDateString();
};
/**
* Judge the date is in same week as another date
* @param {date} date
* @return {Boolean}
* Judge the date is in same week with another date
* @param {Date} date1
* @param {Date} date2
* @return {boolean}
*/
if(!Date.prototype.isSameWeek) {
Date.prototype.isSameWeek = function(date) {
var weekStart = this.getLastWeekday();
var weekEnd = weekStart.clone().addDays(7);
return date >= weekStart && date < weekEnd;
};
}
const isSameWeek = function(date1, date2) {
var weekStart = getLastWeekday(date1);
var weekEnd = addDays(cloneDate(weekStart), 7);
return date2 >= weekStart && date2 < weekEnd;
};
/**
* Judge the date is in same year as another date
* @param {date} date
* @return {Boolean}
* Judge the date is in same year with another date
* @param {Date} date1
* @param {Date} date2
* @return {boolean}
*/
if(!Date.prototype.isSameYear) {
Date.prototype.isSameYear = function(date) {
return this.getFullYear() === date.getFullYear();
};
}
const isSameYear = function(date1, date2) {
return date1.getFullYear() === date2.getFullYear();
};
/**
* Create an date instance with string, timestamp or date instance
* @param {Date|String|Number} date
* @return {Date}
*/
if (!Date.create) {
Date.create = function(date) {
if (!(date instanceof Date)) {
if (typeof date === 'number' && date < 10000000000) {
date *= 1000;
}
date = new Date(date);
}
return date;
};
const exports = {
formatDate: formatDate,
createDate: createDate,
date: {
ONEDAY_TICKS: ONEDAY_TICKS,
create: createDate,
getTimestamp: getTimestamp,
format: formatDate,
addMilliseconds: addMilliseconds,
addDays: addDays,
cloneDate: cloneDate,
isLeapYear: isLeapYear,
getDaysInMonth: getDaysInMonth,
getDaysOfThisMonth: getDaysOfThisMonth,
clearTime: clearTime,
addMonths: addMonths,
getLastWeekday: getLastWeekday,
isSameDay: isSameDay,
isSameWeek: isSameWeek,
isSameYear: isSameYear,
}
};
if (window.$ && window.$.zui) {
$.zui(exports);
} else {
window.dateHelper = exports.date;
}
if (!Date.timestamp) {
Date.timestamp = function(date) {
if (typeof date === 'number') {
if (date < 10000000000) {
date *= 1000;
}
} else {
date = Date.create(date).getTime();
}
return date;
};
if (!window.noDatePrototypeHelper) {
/**
* Ticks of a whole day
* @type {number}
*/
Date.ONEDAY_TICKS = ONEDAY_TICKS;
/**
* Format date to a string
*
* @param string format
* @return string
*/
if(!Date.prototype.format) {
Date.prototype.format = function(format) {
return formatDate(this, format);
};
}
/**
* Add milliseconds to the date
* @param {number} value
*/
if(!Date.prototype.addMilliseconds) {
Date.prototype.addMilliseconds = function(value) {
return addMilliseconds(this, value);
};
}
/**
* Add days to the date
* @param {number} days
*/
if(!Date.prototype.addDays) {
Date.prototype.addDays = function(days) {
return addDays(this, days);
};
}
/**
* Clone a new date instane from the date
* @return {Date}
*/
if(!Date.prototype.clone) {
Date.prototype.clone = function() {
return cloneDate(this);
};
}
/**
* Judge the year is in a leap year
* @param {integer} year
* @return {Boolean}
*/
if(!Date.isLeapYear) {
Date.isLeapYear = function(year) {
return isLeapYear(year);
};
}
if(!Date.getDaysInMonth) {
/**
* Get days number of the date
* @param {integer} year
* @param {integer} month
* @return {integer}
*/
Date.getDaysInMonth = function(year, month) {
return getDaysInMonth(year, month);
};
}
/**
* Judge the date is in a leap year
* @return {Boolean}
*/
if(!Date.prototype.isLeapYear) {
Date.prototype.isLeapYear = function() {
return isLeapYear(this.getFullYear());
};
}
/**
* Clear time part of the date
* @return {date}
*/
if(!Date.prototype.clearTime) {
Date.prototype.clearTime = function() {
return clearTime(this);
};
}
/**
* Get days of this month of the date
* @return {integer}
*/
if(!Date.prototype.getDaysInMonth) {
Date.prototype.getDaysInMonth = function() {
return getDaysOfThisMonth(this);
};
}
/**
* Add months to the date
* @param {number} monthsCount
*/
if(!Date.prototype.addMonths) {
Date.prototype.addMonths = function(monthsCount) {
return addMonths(this, monthsCount);
};
}
/**
* Get last week day of the date
* @param {integer} day
* @return {date}
*/
if(!Date.prototype.getLastWeekday) {
Date.prototype.getLastWeekday = function(day) {
return getLastWeekday(this, day);
};
}
/**
* Judge the date is same day as another date
* @param {Date} date
* @return {Boolean}
*/
if(!Date.prototype.isSameDay) {
Date.prototype.isSameDay = function(date) {
return isSameDay(date, this);
};
}
/**
* Judge the date is in same week as another date
* @param {Date} date
* @return {Boolean}
*/
if(!Date.prototype.isSameWeek) {
Date.prototype.isSameWeek = function(date) {
return isSameWeek(date, this);
};
}
/**
* Judge the date is in same year as another date
* @param {Date} date
* @return {Boolean}
*/
if(!Date.prototype.isSameYear) {
Date.prototype.isSameYear = function(date) {
return isSameYear(this, date);
};
}
/**
* Create an date instance with string, timestamp or date instance
* @param {Date|String|Number} date
* @return {Date}
*/
if (!Date.create) {
Date.create = function(date) {
return createDate(date);
};
}
if (!Date.timestamp) {
Date.timestamp = function(date) {
return getTimestamp(date);
};
}
}
}());
}(window));
......@@ -6,81 +6,118 @@
* Copyright (c) 2014-2016 cnezsoft.com; Licensed MIT
* ======================================================================== */
(function() {
'use strict';
/**
* Format string with argument list or object
* @param {String} str
* @param {object | arguments} args
* @return {String}
*/
if(!String.prototype.format) {
String.prototype.format = function(args) {
var result = this;
if(arguments.length > 0) {
var reg;
if(arguments.length <= 2 && typeof(args) == 'object') {
for(var key in args) {
if(args[key] !== undefined) {
reg = new RegExp('(' + (arguments[1] ? arguments[1].replace('0', key) : '{' + key + '}') + ')', 'g');
result = result.replace(reg, args[key]);
}
const formatString = function(str, args) {
if(arguments.length > 1) {
var reg;
if(arguments.length == 2 && typeof(args) == "object") {
for(var key in args) {
if(args[key] !== undefined) {
reg = new RegExp("({" + key + "})", "g");
str = str.replace(reg, args[key]);
}
} else {
for(var i = 0; i < arguments.length; i++) {
if(arguments[i] !== undefined) {
reg = new RegExp('({[' + i + ']})', 'g');
result = result.replace(reg, arguments[i]);
}
}
} else {
for(var i = 1; i < arguments.length; i++) {
if(arguments[i] !== undefined) {
reg = new RegExp("({[" + (i - 1) + "]})", "g");
str = str.replace(reg, arguments[i]);
}
}
}
return result;
};
}
}
return str;
};
/**
* Judge the string is a integer number
*
* @param {String} str
* @access public
* @return bool
* @return {Boolean}
*/
if(!String.prototype.isNum) {
String.prototype.isNum = function(s) {
if(s !== null) {
var r, re;
re = /\d*/i;
r = s.match(re);
return(r == s) ? true : false;
}
return false;
};
}
const isNum = function(str) {
if(str !== null) {
var r, re;
re = /\d*/i;
r = str.match(re);
return(r == str) ? true : false;
}
return false;
};
if(!String.prototype.endsWith) {
String.prototype.endsWith = function(searchString, position) {
var subjectString = this.toString();
if(position === undefined || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
}
const exports = {
formatString: formatString,
string: {
format: formatString,
isNum: isNum,
}
};
if(!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
position = position || 0;
return this.lastIndexOf(searchString, position) === position;
};
if (window.$ && window.$.zui) {
$.zui(exports);
} else {
window.stringHelper = exports.string;
}
if(!String.prototype.includes) {
String.prototype.includes = function() {
return String.prototype.indexOf.apply(this, arguments) !== -1;
};
}
if (!window.noStringPrototypeHelper) {
/**
* Format string with argument list or object
* @param {object | arguments} args
* @return {String}
*/
if(!String.prototype.format) {
String.prototype.format = function() {
var args = [].slice.call(arguments);
args.unshift(this);
return formatString.apply(this, args);
};
}
/**
* Judge the string is a integer number
*
* @access public
* @return bool
*/
if(!String.prototype.isNum) {
String.prototype.isNum = function() {
return isNum(this);
};
}
// https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(search, this_len) {
if (this_len === undefined || this_len > this.length) {
this_len = this.length;
}
return this.substring(this_len - search.length, this_len) === search;
};
}
// https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
if (!String.prototype.startsWith) {
Object.defineProperty(String.prototype, 'startsWith', {
value: function(search, pos) {
pos = !pos || pos < 0 ? 0 : +pos;
return this.substring(pos, pos + search.length) === search;
}
});
}
if(!String.prototype.includes) {
String.prototype.includes = function() {
return String.prototype.indexOf.apply(this, arguments) !== -1;
};
}
}
})();
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册