中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
获取格式化日期,将传入的日期格式化为指定格式的字符串
微信扫码分享
/** * 获取格式化日期,将传入的日期格式化为指定格式的字符串 * @param date * @param format 格式化字符串,(yyyy-MM-dd HH:mm:ss) * @returns */ static getFormatDateStr(date: number | string | Date, format?: string): string { date = DateUtil.getFormatDate(date) if (format === undefined || format == '') { format = "yyyy-MM-dd HH:mm:ss"; //yyyy-MM-dd HH:mm:ss.fff } const year = date.getFullYear(); const month = DateUtil.padZero(date.getMonth() + 1); const day = DateUtil.padZero(date.getDate()); const hours = DateUtil.padZero(date.getHours()); const minutes = DateUtil.padZero(date.getMinutes()); const seconds = DateUtil.padZero(date.getSeconds()); let formattedDate = format.replace("yyyy", year.toString()); formattedDate = formattedDate.replace("MM", month); formattedDate = formattedDate.replace("dd", day); formattedDate = formattedDate.replace("HH", hours); formattedDate = formattedDate.replace("mm", minutes); formattedDate = formattedDate.replace("ss", seconds); return formattedDate; }