// time 01~09
export function addZero(time: number) {
return time > 10 ? time.toString() : "0" + time;
}
// zone
export function Zone(zone: number) {
return zone > 0 ? "-" + Math.abs(zone) : "+" + Math.abs(zone);
}
// initial format 2019-08-08 12:50:09
export function timeFormat(date?: any, format?: any) {
const time = date;
const year = time.getFullYear();
const month = addZero(time.getMonth() + 1);
const day = addZero(time.getDate());
const hours = addZero(time.getHours());
const minute = addZero(time.getMinutes());
const second = addZero(time.getSeconds());
const milliSecond = addZero(time.getMilliseconds());
const zone = time.getTimezoneOffset() / 60;
const newZone = Zone(zone);
let dateWithFormat: any;
if (format === "YYYY") {
dateWithFormat = year + "";
} else if (format === "YYYY-MM") {
dateWithFormat = year + "-" + month;
} else if (format === "YYYY-MM-DD") {
dateWithFormat = year + "-" + month + "-" + day;
} else if (format === "YYYY-MM-DDThh:mmTZD") {
dateWithFormat = year + "-" + month + "-" + day + "T" + hours + ":" + minute + newZone + ":00";
} else if (format === "YYYY-MM-DDThh:mm:ssTZD") {
dateWithFormat = year + "-" + month + "-" + day + "T" + hours + ":" + minute + ":" + second + newZone + ":00";
} else if (format === "YYYY-MM-DDThh:mm:ss.sTZD") {
dateWithFormat = year + "-" + month + "-" + day + "T" + hours + ":" + minute + ":" + second + ":" + milliSecond
+ newZone + ":00";
}
return dateWithFormat;
}