Skip to content

Commit

Permalink
优化日期函数,支持周视图
Browse files Browse the repository at this point in the history
  • Loading branch information
xuliangzhan committed Dec 11, 2021
1 parent f345e97 commit 0cf4464
Show file tree
Hide file tree
Showing 23 changed files with 835 additions and 125 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
.history
node_modules
/dist
package-lock.json
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
}
})();</script><link href=/xe-utils/static/css/app.e1992a6b.css rel=preload as=style><link href=/xe-utils/static/css/chunk-vendors.42851454.css rel=preload as=style><link href=/xe-utils/static/js/app.e597c307.js rel=preload as=script><link href=/xe-utils/static/js/chunk-vendors.8cc45737.js rel=preload as=script><link href=/xe-utils/static/css/chunk-vendors.42851454.css rel=stylesheet><link href=/xe-utils/static/css/app.e1992a6b.css rel=stylesheet></head><body><noscript><strong>We're sorry but xe-utils doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id=app></div><script src=https://cdn.jsdelivr.net/npm/xe-utils></script><script src=https://cdn.jsdelivr.net/npm/xe-ajax></script><script src=https://cdn.jsdelivr.net/npm/xe-clipboard></script><script src=/xe-utils/static/js/chunk-vendors.8cc45737.js></script><script src=/xe-utils/static/js/app.e597c307.js></script></body></html>
})();</script><link href=/xe-utils/static/css/app.10e8b0b2.css rel=preload as=style><link href=/xe-utils/static/css/chunk-vendors.42851454.css rel=preload as=style><link href=/xe-utils/static/js/app.9b08825e.js rel=preload as=script><link href=/xe-utils/static/js/chunk-vendors.8cc45737.js rel=preload as=script><link href=/xe-utils/static/css/chunk-vendors.42851454.css rel=stylesheet><link href=/xe-utils/static/css/app.10e8b0b2.css rel=stylesheet></head><body><noscript><strong>We're sorry but xe-utils doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id=app></div><script src=https://cdn.jsdelivr.net/npm/xe-utils></script><script src=https://cdn.jsdelivr.net/npm/xe-ajax></script><script src=https://cdn.jsdelivr.net/npm/xe-clipboard></script><script src=/xe-utils/static/js/chunk-vendors.8cc45737.js></script><script src=/xe-utils/static/js/app.9b08825e.js></script></body></html>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion func/commafy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var setupDefaults = require('./setupDefaults')

var round = require('./round')
var ceil = require('./ceil')
var floor = require('./floor')
Expand All @@ -7,6 +9,7 @@ var toValueString = require('./toValueString')
var toFixed = require('./toFixed')

var toNumberString = require('./toNumberString')
var assign = require('./assign')

/**
* 千分位分隔符、小数点
Expand All @@ -16,7 +19,7 @@ var toNumberString = require('./toNumberString')
* @return {String}
*/
function commafy(num, options) {
var opts = options || {}
var opts = assign({}, setupDefaults.commafyOptions, options)
var optDigits = opts.digits
var isNum = isNumber(num)
var rest, result, isNegative, intStr, floatStr
Expand Down
7 changes: 6 additions & 1 deletion func/getDateDiff.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export interface DateDiffResult {
S: number;
}

/**
* 例如:[['yyyy', 31536000000], ['MM', 2592000000], ['dd', 86400000], ['HH', 3600000], ['mm', 60000], ['ss', 1000], ['S', 0]]
*/
export type GetDateDiffRules = any[][]

/**
* 返回两个日期之间差距,如果结束日期小于开始日期 done 为 fasle
* @param startDate 开始日期
Expand All @@ -50,7 +55,7 @@ export declare function getDateDiff(startDate: string | Date | number, endDate:
* @param endDate 结束日期或当期日期
* @param rules 自定义计算规则
*/
export declare function getDateDiff(startDate: string | Date | number, endDate: string | Date | number, rules?: any[][]): DateDiffResult;
export declare function getDateDiff(startDate: string | Date | number, endDate: string | Date | number, rules?: GetDateDiffRules): DateDiffResult;

declare module './ctor' {
interface XEUtilsMethods {
Expand Down
5 changes: 4 additions & 1 deletion func/getMonthWeek.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { FirstDayOfWeek } from './getWhatWeek'

/**
* 返回某个月份的第几周
* @param date 字符串/日期/时间戳
* @param firstDay 周视图的起始天,默认星期一
*/
export declare function getMonthWeek(date: string | Date | number): number;
export declare function getMonthWeek(date: string | Date | number, firstDay?: FirstDayOfWeek): number;

declare module './ctor' {
interface XEUtilsMethods {
Expand Down
31 changes: 5 additions & 26 deletions func/getMonthWeek.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,14 @@
var staticWeekTime = require('./staticWeekTime')
var staticStrFirst = require('./staticStrFirst')

var helperGetYMDTime = require('./helperGetYMDTime')

var getWhatMonth = require('./getWhatMonth')
var toStringDate = require('./toStringDate')
var getWhatWeek = require('./getWhatWeek')

var isValidDate = require('./isValidDate')
var helperCreateGetDateWeek = require('./helperCreateGetDateWeek')

/**
* 返回某个月的第几周
*
* @param {Date} date 日期或数字
* @param {Number} firstDay 周视图的起始天,默认星期一
* @return {Number}
*/
function getMonthWeek (date) {
var monthFirst, monthFirstWeek
var currentDate = toStringDate(date)
if (isValidDate(currentDate)) {
monthFirst = getWhatMonth(currentDate, 0, staticStrFirst)
monthFirstWeek = getWhatWeek(monthFirst, 0, 1)
if (monthFirstWeek < monthFirst) {
monthFirstWeek = getWhatWeek(monthFirst, 1, 1)
}
if (currentDate >= monthFirstWeek) {
return Math.floor((helperGetYMDTime(currentDate) - helperGetYMDTime(monthFirstWeek)) / staticWeekTime) + 1
}
return getMonthWeek(getWhatWeek(currentDate, 0, 1))
}
return NaN
}
var getMonthWeek = helperCreateGetDateWeek(function (targetDate) {
return new Date(targetDate.getFullYear(), targetDate.getMonth(), 1)
})

module.exports = getMonthWeek
6 changes: 3 additions & 3 deletions func/getWhatMonth.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* 返回前几月或后几月的日期
* @param date 字符串/日期/时间戳
* @param offset 月偏移量(默认0)、前几个月、后几个月
* @param offsetMonth 月偏移量(默认0)、前几个月、后几个月
*/
export declare function getWhatMonth(date: string | Date | number, offset: number): Date;

/**
* 返回前几月或后几月的日期,可以指定月初(first)、月末(last)、天数,默认当前
* @param date 字符串/日期/时间戳
* @param offset 月偏移量(默认默认当前月)、前几个月、后几个月
* @param day 获取哪天:月初(first)、月末(last)、指定天数(数值)
* @param offsetMonth 月偏移量(默认默认当前月)、前几个月、后几个月
* @param offsetDay 获取哪天:月初(first)、月末(last)、指定天数(数值)
*/
export declare function getWhatMonth(date: string | Date | number, offset: number, day: number | 'first' | 'last'): Date;

Expand Down
24 changes: 12 additions & 12 deletions func/getWhatMonth.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ var isNumber = require('./isNumber')
* 返回前几月或后几月的日期
*
* @param {Date} date 日期或数字
* @param {Number} offset 月(默认当前月)、前几个月、后几个月
* @param {Number/String} day 获取哪天:月初(first)、月末(last)、指定天数(数值),如果为空,但超过指定月份的天数时,则默认单月最后一天
* @param {Number} offsetMonth 月(默认当前月)、前几个月、后几个月
* @param {Number/String} offsetDay 获取哪天:月初(first)、月末(last)、指定天数(数值),如果为空,但超过指定月份的天数时,则默认单月最后一天
* @return {Date}
*/
function getWhatMonth (date, offset, day) {
var monthOffset = offset && !isNaN(offset) ? offset : 0
function getWhatMonth (date, offsetMonth, offsetDay) {
var monthNum = offsetMonth && !isNaN(offsetMonth) ? offsetMonth : 0
date = toStringDate(date)
if (isValidDate(date)) {
if (day === staticStrFirst) {
return new Date(helperGetDateFullYear(date), helperGetDateMonth(date) + monthOffset, 1)
} else if (day === staticStrLast) {
return new Date(helperGetDateTime(getWhatMonth(date, monthOffset + 1, staticStrFirst)) - 1)
} else if (isNumber(day)) {
date.setDate(day)
if (offsetDay === staticStrFirst) {
return new Date(helperGetDateFullYear(date), helperGetDateMonth(date) + monthNum, 1)
} else if (offsetDay === staticStrLast) {
return new Date(helperGetDateTime(getWhatMonth(date, monthNum + 1, staticStrFirst)) - 1)
} else if (isNumber(offsetDay)) {
date.setDate(offsetDay)
}
if (monthOffset) {
if (monthNum) {
var currDate = date.getDate()
date.setMonth(helperGetDateMonth(date) + monthOffset)
date.setMonth(helperGetDateMonth(date) + monthNum)
if (currDate !== date.getDate()) {
// 当为指定天数,且被跨月了,则默认单月最后一天
date.setDate(1)
Expand Down
13 changes: 8 additions & 5 deletions func/getWhatWeek.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
export type FirstDayOfWeek = 0 | 1 | 2 | 3 | 4 |5 | 6

/**
* 返回前几周或后几周的日期
* @param date 字符串/日期/时间戳
* @param offset 周偏移量(默认当前周)、前几周、后几周
* @param offsetWeek 周偏移量(默认当前周)、前几周、后几周
*/
export declare function getWhatWeek(date: string | Date | number, offset: number): Date;
export declare function getWhatWeek(date: string | Date | number, offsetWeek?: FirstDayOfWeek): Date;

/**
* 返回前几周或后几周的日期,可以指定星期几(0~6),默认当前
* @param date 字符串/日期/时间戳
* @param offset 周偏移量(默认当前周)、前几周、后几周
* @param day 星期天(0)、星期一(1)、星期二(2)、星期三(3)、星期四(4)、星期五(5)、星期六(6)
* @param offsetWeek 获取周偏移量(默认0当前周、前几周、后几周)
* @param offsetDay 获取星期几(星期天0、星期一1、星期二2、星期三3、星期四4、星期五5、星期六6)
* @param firstDay 周视图的起始天,默认星期一
*/
export declare function getWhatWeek(date: string | Date | number, offset: number, day: number): Date;
export declare function getWhatWeek(date: string | Date | number, offsetWeek?: number, offsetDay?: FirstDayOfWeek, firstDay?: FirstDayOfWeek): Date;

declare module './ctor' {
interface XEUtilsMethods {
Expand Down
44 changes: 33 additions & 11 deletions func/getWhatWeek.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,53 @@
var setupDefaults = require('./setupDefaults')

var staticDayTime = require('./staticDayTime')
var staticWeekTime = require('./staticWeekTime')
var staticParseInt = require('./staticParseInt')

var helperGetDateTime = require('./helperGetDateTime')

var toStringDate = require('./toStringDate')

var isValidDate = require('./isValidDate')
var isNumber = require('./isNumber')

/**
* 返回前几周或后几周的星期几
*
* @param {Date} date 日期
* @param {Number} offset 周(默认当前周)、前几周、后几周
* @param {Number} day 星期天(默认0)、星期一(1)、星期二(2)、星期三(3)、星期四(4)、星期五(5)、星期六(6)
* @param {Number} offsetWeek 周(默认当前周)、前几周、后几周
* @param {Number} offsetDay 星期天(默认0)、星期一(1)、星期二(2)、星期三(3)、星期四(4)、星期五(5)、星期六(6)
* @param {Number} firstDay 周视图的起始天,默认星期一
* @return {Date}
*/
function getWhatWeek (date, offset, day) {
var time, whatDayTime, currentDay, customDay
function getWhatWeek (date, offsetWeek, offsetDay, firstDay) {
date = toStringDate(date)
if (isValidDate(date)) {
customDay = staticParseInt(/^[0-7]$/.test(day) ? day : date.getDay())
currentDay = date.getDay()
time = helperGetDateTime(date)
whatDayTime = time + ((customDay === 0 ? 7 : customDay) - (currentDay === 0 ? 7 : currentDay)) * staticDayTime
if (offset && !isNaN(offset)) {
whatDayTime += offset * staticWeekTime
var hasCustomDay = isNumber(offsetDay)
var hasStartDay = isNumber(firstDay)
var whatDayTime = helperGetDateTime(date)
// 如果指定了天或周视图起始天
if (hasCustomDay || hasStartDay) {
var viewStartDay = hasStartDay ? firstDay : setupDefaults.firstDayOfWeek
var currentDay = date.getDay()
var customDay = hasCustomDay ? offsetDay : currentDay
if (currentDay !== customDay) {
var offsetNum = 0
if (viewStartDay > currentDay) {
offsetNum = -(7 - viewStartDay + currentDay)
} else if (viewStartDay < currentDay) {
offsetNum = viewStartDay - currentDay
}
if (customDay > viewStartDay) {
whatDayTime += ((customDay === 0 ? 7 : customDay) - viewStartDay + offsetNum) * staticDayTime
} else if (customDay < viewStartDay) {
whatDayTime += (7 - viewStartDay + customDay + offsetNum) * staticDayTime
} else {
whatDayTime += offsetNum * staticDayTime
}
}
}
if (offsetWeek && !isNaN(offsetWeek)) {
whatDayTime += offsetWeek * staticWeekTime
}
return new Date(whatDayTime)
}
Expand Down
5 changes: 4 additions & 1 deletion func/getYearWeek.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { FirstDayOfWeek } from './getWhatWeek'

/**
* 返回某个年份的第几周
* @param date 字符串/日期/时间戳
* @param firstDay 从年初的星期几为起始开始周开始算,默认星期一
*/
export declare function getYearWeek(date: string | Date | number): number;
export declare function getYearWeek(date: string | Date | number, firstDay?: FirstDayOfWeek): number;

declare module './ctor' {
interface XEUtilsMethods {
Expand Down
20 changes: 5 additions & 15 deletions func/getYearWeek.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
var staticDayTime = require('./staticDayTime')

var toStringDate = require('./toStringDate')

var isValidDate = require('./isValidDate')
var helperCreateGetDateWeek = require('./helperCreateGetDateWeek')

/**
* 返回某个年份的第几周
*
* @param {Date} date 日期或数字
* @param {Number} firstDay 从年初的星期几为起始开始周开始算,默认星期一
* @return {Number}
*/
function getYearWeek (date) {
date = toStringDate(date)
if (isValidDate(date)) {
date.setHours(0, 0, 0, 0)
date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7)
var week = new Date(date.getFullYear(), 0, 4)
return Math.round(((date.getTime() - week.getTime()) / staticDayTime + (week.getDay() + 6) % 7 - 3) / 7) + 1
}
return NaN
}
var getYearWeek = helperCreateGetDateWeek(function (targetDate) {
return new Date(targetDate.getFullYear(), 0, 1)
})

module.exports = getYearWeek
31 changes: 31 additions & 0 deletions func/helperCreateGetDateWeek.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var setupDefaults = require('./setupDefaults')

var staticWeekTime = require('./staticWeekTime')

var isNumber = require('./isNumber')
var isValidDate = require('./isValidDate')
var getWhatWeek = require('./getWhatWeek')

var helperGetDateTime = require('./helperGetDateTime')

function createGetDateMonthWeek (getStartDate) {
return function (date, firstDay) {
var viewStartDay = isNumber(firstDay) ? firstDay : setupDefaults.firstDayOfWeek
var targetDate = getWhatWeek(date, 0, viewStartDay, viewStartDay)
if (isValidDate(targetDate)) {
var targetOffsetDate = new Date(targetDate.getFullYear(), targetDate.getMonth(), targetDate.getDate())
var targerStartDate = getStartDate(targetDate)
var targetFirstDay = targerStartDate.getDay()
if (targetFirstDay > viewStartDay) {
targerStartDate.setDate(7 - targetFirstDay + viewStartDay + 1)
}
if (targetFirstDay < viewStartDay) {
targerStartDate.setDate(viewStartDay - targetFirstDay + 1)
}
return Math.floor((helperGetDateTime(targetOffsetDate) - helperGetDateTime(targerStartDate)) / staticWeekTime + 1)
}
return NaN
}
}

module.exports = createGetDateMonthWeek
Loading

0 comments on commit 0cf4464

Please sign in to comment.