| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- import { getDay, getYear as _getYear, getMonth as _getMonth, getDate as _getDate, endOfMonth, getHours, getMinutes, getSeconds, addYears, addMonths, addDays, setYear as _setYear, setMonth as _setMonth, setDate as _setDate, setHours, setMinutes, setSeconds, isAfter as _isAfter, isValid, getWeek as _getWeek, startOfWeek, format as formatDate, parse as parseDate, isDate, isMatch } from 'date-fns';
- import * as Locale from 'date-fns/locale';
- var dealLocal = function dealLocal(str) {
- return str.replace(/_/g, '');
- };
- var localeParse = function localeParse(format) {
- return format.replace(/Y/g, 'y').replace(/D/g, 'd').replace(/gggg/, 'yyyy').replace(/g/g, 'G').replace(/([Ww])o/g, 'wo');
- };
- var generateConfig = {
- // get
- getNow: function getNow() {
- return new Date();
- },
- getFixedDate: function getFixedDate(string) {
- return new Date(string);
- },
- getEndDate: function getEndDate(date) {
- return endOfMonth(date);
- },
- getWeekDay: function getWeekDay(date) {
- return getDay(date);
- },
- getYear: function getYear(date) {
- return _getYear(date);
- },
- getMonth: function getMonth(date) {
- return _getMonth(date);
- },
- getDate: function getDate(date) {
- return _getDate(date);
- },
- getHour: function getHour(date) {
- return getHours(date);
- },
- getMinute: function getMinute(date) {
- return getMinutes(date);
- },
- getSecond: function getSecond(date) {
- return getSeconds(date);
- },
- // set
- addYear: function addYear(date, diff) {
- return addYears(date, diff);
- },
- addMonth: function addMonth(date, diff) {
- return addMonths(date, diff);
- },
- addDate: function addDate(date, diff) {
- return addDays(date, diff);
- },
- setYear: function setYear(date, year) {
- return _setYear(date, year);
- },
- setMonth: function setMonth(date, month) {
- return _setMonth(date, month);
- },
- setDate: function setDate(date, num) {
- return _setDate(date, num);
- },
- setHour: function setHour(date, hour) {
- return setHours(date, hour);
- },
- setMinute: function setMinute(date, minute) {
- return setMinutes(date, minute);
- },
- setSecond: function setSecond(date, second) {
- return setSeconds(date, second);
- },
- // Compare
- isAfter: function isAfter(date1, date2) {
- return _isAfter(date1, date2);
- },
- isValidate: function isValidate(date) {
- return isValid(date);
- },
- locale: {
- getWeekFirstDay: function getWeekFirstDay(locale) {
- var clone = Locale[dealLocal(locale)];
- return clone.options.weekStartsOn;
- },
- getWeekFirstDate: function getWeekFirstDate(locale, date) {
- return startOfWeek(date, {
- locale: Locale[dealLocal(locale)]
- });
- },
- getWeek: function getWeek(locale, date) {
- return _getWeek(date, {
- locale: Locale[dealLocal(locale)]
- });
- },
- getShortWeekDays: function getShortWeekDays(locale) {
- var clone = Locale[dealLocal(locale)];
- return Array.from({
- length: 7
- }).map(function (_, i) {
- return clone.localize.day(i, {
- width: 'short'
- });
- });
- },
- getShortMonths: function getShortMonths(locale) {
- var clone = Locale[dealLocal(locale)];
- return Array.from({
- length: 12
- }).map(function (_, i) {
- return clone.localize.month(i, {
- width: 'abbreviated'
- });
- });
- },
- format: function format(locale, date, _format) {
- if (!isValid(date)) {
- return null;
- }
- return formatDate(date, localeParse(_format), {
- locale: Locale[dealLocal(locale)]
- });
- },
- parse: function parse(locale, text, formats) {
- for (var i = 0; i < formats.length; i += 1) {
- var format = localeParse(formats[i]);
- var formatText = text;
- var date = parseDate(formatText, format, new Date(), {
- locale: Locale[dealLocal(locale)]
- });
- if (isValid(date) && formatText.length === format.length && isMatch(formatText, format)) {
- return date;
- }
- }
- return null;
- }
- },
- toDate: function toDate(value, valueFormat) {
- if (Array.isArray(value)) {
- return value.map(function (val) {
- return typeof val === 'string' && val ? parseDate(val, valueFormat, new Date()) : val || null;
- });
- } else {
- return typeof value === 'string' && value ? parseDate(value, valueFormat, new Date()) : value || null;
- }
- },
- toString: function toString(value, valueFormat) {
- if (Array.isArray(value)) {
- return value.map(function (val) {
- return isDate(val) ? formatDate(val, valueFormat) : val;
- });
- } else {
- return isDate(value) ? formatDate(value, valueFormat) : value;
- }
- }
- };
- export default generateConfig;
|