Blame view

node_modules/bootstrap-vue/src/utils/date.js 3.68 KB
4cd4fd28   郭伟龙   feat: 初始化项目
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Date utility functions
import { CALENDAR_GREGORY } from '../constants/date'
import { RX_DATE, RX_DATE_SPLIT } from '../constants/regex'
import { concat } from './array'
import { identity } from './identity'
import { isDate, isString } from './inspect'
import { toInteger } from './number'

// --- Date utility methods ---

// Create or clone a date (`new Date(...)` shortcut)
export const createDate = (...args) => new Date(...args)

// Parse a date sting, or Date object, into a Date object (with no time information)
export const parseYMD = date => {
  if (isString(date) && RX_DATE.test(date.trim())) {
    const [year, month, day] = date.split(RX_DATE_SPLIT).map(v => toInteger(v, 1))
    return createDate(year, month - 1, day)
  } else if (isDate(date)) {
    return createDate(date.getFullYear(), date.getMonth(), date.getDate())
  }
  return null
}

// Format a date object as `YYYY-MM-DD` format
export const formatYMD = date => {
  date = parseYMD(date)
  if (!date) {
    return null
  }
  const year = date.getFullYear()
  const month = `0${date.getMonth() + 1}`.slice(-2)
  const day = `0${date.getDate()}`.slice(-2)
  return `${year}-${month}-${day}`
}

// Given a locale (or locales), resolve the browser available locale
export const resolveLocale = (locales, calendar = CALENDAR_GREGORY) => /* istanbul ignore next */ {
  locales = concat(locales).filter(identity)
  const fmt = new Intl.DateTimeFormat(locales, { calendar })
  return fmt.resolvedOptions().locale
}

// Create a `Intl.DateTimeFormat` formatter function
export const createDateFormatter = (locale, options) => /* istanbul ignore next */ {
  const dtf = new Intl.DateTimeFormat(locale, options)
  return dtf.format
}

// Determine if two dates are the same date (ignoring time portion)
export const datesEqual = (date1, date2) => {
  // Returns true of the date portion of two date objects are equal
  // We don't compare the time portion
  return formatYMD(date1) === formatYMD(date2)
}

// --- Date "math" utility methods (for BCalendar component mainly) ---

export const firstDateOfMonth = date => {
  date = createDate(date)
  date.setDate(1)
  return date
}

export const lastDateOfMonth = date => {
  date = createDate(date)
  date.setMonth(date.getMonth() + 1)
  date.setDate(0)
  return date
}

export const addYears = (date, numberOfYears) => {
  date = createDate(date)
  const month = date.getMonth()
  date.setFullYear(date.getFullYear() + numberOfYears)
  // Handle Feb 29th for leap years
  if (date.getMonth() !== month) {
    date.setDate(0)
  }
  return date
}

export const oneMonthAgo = date => {
  date = createDate(date)
  const month = date.getMonth()
  date.setMonth(month - 1)
  // Handle when days in month are different
  if (date.getMonth() === month) {
    date.setDate(0)
  }
  return date
}

export const oneMonthAhead = date => {
  date = createDate(date)
  const month = date.getMonth()
  date.setMonth(month + 1)
  // Handle when days in month are different
  if (date.getMonth() === (month + 2) % 12) {
    date.setDate(0)
  }
  return date
}

export const oneYearAgo = date => {
  return addYears(date, -1)
}

export const oneYearAhead = date => {
  return addYears(date, 1)
}

export const oneDecadeAgo = date => {
  return addYears(date, -10)
}

export const oneDecadeAhead = date => {
  return addYears(date, 10)
}

// Helper function to constrain a date between two values
// Always returns a `Date` object or `null` if no date passed
export const constrainDate = (date, min = null, max = null) => {
  // Ensure values are `Date` objects (or `null`)
  date = parseYMD(date)
  min = parseYMD(min) || date
  max = parseYMD(max) || date
  // Return a new `Date` object (or `null`)
  return date ? (date < min ? min : date > max ? max : date) : null
}