早上好,中午好,下午好,晚上好

/**

*? 將時間點轉(zhuǎn)化成日歷形式

*/

- (NSDate *)getCustomDateWithHour:(NSInteger)hour {

//獲取當(dāng)前時間

NSDate * destinationDateNow = [NSDate date];

NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

NSDateComponents *currentComps = [[NSDateComponents alloc] init];

NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

currentComps = [currentCalendar components:unitFlags fromDate:destinationDateNow];

//設(shè)置當(dāng)前的時間點

NSDateComponents *resultComps = [[NSDateComponents alloc] init];

[resultComps setYear:[currentComps year]];

[resultComps setMonth:[currentComps month]];

[resultComps setDay:[currentComps day]];

[resultComps setHour:hour];

NSCalendar *resultCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

return [resultCalendar dateFromComponents:resultComps];

}



/**

*? 獲取時間段

*/

- (NSString *)getTheTimeBucket {

//? ? NSDate * currentDate = [self getNowDateFromatAnDate:[NSDate date]];

NSDate * currentDate = [NSDate date];

if ([currentDate compare:[self getCustomDateWithHour:0]] == NSOrderedDescending && [currentDate compare:[self getCustomDateWithHour:9]] == NSOrderedAscending) {

return @"早上好";

} else if ([currentDate compare:[self getCustomDateWithHour:9]] == NSOrderedDescending && [currentDate compare:[self getCustomDateWithHour:11]] == NSOrderedAscending) {

return @"上午好";

} else if ([currentDate compare:[self getCustomDateWithHour:11]] == NSOrderedDescending && [currentDate compare:[self getCustomDateWithHour:13]] == NSOrderedAscending) {

return @"中午好";

} else if ([currentDate compare:[self getCustomDateWithHour:13]] == NSOrderedDescending && [currentDate compare:[self getCustomDateWithHour:18]] == NSOrderedAscending) {

return @"下午好";

} else {

return @"晚上好";

}

}



/**

*? [self getTheTimeBucket] 這樣就得到時間段了 擴展 通過[NSDate date]獲得的時間可能與當(dāng)前的系統(tǒng)時間不一樣,這是因為時區(qū)時差的緣故

*? 考慮時區(qū),獲取準(zhǔn)備的系統(tǒng)時間方法

*/

- (NSDate *)getNowDateFromatAnDate:(NSDate *)anyDate {

//設(shè)置源日期時區(qū)

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT

//設(shè)置轉(zhuǎn)換后的目標(biāo)日期時區(qū)

NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];

//得到源日期與世界標(biāo)準(zhǔn)時間的偏移量

NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];

//目標(biāo)日期與本地時區(qū)的偏移量

NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];

//得到時間偏移量的差值

NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

//轉(zhuǎn)為現(xiàn)在時間

NSDate* destinationDateNow = [[NSDate alloc] initWithTimeInterval:interval sinceDate:anyDate];

return destinationDateNow;

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容