2019-10-19 19:52:47 +08:00
|
|
|
// import moment from 'moment'
|
2019-10-24 18:04:31 +08:00
|
|
|
import isValid from 'date-fns/isValid'
|
|
|
|
import isSameDay from 'date-fns/isSameDay'
|
|
|
|
import getDate from 'date-fns/getDate'
|
|
|
|
import getMonth from 'date-fns/getMonth'
|
|
|
|
import getYear from 'date-fns/getYear'
|
|
|
|
import isSameMonth from 'date-fns/isSameMonth'
|
|
|
|
import getTime from 'date-fns/getTime'
|
|
|
|
import startOfMonth from 'date-fns/startOfMonth'
|
|
|
|
import addDays from 'date-fns/addDays'
|
|
|
|
import getDay from 'date-fns/getDay'
|
|
|
|
import parse from 'date-fns/parse'
|
|
|
|
import format from 'date-fns/format'
|
2019-07-04 16:47:41 +08:00
|
|
|
|
2019-10-19 19:52:47 +08:00
|
|
|
function matchDate (sourceTime, patternTime) {
|
|
|
|
if (Array.isArray(sourceTime)) {
|
|
|
|
// console.log(sourceTime, patternTime, sourceTime.some(time => isSameDay(time, patternTime)))
|
|
|
|
return sourceTime.some(time => isSameDay(time, patternTime))
|
|
|
|
// return time.some(t => t.year() === patternTime.year() && t.month() === patternTime.month() && t.date() === patternTime.date())
|
2019-07-25 19:53:31 +08:00
|
|
|
} else {
|
2019-10-19 19:52:47 +08:00
|
|
|
return isSameDay(sourceTime, patternTime)
|
|
|
|
// return time.year() === patternTime.year() && time.month() === patternTime.month() && time.date() === patternTime.date()
|
2019-07-25 19:53:31 +08:00
|
|
|
}
|
2019-07-04 16:47:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function dateItem (time, displayTime, selectedTime, currentTime) {
|
2019-07-25 19:53:31 +08:00
|
|
|
let isInSpan = false
|
|
|
|
if (Array.isArray(selectedTime)) {
|
2019-10-19 19:52:47 +08:00
|
|
|
if (selectedTime[0] < time && time < selectedTime[1]) {
|
2019-07-25 19:53:31 +08:00
|
|
|
isInSpan = true
|
|
|
|
}
|
|
|
|
}
|
2019-07-04 16:47:41 +08:00
|
|
|
return {
|
2019-10-19 19:52:47 +08:00
|
|
|
dateObject: {
|
|
|
|
date: getDate(time), // time.date(),
|
|
|
|
month: getMonth(time), // time.month(),
|
|
|
|
year: getYear(time) // time.year(),
|
|
|
|
},
|
|
|
|
isDateOfDisplayMonth: isSameMonth(time, displayTime), // time.month() === displayTime.month(),
|
2019-07-25 19:53:31 +08:00
|
|
|
isInSpan,
|
2019-10-19 19:52:47 +08:00
|
|
|
isSelectedDate: selectedTime !== null && matchDate(selectedTime, time),
|
|
|
|
isCurrentDate: matchDate(currentTime, time),
|
|
|
|
timestamp: getTime(time) // time.valueOf()
|
2019-07-04 16:47:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-21 23:13:05 +08:00
|
|
|
/**
|
|
|
|
* Given time to display calendar, given the selected time, given current time,
|
|
|
|
* return the date array of display time's month.
|
|
|
|
* @param {Moment} displayTime
|
|
|
|
* @param {Moment} selectedTime
|
|
|
|
* @param {Moment} currentTime
|
|
|
|
*/
|
2019-07-04 16:47:41 +08:00
|
|
|
function dateArray (displayTime, selectedTime, currentTime) {
|
2019-10-19 19:52:47 +08:00
|
|
|
const displayMonth = getMonth(displayTime) // displayTime.month()
|
2019-07-04 16:47:41 +08:00
|
|
|
/**
|
|
|
|
* First day of current month
|
|
|
|
*/
|
2019-10-19 19:52:47 +08:00
|
|
|
let displayMonthIterator = startOfMonth(displayTime) // moment(displayTime).startOf('month')
|
2019-07-04 16:47:41 +08:00
|
|
|
/**
|
|
|
|
* Last day of last month
|
|
|
|
*/
|
2019-10-19 19:52:47 +08:00
|
|
|
let lastMonthIterator = addDays(displayMonthIterator, -1) // moment(displayMonthIterator).subtract(1, 'day')
|
2019-07-04 16:47:41 +08:00
|
|
|
const calendarDays = []
|
|
|
|
let protectLastMonthDateIsShownFlag = true
|
2019-10-19 19:52:47 +08:00
|
|
|
while (getDay(lastMonthIterator) !== 6 || protectLastMonthDateIsShownFlag) {
|
2019-07-04 16:47:41 +08:00
|
|
|
calendarDays.unshift(dateItem(lastMonthIterator, displayTime, selectedTime, currentTime))
|
2019-10-19 19:52:47 +08:00
|
|
|
lastMonthIterator = addDays(lastMonthIterator, -1) // .subtract(1, 'day')
|
2019-07-04 16:47:41 +08:00
|
|
|
protectLastMonthDateIsShownFlag = false
|
|
|
|
}
|
2019-10-19 19:52:47 +08:00
|
|
|
while (getMonth(displayMonthIterator) === displayMonth) {
|
2019-07-04 16:47:41 +08:00
|
|
|
calendarDays.push(dateItem(displayMonthIterator, displayTime, selectedTime, currentTime))
|
2019-10-19 19:52:47 +08:00
|
|
|
displayMonthIterator = addDays(displayMonthIterator, 1) // .add(1, 'day')
|
2019-07-04 16:47:41 +08:00
|
|
|
}
|
2019-07-04 19:23:35 +08:00
|
|
|
while (calendarDays.length < 42) {
|
2019-07-04 16:47:41 +08:00
|
|
|
calendarDays.push(dateItem(displayMonthIterator, displayTime, selectedTime, currentTime))
|
2019-10-19 19:52:47 +08:00
|
|
|
displayMonthIterator = addDays(displayMonthIterator, 1)
|
|
|
|
// displayMonthIterator.add(1, 'day')
|
2019-07-04 16:47:41 +08:00
|
|
|
}
|
2019-10-19 19:52:47 +08:00
|
|
|
// console.log(calendarDays, selectedTime)
|
2019-07-04 16:47:41 +08:00
|
|
|
return calendarDays
|
|
|
|
}
|
|
|
|
|
2019-10-19 19:52:47 +08:00
|
|
|
function strictParse (string, pattern, backup) {
|
|
|
|
const result = parse(string, pattern, backup)
|
|
|
|
if (!isValid(result)) return result
|
|
|
|
else if (format(result, pattern) === string) return result
|
|
|
|
else return new Date(NaN)
|
|
|
|
}
|
|
|
|
|
|
|
|
export { dateArray, strictParse }
|