vue 读取Excel文件中的日期,转为所需要的时间格式

const FileDate = (date: number, type: String) => {
	if (date) {
		let year, month, day, HH, mm, ss;
		const time = new Date((date - 1) * 24 * 3600000 + 1 - 8 * 3600000);

		time.setFullYear(time.getFullYear() - 70);
		console.log('time', time);
		let timeDate;
		year = time.getFullYear(); // 年
		month = time.getMonth() + 1; // 月
		day = time.getDate(); // 日
		HH = time.getHours(); // 时
		mm = time.getMinutes(); // 分
		ss = time.getSeconds(); // 秒

		month = month < 10 ? '0' + month : month;
		day = day < 10 ? '0' + day : day;
		HH = HH < 10 ? '0' + HH : HH; // 时
		mm = mm < 10 ? '0' + mm : mm; // 分
		ss = ss < 10 ? '0' + ss : ss; // 秒

		switch (type) {
			case 'yyyy':
				timeDate = String(year);
				break;
			case 'yyyy-MM':
				timeDate = year + '-' + month;
				break;
			case 'yyyy-MM-dd':
				timeDate = year + '-' + month + '-' + day;
				break;
			case 'yyyy/MM/dd':
				timeDate = year + '/' + month + '/' + day;
				break;
			case 'yyyy-MM-dd HH:mm:ss':
				timeDate = year + '-' + month + '-' + day + ' ' + HH + ':' + mm + ':' + ss;
				break;
			case 'yyyy-MM-dd_HH:mm:ss':
				timeDate = year + '-' + month + '-' + day + '_' + HH + ':' + mm + ':' + ss;
				break;
			case 'yyyy-MM-dd HH:mm':
				timeDate = year + '-' + month + '-' + day + ' ' + HH + ':' + mm;
				break;
			case 'HH:mm:ss':
				timeDate = HH + ':' + mm + ':' + ss;
				break;
			case 'MM':
				timeDate = String(month);
				break;
			case 'dd':
				timeDate = String(day);
				break;
			default:
				timeDate = year + '-' + month + '-' + day;
				break;
		}
		return timeDate;
	} else {
		return '';
	}
};
export default FileDate;

你可能感兴趣的:(vue.js,javascript,前端)