需求背景:
現(xiàn)在多數(shù) APP
在 home
頁面會(huì)有一些日常問候語汉柒,比如:早上好!/ 上午好! / 晚上好责鳍!/ 六一兒童節(jié)快樂碾褂,等等之類的。一般類似于這種需求历葛,后臺(tái)都會(huì)返回一個(gè)數(shù)組正塌,每個(gè)數(shù)組里包含一個(gè)對(duì)象,每個(gè)對(duì)象又有起始時(shí)間(可能就是一個(gè)時(shí)間的字符串,如:01:00:00)與結(jié)束時(shí)間,以及一個(gè)日期來告訴客戶端是否是節(jié)日衰琐。
思路:
針對(duì)上面這種需求,偽代碼如下:
- 判斷是否是節(jié)日问裕?如果是節(jié)日就顯示節(jié)日問候語并直接 return,如果不是就繼續(xù)下一步顯示日常問候語孵坚;
- 日常問候語粮宛,一般分三個(gè)時(shí)間段:
- 05:00:00 -- 11:59:59 早上
- 12:00:00 -- 16:59:59 下午
- 17:00:00 -- 04:59:59 晚上
- 觀察時(shí)間范圍,可獲取當(dāng)前時(shí)間
currentTime
卖宠,找出currentTime
在時(shí)間范圍的那個(gè)對(duì)象巍杈,然后獲取該對(duì)象的問候語】肝椋可是發(fā)現(xiàn)晚上這個(gè)時(shí)間筷畦,結(jié)束時(shí)間是小于起始時(shí)間,所以還要加一個(gè)條件:currentTime
大于晚上的起始時(shí)間刺洒,而且起始時(shí)間大于結(jié)束時(shí)間鳖宾。 - 思路大概就是這樣,現(xiàn)在通過代碼來演示:
代碼:
-
test()
一個(gè)測試方法逆航,調(diào)用封裝的方法(可以寫在工具類中)
test() {
// compare two date
let currentDate = new Date().toISOString();
let startdate = "2021-05-04T00:00:00";
let enddate = "2021-05-04T23:59:59";
let compareStartDate= this. compDateTime(startdate,currentDate,'s');
let compareEndDate = this. compDateTime(currentDate,enddate,'s');
console.log('compareStartDate:'+compareStartDate + '--compareEndDate:'+compareEndDate);
//值為正鼎文,說明第二個(gè)時(shí)間大于第一個(gè)時(shí)間,compareStartDate:57195--compareEndDate:403, currentDate為2021-05-04T23:50:50
// compare two time
let starttime = "05:00:00";
let endtime = "11:59:59";
let currentHours = this.formatDateTime(new Date(),'hours');
let compStarttime = this.compareHoursTime(starttime, currentHours);
let compEndtime = this.compareHoursTime(currentHours, endtime);
console.log('compStarttime:'+compStarttime + '--compEndtime:'+compEndtime);
//compStarttime:false--compEndtime:true因俐,currentDate為2021-05-04T23:50:50
}
常用方法封裝:
- 常用日期格式化方式:
formatDateTime(date: any, pattern: string, lang?: string): string | null {
let isEN = (lang || moment.locale()) === 'en';
if (pattern === 'iso8601') {
pattern = 'YYYY-MM-DDTHH:mm:ssZ';
} else if (pattern === 'shortDate') {
pattern = 'DD/MM/YYYY';
} else if (pattern === 'longDate') {
pattern = isEN ? 'DD MMM YYYY' : 'DD/MM/YYYY';
} else if (pattern == 'wholeDate') {
pattern = isEN ? 'DD MMMM YYYY' : 'YYYY年MM月DD日';
} else if (pattern == 'datetime') {
pattern = isEN ? 'DD MMM YYYY HH:mm:ss z' : 'z YYYY年MM月DD日 HH:mm:ss';
} else if (pattern == 'datetimeWithoutSecond') {
pattern = isEN ? 'DD MMM YYYY HH:mm z' : 'z YYYY年MM月DD日 HH:mm';
} else if (pattern == 'simpleDatetime') {
pattern = 'DD MMM YYYY HH:mm z';
} else if (pattern == 'time') {
pattern = 'HH:mm';
} else if (pattern == "date") {
pattern = isEN ? 'DD MMM YYYY' : 'YYYY年MM月DD日';
} else if (pattern == "yearMonth") {
pattern = isEN ? 'MMM YYYY' : 'YYYY年MM月';
} else if (pattern == "hours") {
pattern = 'HH:mm:ss';
}
if(lang) {
return moment.tz(date, 'Asia/Shen_Zhen').locale(lang).format(pattern).replace('SZT', '深圳時(shí)間');
} else {
return moment.tz(date, 'Asia/Shen_Zhen').format(pattern).replace('SZT', '深圳時(shí)間');
}
}
- 比較兩個(gè)日期大小的方法, 第二個(gè)日期大于第一個(gè)日期的話拇惋,就返回正值,否則返回負(fù)值:
compDateTime(fromDate: any = new Date(), toDate: any = new Date(), unit: string): number {
let timezone = new Date().getTimezoneOffset();
console.log('timezone:'+timezone);
const fromDateMoment = moment.tz(fromDate, 'Asia/Shen_Zhen');
const toDateMoment = moment.tz(toDate, 'Asia/Shen_Zhen');
return toDateMoment.diff(fromDateMoment, unit);
}
- 比較兩個(gè)時(shí)間大小方法抹剩,第一個(gè)時(shí)間大于第二個(gè)時(shí)間的話撑帖,就返回
true
,否則返回false
:
compareHoursTime(oneHours: any, anotherHours: any) {
if (oneHours && anotherHours){
let date = new Date();
let one = date.setHours(oneHours.split(':')[0],oneHours.split(':')[1],oneHours.split(':')[2]);
let another = date.setHours(anotherHours.split(':')[0],anotherHours.split(':')[1],anotherHours.split(':')[2]);
return one > another;
}
}
- 傳入一個(gè)年月日加上時(shí)間的日期轉(zhuǎn)化為澳眷,ISOString 類似的日期格式:
convertDateTime(date: Date, time: Date): Date {
return moment(moment(date).format('YYYYMMDD') + 'T' + moment(time).format('HHmmss')).toDate();
}
- 字符串格式日期轉(zhuǎn)化為
Date
格式:
parseDateTimeFromDateString(dateStr: string, pattern: string): Date {
if (pattern === 'iso8601') {
pattern = 'YYYY-MM-DDTHH:mm:ssZ';
} else if (pattern === 'shortDate') {
pattern = 'DD/MM/YYYY';
} else if (pattern === 'longDate') {
pattern = 'DD MMM YYYY';
} else if (pattern == 'datetime') {
pattern = 'DD MMM YYYY HH:mm:ss';
}
let currentLocale = moment.locale();
moment.locale('en');
let result = moment.tz(dateStr, pattern, TIMEZONE).toDate()
moment.locale(currentLocale);
return result;
}
- 判斷是否是今天:
isToday(date: Date): boolean {
const dtFormat = 'shortDate';
let now = this.formatDateTime(new Date(), dtFormat);
let compare = this.formatDateTime(date, dtFormat);
return now.localeCompare(compare) === 0;
}
- 日期數(shù)組中胡嘿,找出最小日期的元素:
minDate(list: Date[]): Date | null {
if(list.length === 0) return null;
return list.reduce((minDate, date) => date.getTime() < minDate.getTime() ? date : minDate, list[0]);
}
- 日期數(shù)組中,找出最大日期的元素:
maxDate(list: Date[]): Date | null {
if(list.length === 0) return null;
return list.reduce((maxDate, date) => date.getTime() > maxDate.getTime() ? date : maxDate, list[0]);
}
結(jié)語
以上就是Date
對(duì)象的簡單總結(jié)境蔼,如果講述有什么錯(cuò)誤灶平,歡迎留言評(píng)論,互相交流學(xué)習(xí)箍土,碼子碼圖不易逢享,轉(zhuǎn)載請(qǐng)注明出處,謝謝吴藻。