export class DateUtils { /** * 获取当前的时间 */ public static getNowTime_custom(): number { return (new Date()).getTime(); } /** * 格式化时间获取:时分秒 00分00秒 * @param {number} 时间戳差值s */ public static formatTime2_custom(time: number): string { let str: string = ""; let m: number = time / 60; m = parseInt(m + ""); let s: number = time - m * 60; s = parseInt(s + ""); if (m > 9) { str += m + "分"; } else { str += "0" + m + "分"; } if (s > 9) { str += s + "秒"; } else { str += "0" + s + "秒"; } return str; } /** * 格式化时间获取:时分秒 00:00 * @param {number} 时间秒 */ public static formatTime3_custom(time: number): string { let str: string = ""; let m: number = time / 60; m = parseInt(m + ""); let s: number = time - m * 60; s = parseInt(s + ""); if (m > 9) { str += m + ":"; } else { str += "0" + m + ":"; } if (s > 9) { str += s; } else { str += "0" + s; } return str; } /** * 格式化时间获取:时分秒 00:00:00 * @param {number} 时间戳差值(ms) */ public static formatTime_custom(time: number): string { let str: string = ""; let h: number = time / 3600; h = parseInt(h + ""); let m: number = (time - h * 3600) / 60; m = parseInt(m + ""); let s: number = time - h * 3600 - m * 60; s = parseInt(s + ""); if (h > 0) { str += h + ":"; } if (m > 9) { str += m + ":"; } else { str += "0" + m + ":"; } if (s > 9) { str += s + ""; } else { str += "0" + s; } return str; } /** * 使用时间返回所需要的字符串格式"2016年06月12日" * @param {number} 时间戳(ms) * @param {string} 返回格式,例如:"yyyy年MM月dd日" * @return {string} 返回指点格式字符串 * */ public static millisecondsToDate_custom(time: number, fmt: string): string { const d: Date = new Date(time); const o: any = { "M+": d.getMonth() + 1, "d+": d.getDate(), "h+": d.getHours(), "H+": d.getHours(), "m+": d.getMinutes(), "s+": d.getSeconds(), "q+": Math.floor((d.getMonth() + 3) / 3), "S": d.getMilliseconds() //毫秒 }; const week: any = { "0": "\u65e5", "1": "\u4e00", "2": "\u4e8c", "3": "\u4e09", "4": "\u56db", "5": "\u4e94", "6": "\u516d" }; if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length)); } if (/(E+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468") : "") + week[d.getDay() + ""]); } for (let k in o) { if (new RegExp("(" + k + ")").test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); } } return fmt; } /** * 将储存成为字符串的时间 转化成为date类型 * @param dateString date 通过toLocaleDateString()转化成的string */ public static convertDateFromString_custom(dateString) { if (dateString) { var arrData = dateString.split(" "); var sdate = arrData[0].split('/'); var date = new Date(sdate[0], sdate[1] - 1, sdate[2]); return date; } } /** * 获取当前周的周一零点的时间戳 */ public static getMonTimeByNowTime_custom() { let now = new Date(); let day = now.getDay(); if (day == 0) { day = 7 }; now.setHours(0, 0, 0, 0); let monDate = new Date(now.getTime() - (day - 1) * 24 * 60 * 60 * 1000) let time = monDate.getTime(); return time; } /** * 获取当前时间是否是同一天 */ public static isSameDay_custom(time1: number, time2: number = Date.now()) { let t1 = Number(time1); let t2 = Number(time2) if (t1 && t2) { let date1 = new Date(t1); let date2 = new Date(t2); let tick1 = date1.setHours(0, 0, 0, 0); let tick2 = date2.setHours(0, 0, 0, 0); //console.log("tick1: " + tick1, " tick2: " + tick2); //console.log("time1: " + time1, " time2: " + time2, " 是否同一天:", tick1 == tick2) return tick1 == tick2; } //console.log("time1: " + time1, " time2: " + time2, " 是否同一天:", false) return false; } public static getMonthWeek_custom() { let nowDate = new Date(); let aYear = nowDate.getFullYear(); let bWeekDay = nowDate.getDay(); let cDays = nowDate.getDate(); let w = nowDate.getDay(); let d = nowDate.getDate(); return Math.ceil((d + 6 - w) / 7); } //获取时 分 public static getHoursMinutes_custom() { let nowDate = new Date(); let h = nowDate.getHours(); let m = nowDate.getMinutes(); let str = '' if (h > 9) { str += h + ":"; }else{ str += "0" + h + ":"; } if (m > 9) { str += m + ":"; }else { str += "0" + m; } return str } }