2016-12-12 20:34:28 +08:00
|
|
|
import dateUtil from '../../utils/date';
|
|
|
|
|
|
|
|
export const toDate = function(date) {
|
2017-08-31 16:45:22 +02:00
|
|
|
let _date = new Date(date);
|
|
|
|
// IE patch start (#1422)
|
|
|
|
if (isNaN(_date.getTime()) && typeof date === 'string'){
|
|
|
|
_date = date.split('-').map(Number);
|
|
|
|
_date[1] += 1;
|
|
|
|
_date = new Date(..._date);
|
|
|
|
}
|
|
|
|
// IE patch end
|
|
|
|
|
|
|
|
if (isNaN(_date.getTime())) return null;
|
|
|
|
return _date;
|
2016-12-12 20:34:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export const formatDate = function(date, format) {
|
|
|
|
date = toDate(date);
|
|
|
|
if (!date) return '';
|
|
|
|
return dateUtil.format(date, format || 'yyyy-MM-dd');
|
|
|
|
};
|
|
|
|
|
|
|
|
export const parseDate = function(string, format) {
|
|
|
|
return dateUtil.parse(string, format || 'yyyy-MM-dd');
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getDayCountOfMonth = function(year, month) {
|
2017-08-24 08:21:50 +02:00
|
|
|
return new Date(year, month + 1, 0).getDate();
|
2016-12-12 20:34:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export const getFirstDayOfMonth = function(date) {
|
|
|
|
const temp = new Date(date.getTime());
|
|
|
|
temp.setDate(1);
|
|
|
|
return temp.getDay();
|
|
|
|
};
|
|
|
|
|
2017-08-24 08:21:50 +02:00
|
|
|
export const siblingMonth = function(src, diff) {
|
|
|
|
const temp = new Date(src); // lets copy it so we don't change the original
|
|
|
|
const newMonth = temp.getMonth() + diff;
|
|
|
|
const newMonthDayCount = getDayCountOfMonth(temp.getFullYear(), newMonth);
|
|
|
|
if (newMonthDayCount < temp.getDate()) {
|
|
|
|
temp.setDate(newMonthDayCount);
|
2016-12-12 20:34:28 +08:00
|
|
|
}
|
2017-08-24 08:21:50 +02:00
|
|
|
temp.setMonth(newMonth);
|
2016-12-12 20:34:28 +08:00
|
|
|
|
2017-08-24 08:21:50 +02:00
|
|
|
return temp;
|
|
|
|
};
|
2016-12-12 20:34:28 +08:00
|
|
|
|
2017-08-24 08:21:50 +02:00
|
|
|
export const prevMonth = function(src) {
|
|
|
|
return siblingMonth(src, -1);
|
2016-12-12 20:34:28 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export const nextMonth = function(src) {
|
2017-08-24 08:21:50 +02:00
|
|
|
return siblingMonth(src, 1);
|
2016-12-27 17:16:11 +08:00
|
|
|
};
|
|
|
|
|
2017-08-24 08:21:50 +02:00
|
|
|
export const initTimeDate = function() {
|
2016-12-27 17:16:11 +08:00
|
|
|
const date = new Date();
|
|
|
|
date.setHours(0);
|
|
|
|
date.setMinutes(0);
|
|
|
|
date.setSeconds(0);
|
|
|
|
return date;
|
2017-08-24 08:21:50 +02:00
|
|
|
};
|