@mekari/pixel-date-picker
Advanced tools
Comparing version 0.7.0 to 0.8.0
{ | ||
"name": "@mekari/pixel-date-picker", | ||
"version": "0.7.0", | ||
"version": "0.8.0", | ||
"description": "Mekari Pixel | Date picker component", | ||
@@ -5,0 +5,0 @@ "homepage": "https://mekari.design/", |
@@ -8,4 +8,8 @@ import { | ||
startOfMonth, | ||
startOfDay | ||
startOfDay, | ||
isDateBetween, | ||
getWeekDates | ||
} from '../util/date' | ||
import { getWeek } from 'date-format-parse' | ||
import { getLocale } from '../locale' | ||
import TableDate from './table-date' | ||
@@ -23,3 +27,9 @@ import TableMonth from './table-month' | ||
default: () => () => { } | ||
} | ||
}, | ||
getLocale: { | ||
default: () => getLocale | ||
}, | ||
getWeek: { | ||
default: () => getWeek | ||
}, | ||
}, | ||
@@ -129,3 +139,3 @@ props: { | ||
return this.events.map((item) => item.eventDate.toDateString()) | ||
} | ||
}, | ||
}, | ||
@@ -202,3 +212,9 @@ watch: { | ||
handleSelectDate(date) { | ||
this.emitDate(date, this.type === 'week' ? 'week' : 'date') | ||
const type = () => { | ||
if (this.type === 'week') return 'week' | ||
if (this.type === 'biweek') return 'biweek' | ||
return 'date' | ||
} | ||
this.emitDate(date, type()) | ||
}, | ||
@@ -254,2 +270,4 @@ getMonthCellDate(month) { | ||
endRange: false, | ||
isWeekDates: false, | ||
isBiweekDates: false, | ||
pinbar: false | ||
@@ -268,2 +286,6 @@ } | ||
} | ||
if (this.type === 'biweek' && state === 'active') { | ||
status.active = true | ||
} | ||
if (state === 'disabled') { | ||
@@ -283,2 +305,6 @@ status.disabled = true | ||
} | ||
if (this.type === 'biweek') { | ||
status.isBiweekDates = isDateBetween(cellDate, this.innerValue[0], this.innerValue[1]) | ||
} | ||
} | ||
@@ -291,2 +317,9 @@ | ||
if (this.type === 'week' && this.innerValue.length === 1) { | ||
const weekNumber = this.getWeek(this.innerValue[0], this.getLocale().formatLocale) | ||
const weekDates = getWeekDates(weekNumber, new Date(this.innerValue).getFullYear(), this.getLocale().formatLocale.firstDayOfWeek) | ||
status.isWeekDates = isDateBetween(cellDate, weekDates[0], weekDates[1]) | ||
} | ||
status = { | ||
@@ -293,0 +326,0 @@ ...status, |
import CalendarPanel from './calendar-panel' | ||
import { getValidDate, isValidDate, isValidRangeDate, startOfMonth } from '../util/date' | ||
import { getValidDate, isValidDate, isValidRangeDate, startOfMonth, startOfWeek, addDays } from '../util/date' | ||
import { getWeek } from 'date-format-parse' | ||
import { getLocale } from '../locale' | ||
import { MpSimpleGrid } from '@mekari/pixel-simple-grid' | ||
@@ -18,3 +20,12 @@ import { MpBox } from '@mekari/pixel-box' | ||
default: 'mx' | ||
} | ||
}, | ||
$DatePickerContext: { | ||
default: undefined | ||
}, | ||
getLocale: { | ||
default: () => getLocale | ||
}, | ||
getWeek: { | ||
default: () => getWeek | ||
}, | ||
}, | ||
@@ -32,2 +43,5 @@ props: { | ||
computed: { | ||
context() { | ||
return this.$DatePickerContext() | ||
}, | ||
// Minimum difference between start and end calendars | ||
@@ -62,3 +76,3 @@ calendarMinDiff() { | ||
} | ||
} | ||
}, | ||
}, | ||
@@ -68,2 +82,12 @@ methods: { | ||
const [startValue, endValue] = this.innerValue | ||
if (type === 'biweek') { | ||
const startDate = startOfWeek(date, this.getLocale().formatLocale.firstDayOfWeek) | ||
const endDate = addDays(startDate, 13) | ||
this.innerValue = [startDate, endDate] | ||
this.emitDate(this.innerValue, type) | ||
return | ||
} | ||
if (isValidDate(startValue) && !isValidDate(endValue)) { | ||
@@ -121,32 +145,7 @@ if (startValue.getTime() > date.getTime()) { | ||
}, | ||
// getRangeClasses(cellDate, currentDates, classnames) { | ||
// const classes = [].concat(this.getClasses(cellDate, currentDates, classnames)) | ||
// if (/disabled|active/.test(classnames)) return classes | ||
// const inRange = (data, range, fn = (v) => v.getTime()) => { | ||
// const value = fn(data) | ||
// let [min, max] = range.map(fn) | ||
// if (min > max) { | ||
// ;[min, max] = [max, min] | ||
// } | ||
// return value > min && value < max | ||
// } | ||
// if (currentDates.length === 2 && inRange(cellDate, currentDates)) { | ||
// return classes.concat('in-range') | ||
// } | ||
// if ( | ||
// currentDates.length === 1 && | ||
// this.hoveredValue && | ||
// inRange(cellDate, [currentDates[0], this.hoveredValue]) | ||
// ) { | ||
// return classes.concat('hover-in-range') | ||
// } | ||
// return classes | ||
// } | ||
// }, | ||
getRangeClasses(cellDate, currentDates) { | ||
const status = { | ||
inRange: false, | ||
hoverInRange: false | ||
hoverInRange: false, | ||
activeDragRange: false | ||
} | ||
@@ -173,2 +172,5 @@ | ||
} | ||
if (currentDates[0]) { | ||
status.activeDragRange = true | ||
} | ||
return status | ||
@@ -187,3 +189,3 @@ } | ||
isShortcut: false, | ||
isShowNotCurrentMonth: false | ||
isShowNotCurrentMonth: this.type === 'biweek' | ||
} | ||
@@ -202,4 +204,5 @@ const on = { | ||
return this.inline ? renderSingleNode() : renderDualNode() | ||
return this.inline || this.context.type === 'biweek' ? renderSingleNode() : renderDualNode() | ||
} | ||
} |
import { getWeek, format } from 'date-format-parse' | ||
import { chunk } from '../util/base' | ||
import { getCalendar, setMonth } from '../util/date' | ||
import { getCalendar, setMonth, startOfWeek, addDays, isDateBetween } from '../util/date' | ||
import { getLocale } from '../locale' | ||
@@ -25,3 +25,3 @@ import { MpButtonIcon } from '@mekari/pixel-button-icon' | ||
default: undefined | ||
} | ||
}, | ||
}, | ||
@@ -78,2 +78,7 @@ props: { | ||
}, | ||
data() { | ||
return { | ||
hoveredBiweekDates: [], | ||
} | ||
}, | ||
computed: { | ||
@@ -133,2 +138,8 @@ firstDayOfWeek() { | ||
handleMouseEnter(cell) { | ||
if (this.type === 'biweek') { | ||
const startDate = startOfWeek(cell, this.getLocale().formatLocale.firstDayOfWeek) | ||
const endDate = addDays(startDate, 13) | ||
this.hoveredBiweekDates = [startDate, endDate] | ||
} | ||
if (typeof this.onDateMouseEnter === 'function') { | ||
@@ -139,2 +150,6 @@ this.onDateMouseEnter(cell) | ||
handleMouseLeave(cell) { | ||
if (this.type === 'biweek') { | ||
this.hoveredBiweekDates = [] | ||
} | ||
if (typeof this.onDateMouseLeave === 'function') { | ||
@@ -176,25 +191,24 @@ this.onDateMouseLeave(cell) | ||
const dateStatus = this.getDateStatus(item) | ||
let activeStyle = dateStatus.active | ||
? { | ||
color: this.type !== 'week' ? 'white' : 'unset', | ||
backgroundColor: 'blue.400', | ||
_hover: { | ||
backgroundColor: 'blue.400' | ||
} | ||
} | ||
: {} | ||
let startRange = dateStatus.startRange | ||
? { | ||
borderTopRightRadius: 0, | ||
borderBottomRightRadius: 0 | ||
let activeStyle = {} | ||
if (dateStatus.active) { | ||
activeStyle = { | ||
color: this.type !== 'week' ? 'white' : 'unset', | ||
backgroundColor: 'blue.400', | ||
_hover: { | ||
backgroundColor: 'blue.400' | ||
} | ||
: {} | ||
let endRange = dateStatus.endRange | ||
? { | ||
borderTopLeftRadius: 0, | ||
borderBottomLeftRadius: 0 | ||
} | ||
: {} | ||
} | ||
} | ||
let startRange = {} | ||
if (dateStatus.startRange) { | ||
startRange = {} | ||
} | ||
let endRange = {} | ||
if (dateStatus.endRange) { | ||
endRange = {} | ||
} | ||
let disabledStyle = {} | ||
@@ -214,72 +228,72 @@ if (dateStatus.disabled) { | ||
let inRangeStyle = dateStatus.inRange | ||
? { | ||
backgroundColor: 'ice.50', | ||
_hover: { | ||
backgroundColor: 'none' | ||
}, | ||
borderRadius: 0 | ||
} | ||
: {} | ||
let inRangeStyle = {} | ||
if (dateStatus.inRange) { | ||
inRangeStyle = { | ||
backgroundColor: 'ice.50', | ||
_hover: { | ||
backgroundColor: 'none' | ||
}, | ||
borderRadius: 0, | ||
} | ||
} | ||
let hoverInRangeStyle = dateStatus.hoverInRange | ||
? { | ||
backgroundColor: 'ice.50', | ||
_hover: { | ||
backgroundColor: 'none' | ||
}, | ||
borderRadius: 0 | ||
} | ||
: {} | ||
let hoverInRangeStyle = {} | ||
if (dateStatus.hoverInRange) { | ||
hoverInRangeStyle = { | ||
backgroundColor: 'ice.50', | ||
_hover: { | ||
backgroundColor: 'none' | ||
}, | ||
borderRadius: 0 | ||
} | ||
} | ||
let notThisMonthStyle = !dateStatus.currentMonth | ||
? { | ||
visibility: this.isShowNotCurrentMonth ? 'visible' : 'hidden', | ||
color: 'gray.600', | ||
let notThisMonthStyle = {} | ||
if (!dateStatus.currentMonth) { | ||
notThisMonthStyle = { | ||
visibility: this.isShowNotCurrentMonth ? 'visible' : 'hidden', | ||
color: dateStatus.active ? 'white' : 'gray.600', | ||
_hover: { | ||
backgroundColor: 'none' | ||
}, | ||
cursor: 'default' | ||
} | ||
} | ||
let todayStyle = {} | ||
if (dateStatus.today && !dateStatus.active) { | ||
todayStyle = { | ||
backgroundColor: 'orange.50', | ||
...(!this.isNotClickable && { | ||
_hover: { | ||
backgroundColor: 'none' | ||
}, | ||
cursor: 'default' | ||
} | ||
: null | ||
let todayStyle = | ||
dateStatus.today && !dateStatus.active | ||
? { | ||
backgroundColor: 'orange.50', | ||
...(!this.isNotClickable && { | ||
_hover: { | ||
boxShadow: '0 0 0 1px #1C44D5', | ||
zIndex: 'docked', | ||
position: 'relative' | ||
} | ||
}) | ||
boxShadow: '0 0 0 1px #1C44D5', | ||
zIndex: 'docked', | ||
position: 'relative' | ||
} | ||
: {} | ||
}) | ||
} | ||
} | ||
let resetWeekStyle = | ||
this.type === 'week' | ||
? { | ||
backgroundColor: 'transparent', | ||
borderRadius: 0, | ||
_hover: { | ||
boxShadow: 'unset' | ||
} | ||
} | ||
: {} | ||
let activeWeekStyle = {} | ||
let hoverWeekStyle = {} | ||
if (this.type === 'week') { | ||
// Hover style | ||
const startStyle = { | ||
_groupHover: { | ||
backgroundColor: 'blue.400', | ||
backgroundColor: 'gray.50', | ||
borderLeftRadius: 'sm', | ||
color: 'white' | ||
color: 'dark' | ||
} | ||
} | ||
const baseStyle = { | ||
_groupHover: { | ||
backgroundColor: 'gray.50', | ||
color: 'dark' | ||
} | ||
} | ||
const endStyle = { | ||
_groupHover: { | ||
backgroundColor: 'blue.400', | ||
backgroundColor: 'gray.50', | ||
borderRightRadius: 'sm', | ||
color: 'white' | ||
color: 'dark' | ||
} | ||
@@ -290,6 +304,70 @@ } | ||
...(col === 0 && startStyle), | ||
...(col > 0 && col < 6 && baseStyle), | ||
...(col === 6 && endStyle) | ||
} | ||
// Active style | ||
activeWeekStyle = { | ||
backgroundColor: 'transparent', | ||
borderRadius: 0, | ||
_hover: { | ||
boxShadow: 'unset' | ||
}, | ||
...(dateStatus.isWeekDates && { | ||
color: 'white', | ||
backgroundColor: 'blue.400', | ||
...(col === 0 && { borderLeftRadius: 'sm' }), | ||
...(col === 6 && { borderRightRadius: 'sm' }), | ||
}) | ||
} | ||
} | ||
let activeBiweekStyle = {} | ||
let hoverBiweekStyle = {} | ||
if (this.type === 'biweek') { | ||
// Hover style | ||
if (this.hoveredBiweekDates.length === 2) { | ||
const isBiweekDates = isDateBetween(item, this.hoveredBiweekDates[0], this.hoveredBiweekDates[1]) | ||
const isStartBiweek = isBiweekDates && col === 0 | ||
const isEndBiweek = isBiweekDates && col === 6 | ||
const baseHoverStyle = { | ||
backgroundColor: 'gray.50', | ||
borderRadius: 0, | ||
cursor: 'pointer', | ||
color: 'dark', | ||
_hover: { | ||
backgroundColor: 'none' | ||
}, | ||
} | ||
const startHoverStyle = { | ||
borderLeftRadius: 'md', | ||
} | ||
const endHoverStyle = { | ||
borderRightRadius: 'md', | ||
} | ||
hoverBiweekStyle = { | ||
...(isBiweekDates && baseHoverStyle), | ||
...(isStartBiweek && startHoverStyle), | ||
...(isEndBiweek && endHoverStyle), | ||
} | ||
} | ||
// Active style | ||
if (dateStatus.inRange || dateStatus.startRange || dateStatus.endRange) { | ||
const isStartBiweek = dateStatus.isBiweekDates && col === 0 | ||
const isEndBiweek = dateStatus.isBiweekDates && col === 6 | ||
activeBiweekStyle = { | ||
backgroundColor: 'blue.400', | ||
color: 'white', | ||
borderTopRightRadius: isEndBiweek ? 'sm' : 0, | ||
borderBottomRightRadius: isEndBiweek ? 'sm' : 0, | ||
borderTopLeftRadius: isStartBiweek ? 'sm' : 0, | ||
borderBottomLeftRadius: isStartBiweek ? 'sm' : 0, | ||
} | ||
} | ||
} | ||
return { | ||
@@ -303,4 +381,4 @@ fontSize: 'md', | ||
_hover: { | ||
color: 'dark', | ||
boxShadow: '0 0 0 1px #1C44D5', | ||
color: dateStatus.activeDragRange ? 'white' : 'dark', | ||
backgroundColor: dateStatus.activeDragRange ? 'blue.400' : 'gray.50', | ||
zIndex: 'docked', | ||
@@ -320,16 +398,8 @@ position: 'relative' | ||
...todayStyle, | ||
...resetWeekStyle, | ||
...hoverWeekStyle | ||
...activeWeekStyle, | ||
...hoverWeekStyle, | ||
...activeBiweekStyle, | ||
...hoverBiweekStyle, | ||
} | ||
}, | ||
weekStyle() { | ||
if (this.type === 'week') { | ||
return { | ||
_hover: { | ||
backgroundColor: 'ice.50' | ||
} | ||
} | ||
} | ||
return {} | ||
}, | ||
renderPinBar(h) { | ||
@@ -372,3 +442,3 @@ return h( | ||
borderCollapse: 'separate', | ||
borderSpacing: 0, | ||
borderSpacing: '0 3px', | ||
boxSizing: 'border-box', | ||
@@ -456,3 +526,2 @@ textAlign: 'center', | ||
role: 'group', | ||
...this.weekStyle() | ||
}, | ||
@@ -471,3 +540,3 @@ class: this.getRowClasses(row) | ||
title: this.getCellTitle(item), | ||
...this.cellStyle(item, row, col) | ||
...this.cellStyle(item, row, col), | ||
}, | ||
@@ -490,24 +559,24 @@ on: { | ||
this.isShortcut && | ||
h( | ||
MpFlex, | ||
{ | ||
attrs: { | ||
'data-pixel-part': 'MpDatePicker-Shortcut', | ||
pt: '12px', | ||
'justify-content': 'center' | ||
} | ||
}, | ||
[ | ||
h( | ||
MpButton, | ||
{ | ||
attrs: { variant: 'ghost', isDisabled: this.isDisabledShortcut() }, | ||
on: { click: this.handleShortcut } | ||
}, | ||
'Today' | ||
) | ||
] | ||
) | ||
h( | ||
MpFlex, | ||
{ | ||
attrs: { | ||
'data-pixel-part': 'MpDatePicker-Shortcut', | ||
pt: '12px', | ||
'justify-content': 'center' | ||
} | ||
}, | ||
[ | ||
h( | ||
MpButton, | ||
{ | ||
attrs: { variant: 'ghost', isDisabled: this.isDisabledShortcut() }, | ||
on: { click: this.handleShortcut } | ||
}, | ||
'Today' | ||
) | ||
] | ||
) | ||
]) | ||
} | ||
} |
@@ -177,6 +177,3 @@ import { chunk } from '../util/base' | ||
if (monthStatus.startRange) { | ||
startRangeStyle = { | ||
borderTopRightRadius: 0, | ||
borderBottomRightRadius: 0 | ||
} | ||
startRangeStyle = {} | ||
} | ||
@@ -186,6 +183,3 @@ | ||
if (monthStatus.endRange) { | ||
endRangeStyle = { | ||
borderTopLeftRadius: 0, | ||
borderBottomLeftRadius: 0 | ||
} | ||
endRangeStyle = {} | ||
} | ||
@@ -200,3 +194,4 @@ | ||
_hover: { | ||
boxShadow: '0 0 0 1px #1C44D5', | ||
color: monthStatus.activeDragRange ? 'white' : 'dark', | ||
backgroundColor: monthStatus.activeDragRange ? 'blue.400' : 'gray.50', | ||
zIndex: 'docked', | ||
@@ -224,3 +219,3 @@ position: 'relative' | ||
borderCollapse: 'separate', | ||
borderSpacing: 0, | ||
borderSpacing: '0 3px', | ||
boxSizing: 'border-box', | ||
@@ -227,0 +222,0 @@ textAlign: 'center', |
@@ -184,6 +184,3 @@ // import IconButton from './icon-button' | ||
if (yearStatus.startRange) { | ||
startRangeStyle = { | ||
borderTopRightRadius: 0, | ||
borderBottomRightRadius: 0 | ||
} | ||
startRangeStyle = {} | ||
} | ||
@@ -193,6 +190,3 @@ | ||
if (yearStatus.endRange) { | ||
endRangeStyle = { | ||
borderTopLeftRadius: 0, | ||
borderBottomLeftRadius: 0 | ||
} | ||
endRangeStyle = {} | ||
} | ||
@@ -207,3 +201,4 @@ | ||
_hover: { | ||
boxShadow: '0 0 0 1px #1C44D5', | ||
color: yearStatus.activeDragRange ? 'white' : 'dark', | ||
backgroundColor: yearStatus.activeDragRange ? 'blue.400' : 'gray.50', | ||
zIndex: 'docked', | ||
@@ -231,3 +226,3 @@ position: 'relative' | ||
borderCollapse: 'separate', | ||
borderSpacing: 0, | ||
borderSpacing: '0 3px', | ||
boxSizing: 'border-box', | ||
@@ -234,0 +229,0 @@ textAlign: 'center', |
@@ -62,3 +62,3 @@ import { useId, createStyledAttrsMixin, isUndef, getElementById } from '@mekari/pixel-utils' | ||
type: { | ||
type: String, // ['date', 'datetime', 'time', 'year', 'month', 'week'] | ||
type: String, // ['date', 'datetime', 'time', 'year', 'month', 'week', 'biweek] | ||
default: 'date' | ||
@@ -159,3 +159,4 @@ }, | ||
isPopoverOpen: this.isPopoverOpen, | ||
range: this.range | ||
range: this.range, | ||
type: this.type | ||
} | ||
@@ -195,3 +196,4 @@ }, | ||
time: 'HH:mm:ss', | ||
week: 'w' | ||
week: 'w', | ||
biweek: 'DD-MM-YYYY' | ||
} | ||
@@ -206,3 +208,3 @@ return this.format || map[this.type] || map.date | ||
} | ||
if (this.range) { | ||
if (this.isUseRange) { | ||
value = Array.isArray(value) ? value.slice(0, 2) : [null, null] | ||
@@ -238,3 +240,3 @@ return value.map(this.value2date) | ||
const types = ['date', 'month', 'year'] | ||
return this.multiple && !this.range && types.indexOf(this.type) !== -1 | ||
return this.multiple && !this.isUseRange && types.indexOf(this.type) !== -1 | ||
}, | ||
@@ -249,2 +251,9 @@ filteredCurrentEvents() { | ||
return dates | ||
}, | ||
isUseRange() { | ||
if (this.range || this.type === 'biweek') { | ||
return true | ||
} | ||
return false | ||
} | ||
@@ -366,3 +375,3 @@ }, | ||
} | ||
if (this.range) { | ||
if (this.isUseRange) { | ||
return isValidRangeDate(value) | ||
@@ -411,3 +420,3 @@ } | ||
clear() { | ||
this.emitValue(this.range ? [null, null] : null) | ||
this.emitValue(this.isUseRange ? [null, null] : null) | ||
this.$emit('clear') | ||
@@ -472,3 +481,3 @@ }, | ||
date = text.split(this.innerRangeSeparator).map((v) => this.parseDate(v.trim())) | ||
} else if (this.range) { | ||
} else if (this.isUseRange) { | ||
let arr = text.split(this.innerRangeSeparator) | ||
@@ -608,3 +617,3 @@ if (arr.length !== 2) { | ||
renderContent(h) { | ||
const map = this.range ? componentRangeMap : componentMap | ||
const map = this.isUseRange ? componentRangeMap : componentMap | ||
const Component = map[this.type] || map.default | ||
@@ -621,3 +630,3 @@ const props = { | ||
...(this.inline && { 'update:calendar': this.handeChangeCalendarPanel }), | ||
...(this.range && { | ||
...(this.isUseRange && { | ||
drag: (e) => { | ||
@@ -686,4 +695,4 @@ this.$emit('drag', e) | ||
attrs: { | ||
maxWidth: this.range ? '592px' : 66, | ||
width: this.range ? '592px' : 66, | ||
maxWidth: this.isUseRange && this.type !== 'biweek' ? '592px' : 66, | ||
width: this.isUseRange && this.type !== 'biweek' ? '592px' : 66, | ||
bg: 'white', | ||
@@ -690,0 +699,0 @@ zIndex: 'popover', |
@@ -241,3 +241,4 @@ import ScrollbarVertical from '../scrollbar/scrollbar-vertical' | ||
_hover: { | ||
borderColor: 'blue.400' | ||
borderColor: 'gray.50', | ||
backgroundColor: 'gray.50' | ||
}, | ||
@@ -244,0 +245,0 @@ ...(activeStatus === 'active' && activeStyle), |
// new Date(10, 0, 1) The year from 0 to 99 will be incremented by 1900 automatically. | ||
export function createDate(y, M = 0, d = 1, h = 0, m = 0, s = 0, ms = 0) { | ||
const date = new Date(y, M, d, h, m, s, ms); | ||
const date = new Date(y, M, d, h, m, s, ms) | ||
if (y < 100 && y >= 0) { | ||
date.setFullYear(y); | ||
date.setFullYear(y) | ||
} | ||
return date; | ||
return date | ||
} | ||
export function isValidDate(date) { | ||
return date instanceof Date && !isNaN(date); | ||
return date instanceof Date && !isNaN(date) | ||
} | ||
export function isValidRangeDate(date) { | ||
return Array.isArray(date) && date.length === 2 && date.every(isValidDate) && date[0] <= date[1]; | ||
return Array.isArray(date) && date.length === 2 && date.every(isValidDate) && date[0] <= date[1] | ||
} | ||
export function isValidDates(dates) { | ||
return Array.isArray(dates) && dates.every(isValidDate); | ||
return Array.isArray(dates) && dates.every(isValidDate) | ||
} | ||
export function getValidDate(value, ...backup) { | ||
const date = new Date(value); | ||
const date = new Date(value) | ||
if (isValidDate(date)) { | ||
return date; | ||
return date | ||
} | ||
if (backup.length) { | ||
return getValidDate(...backup); | ||
return getValidDate(...backup) | ||
} | ||
return new Date(); | ||
return new Date() | ||
} | ||
export function startOfYear(value) { | ||
const date = new Date(value); | ||
date.setMonth(0, 1); | ||
date.setHours(0, 0, 0, 0); | ||
return date; | ||
const date = new Date(value) | ||
date.setMonth(0, 1) | ||
date.setHours(0, 0, 0, 0) | ||
return date | ||
} | ||
export function startOfMonth(value) { | ||
const date = new Date(value); | ||
date.setDate(1); | ||
date.setHours(0, 0, 0, 0); | ||
return date; | ||
const date = new Date(value) | ||
date.setDate(1) | ||
date.setHours(0, 0, 0, 0) | ||
return date | ||
} | ||
export function startOfWeek(value, firstDayOfWeek = 0) { | ||
if (!(firstDayOfWeek >= 0 && firstDayOfWeek <= 6)) { | ||
throw new RangeError('weekStartsOn must be between 0 and 6') | ||
} | ||
const date = new Date(value) | ||
const day = date.getDay() | ||
const diff = (day + 7 - firstDayOfWeek) % 7 | ||
date.setDate(date.getDate() - diff) | ||
date.setHours(0, 0, 0, 0) | ||
return date | ||
} | ||
export function startOfDay(value) { | ||
const date = new Date(value); | ||
date.setHours(0, 0, 0, 0); | ||
return date; | ||
const date = new Date(value) | ||
date.setHours(0, 0, 0, 0) | ||
return date | ||
} | ||
export function getCalendar({ firstDayOfWeek, year, month }) { | ||
const arr = []; | ||
const arr = [] | ||
// change to the last day of the last month | ||
const calendar = createDate(year, month, 0); | ||
const lastDayInLastMonth = calendar.getDate(); | ||
const calendar = createDate(year, month, 0) | ||
const lastDayInLastMonth = calendar.getDate() | ||
// getDay() 0 is Sunday, 1 is Monday | ||
const firstDayInLastMonth = lastDayInLastMonth - ((calendar.getDay() + 7 - firstDayOfWeek) % 7); | ||
const firstDayInLastMonth = lastDayInLastMonth - ((calendar.getDay() + 7 - firstDayOfWeek) % 7) | ||
for (let i = firstDayInLastMonth; i <= lastDayInLastMonth; i++) { | ||
arr.push(createDate(year, month, i - lastDayInLastMonth)); | ||
arr.push(createDate(year, month, i - lastDayInLastMonth)) | ||
} | ||
// change to the last day of the current month | ||
calendar.setMonth(month + 1, 0); | ||
const lastDayInCurrentMonth = calendar.getDate(); | ||
calendar.setMonth(month + 1, 0) | ||
const lastDayInCurrentMonth = calendar.getDate() | ||
for (let i = 1; i <= lastDayInCurrentMonth; i++) { | ||
arr.push(createDate(year, month, i)); | ||
arr.push(createDate(year, month, i)) | ||
} | ||
const lastMonthLength = lastDayInLastMonth - firstDayInLastMonth + 1; | ||
const nextMonthLength = 6 * 7 - lastMonthLength - lastDayInCurrentMonth; | ||
const lastMonthLength = lastDayInLastMonth - firstDayInLastMonth + 1 | ||
const nextMonthLength = 6 * 7 - lastMonthLength - lastDayInCurrentMonth | ||
for (let i = 1; i <= nextMonthLength; i++) { | ||
arr.push(createDate(year, month, lastDayInCurrentMonth + i)); | ||
arr.push(createDate(year, month, lastDayInCurrentMonth + i)) | ||
} | ||
return arr; | ||
return arr | ||
} | ||
export function setMonth(dirtyDate, dirtyMonth) { | ||
const date = new Date(dirtyDate); | ||
const month = typeof dirtyMonth === 'function' ? dirtyMonth(date.getMonth()) : Number(dirtyMonth); | ||
const year = date.getFullYear(); | ||
const daysInMonth = createDate(year, month + 1, 0).getDate(); | ||
const day = date.getDate(); | ||
date.setMonth(month, Math.min(day, daysInMonth)); | ||
return date; | ||
const date = new Date(dirtyDate) | ||
const month = typeof dirtyMonth === 'function' ? dirtyMonth(date.getMonth()) : Number(dirtyMonth) | ||
const year = date.getFullYear() | ||
const daysInMonth = createDate(year, month + 1, 0).getDate() | ||
const day = date.getDate() | ||
date.setMonth(month, Math.min(day, daysInMonth)) | ||
return date | ||
} | ||
export function setYear(dirtyDate, dirtyYear) { | ||
const date = new Date(dirtyDate); | ||
const year = typeof dirtyYear === 'function' ? dirtyYear(date.getFullYear()) : dirtyYear; | ||
date.setFullYear(year); | ||
return date; | ||
const date = new Date(dirtyDate) | ||
const year = typeof dirtyYear === 'function' ? dirtyYear(date.getFullYear()) : dirtyYear | ||
date.setFullYear(year) | ||
return date | ||
} | ||
export function assignTime(target, source) { | ||
const date = new Date(target); | ||
const time = new Date(source); | ||
date.setHours(time.getHours(), time.getMinutes(), time.getSeconds()); | ||
return date; | ||
const date = new Date(target) | ||
const time = new Date(source) | ||
date.setHours(time.getHours(), time.getMinutes(), time.getSeconds()) | ||
return date | ||
} | ||
export function addDays(value, days) { | ||
var date = new Date(value) | ||
date.setDate(date.getDate() + days) | ||
return date | ||
} | ||
export function isDateBetween(date, min, max) { | ||
const _date = new Date(date) | ||
const _min = new Date(min) | ||
const _max = new Date(max) | ||
return _date.getTime() >= _min.getTime() && _date.getTime() <= _max.getTime() | ||
} | ||
export function getWeekDates(weekNumber, year, firstDayOfWeek = 0) { | ||
if (!(firstDayOfWeek >= 0 && firstDayOfWeek <= 6)) { | ||
throw new RangeError('weekStartsOn must be between 0 and 6') | ||
} | ||
const firstDayOfYear = new Date(year, 0, 1).getDay() | ||
const daysUntilFirstWeekStart = (7 - firstDayOfYear + firstDayOfWeek) % 7 | ||
const firstWeekStartDate = new Date(year, 0, daysUntilFirstWeekStart + 1) | ||
const startDate = new Date( | ||
firstWeekStartDate.getTime() + (weekNumber - 2) * 7 * 24 * 60 * 60 * 1000 | ||
) | ||
const endDate = new Date(startDate) | ||
endDate.setDate(endDate.getDate() + 6) | ||
return [startDate, endDate] | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
431477
15773