123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- class DateUtil {
- static formatDate(Dates:Date=new Date()){
- //三目运算符
- // const Dates = new Date();
-
- //年份
- const Year : number = Dates.getFullYear();
-
- //月份下标是0-11
- const Months : any = ( Dates.getMonth() + 1 ) < 10 ? '0' + (Dates.getMonth() + 1) : ( Dates.getMonth() + 1);
-
- //具体的天数
- const Day : any = Dates.getDate() < 10 ? '0' + Dates.getDate() : Dates.getDate();
-
- //小时
- const Hours = Dates.getHours() < 10 ? '0' + Dates.getHours() : Dates.getHours();
-
- //分钟
- const Minutes = Dates.getMinutes() < 10 ? '0' + Dates.getMinutes() : Dates.getMinutes();
-
- //秒
- const Seconds = Dates.getSeconds() < 10 ? '0' + Dates.getSeconds() : Dates.getSeconds();
-
- //返回数据格式
- return Year + '-' + Months + '-' + Day + '-' + Hours + ':' + Minutes + ':' + Seconds;
- }
- static format_hh_mm_ss(Dates:Date=new Date()){
-
- //小时
- const Hours = Dates.getHours() < 10 ? '0' + Dates.getHours() : Dates.getHours();
-
- //分钟
- const Minutes = Dates.getMinutes() < 10 ? '0' + Dates.getMinutes() : Dates.getMinutes();
-
- //秒
- const Seconds = Dates.getSeconds() < 10 ? '0' + Dates.getSeconds() : Dates.getSeconds();
-
- //返回数据格式
- return Hours + ':' + Minutes + ':' + Seconds;
- }
- static formatSecond_00_00(allSecond: number, tag: string = ":") {
- let minutes = Math.floor(allSecond / 60);
- let second = allSecond - minutes * 60;
- let minuteStr = minutes.toString();
- if (minutes < 10) {
- minuteStr = "0" + minuteStr;
- }
- let secondStr = second.toString();
- if (second < 10) {
- secondStr = "0" + secondStr;
- }
- return minuteStr +tag+secondStr;
- }
- /**
- * 天数 :
- * date1在date2天前 ->返回-1,
- * date1在date2同一天 ->返回0,
- * date1在date2天后 ->返回1,
- * @param date1
- * @param date2
- */
- static DaySequence(date1: Date, date2: Date) {
- let y1 = date1.getFullYear();
- let y2 = date2.getFullYear();
- let y = this._DaySequence(y1, y2);
- if (y != 0) {
- return y;
- } else {
- let m1 = date1.getMonth();
- let m2 = date2.getMonth();
- let m = this._DaySequence(m1, m2);
- if (m != 0) {
- return m;
- } else {
- let d1 = date1.getDate();
- let d2 = date2.getDate();
- let d = this._DaySequence(d1, d2);
- return d;
- }
- }
- }
- private static _DaySequence(val1: number, val2: number) {
- if (val1 < val2) {
- return -1
- } else if (val1 < val2) {
- return 1
- } else {
- return 0;
- }
- }
- /**
- * 格式化时间
- * 注意:仅取整仅显示小时、仅显示分钟、仅显示秒
- * @param second 秒
- */
- public static secondRoundFormat(second: number): string {
- GLog.log("timeRoundFormat:", second);
- if (second >= 60) {
- if (second >= 2592000)//大于一月
- {
- //小时显示
- return Math.floor(second / 2592000) + "月";
- }
- else if (second >= 86400)//大于一天
- {
- //小时显示
- return Math.floor(second / 86400) + "天";
- }
- else if (second >= 3600)//大于一小时
- {
- //小时显示
- return Math.floor(second / 3600) + "小时";
- } else {
- //分钟显示
- return Math.floor(second / 60) + "分钟";
- }
- } else {
- //秒显示
- return Math.floor(second) + "秒";
- }
- }
- /**
- * 获取当前系统时间的秒数
- */
- public static getCurTime(): any {
- return Number((new Date().getTime() * 0.001).toFixed(0));
- }
- /**
- * 将秒数转换为 00时00分00秒 的形式
- * @param second 秒数
- * @returns {*} 格式化后的时分秒
- */
- public static secondConvertStringTime(second: number): string {
- let t;
- if (second > -1) {
- let hour = Math.floor(second / 3600);
- let min = Math.floor(second / 60) % 60;
- let sec = second % 60;
- if (hour < 10) {
- t = hour + "小时";
- } else {
- t = hour + "小时";
- }
- if (min < 10) { t += "0"; }
- t += min + "分";
- if (sec < 10) { t += "0"; }
- t += sec.toFixed(0) + "秒";
- }
- return t;
- }
- /**
- * 将秒数转换为 00:00:00 的形式
- * @param second 秒数
- * @returns {*} 格式化后的时分秒
- */
- public static secondConvertTime(second: number,part:number = 3): string {
- let t:string = `格式化时间,传入值错误:${second}`;
- if (second > -1) {
- let hour = Math.floor(second / 3600);
- let min = Math.floor(second / 60) % 60;
- let sec = second % 60;
- if (hour < 10) {
- t = "0" + hour + ":";
- } else {
- t = hour + ":";
- }
- if (min < 10) { t += "0"; }
- t += min + ":";
- if (sec < 10) { t += "0"; }
- t += sec.toFixed(0);
- }
- return t;
- }
- /**
- * 将服务端返回的时间转换为几时几分几秒(hh:mm:ss)
- * @param second 秒数
- */
- public static secondConverDateTime(second: number) {
- let date = new Date(second * 1000);
- let date_minus = ""
- let date_seconds = "";
- if (date.getMinutes() < 10) {
- date_minus = `0${date.getMinutes()}`;
- } else {
- date_minus = date.getMinutes().toString();
- }
- if (date.getSeconds() < 10) {
- date_seconds = `0${date.getSeconds()}`;
- } else {
- date_seconds = date.getSeconds().toString();
- }
- return `${date.getHours()}:${date_minus}:${date_seconds}`;
- }
- /**
- * 时分秒(00:00:00) 转为 时间戳
- * @param time 传入 00:00:00 的形式
- * @returns {string} 时间戳(单位:秒)
- */
- public static timeConvertUnixStamp(time: any): any {
- var s = '';
- var hour = time.split(':')[0];
- var min = time.split(':')[1];
- var sec = time.split(':')[2];
- s = Number(hour * 3600).toString() + Number(min * 60).toString() + Number(sec).toString();
- return s;
- }
- /**时间戳转为x月x日 */
- public static timestampTo_M_D(_timeStampSec: number): string {
- // date.getFullYear(); // 获取完整的年份(4位,1970)
- // date.getMonth(); // 获取月份(0-11,0代表1月,用的时候记得加上1)
- // date.getDate(); // 获取日(1-31)
- // date.getTime(); // 获取时间(从1970.1.1开始的毫秒数)
- // date.getHours(); // 获取小时数(0-23)
- // date.getMinutes(); // 获取分钟数(0-59)
- // date.getSeconds(); // 获取秒数(0-59)
- let date = new Date(_timeStampSec * 1000);
- return (date.getMonth() + 1).toString() + "月" + date.getDate() + "日";
- }
-
- /**时间戳转为年月日数组*/
- public static timestampTo_Y_M_D(timeStampSec: number): number[] {
- // date.getFullYear(); // 获取完整的年份(4位,1970)
- // date.getMonth(); // 获取月份(0-11,0代表1月,用的时候记得加上1)
- // date.getDate(); // 获取日(1-31)
- // date.getTime(); // 获取时间(从1970.1.1开始的毫秒数)
- // date.getHours(); // 获取小时数(0-23)
- // date.getMinutes(); // 获取分钟数(0-59)
- // date.getSeconds(); // 获取秒数(0-59)
- let date = new Date(timeStampSec * 1000);
- let arr:number[] = [];
- arr.push(date.getFullYear());
- arr.push(date.getMonth()+1);
- arr.push(date.getDate());
- return arr;// (date.getMonth() + 1).toString() + "月" + date.getDate() + "日";
- }
- /**
- * 秒转小时
- */
- public static timeSecToHours(sec: number): number {
- return sec / 3600;
- }
- }
- (<any>window).DateUtil = DateUtil;
|