You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
2.3 KiB
104 lines
2.3 KiB
interface DateTime {
|
|
year: number
|
|
month: number
|
|
day: number
|
|
hours: number
|
|
minutes: number
|
|
}
|
|
|
|
/**
|
|
* 获取一个月的天数
|
|
* @param year 年
|
|
* @param month 月
|
|
*/
|
|
const getDaysInOneMonth = function (year: number, month: number): number {
|
|
let _month = parseInt(String(month), 10);
|
|
let d = new Date(year, _month, 0);
|
|
return d.getDate();
|
|
};
|
|
|
|
|
|
/**
|
|
* 获取时间
|
|
* @param date 时间
|
|
*/
|
|
const dateDate = function (date: Date): DateTime {
|
|
let year = date.getFullYear();
|
|
let month = date.getMonth() + 1;
|
|
let day = date.getDate();
|
|
let hours = date.getHours();
|
|
let minutes = date.getMinutes();
|
|
return {
|
|
year, month, day, hours, minutes
|
|
}
|
|
};
|
|
|
|
interface DateTimePicker {
|
|
name: string
|
|
id: number | string
|
|
}
|
|
|
|
/**
|
|
* 时间选择器
|
|
* 获取date time 年份,月份,天数,小时,分钟推后30分
|
|
* @param startyear 开始年
|
|
* @param endyear 结束月
|
|
*/
|
|
const dateTimePicker = function (startyear: number, endyear: number) {
|
|
const years: DateTimePicker[] = [];
|
|
const months: DateTimePicker[] = [];
|
|
const hours: DateTimePicker[] = [];
|
|
const minutes: DateTimePicker[] = [];
|
|
for (let i = startyear - 20; i <= endyear + 20; i++) {
|
|
years.push({name: i + '年', id: i})
|
|
}
|
|
|
|
//获取月份
|
|
for (let i: string | number = 1; i <= 12; i = Number(i) + 1) {
|
|
if (i < 10) {
|
|
i = "0" + i;
|
|
}
|
|
months.push({name: i + '月', id: i});
|
|
}
|
|
|
|
//获取小时
|
|
for (let i: string | number = 0; i < 24; i = Number(i) + 1) {
|
|
if (i < 10) {
|
|
i = "0" + i
|
|
}
|
|
hours.push({name: i + '时', id: i})
|
|
}
|
|
//获取分钟
|
|
for (let i: string | number = 0; i < 60; i = Number(i) + 1) {
|
|
if (i < 10) {
|
|
i = "0" + i;
|
|
}
|
|
minutes.push({
|
|
name: i + '分',
|
|
id: i
|
|
});
|
|
}
|
|
return function (_year: number | string, _month: string | number) {
|
|
const days: DateTimePicker[] = [];
|
|
_year = parseInt(String(_year));
|
|
_month = parseInt(String(_month));
|
|
//获取日期
|
|
for (let i: string | number = 0; i <= getDaysInOneMonth(_year, _month); i = Number(i) + 1) {
|
|
if (i < 10) {
|
|
i = "0" + i;
|
|
}
|
|
days.push({
|
|
name: i + '日',
|
|
id: i
|
|
});
|
|
}
|
|
return [years, months, days, hours, minutes];
|
|
}
|
|
};
|
|
|
|
|
|
export {
|
|
dateTimePicker,
|
|
getDaysInOneMonth,
|
|
dateDate
|
|
}
|
|
|