iOS NSDate 相關(guān)知識(shí)整理

項(xiàng)目最近用到一些關(guān)于 NSDate 的相關(guān)知識(shí),特此記錄。需要用的朋友可以創(chuàng)建一個(gè)類目,把需要用到的方法復(fù)制過去就行。

目錄

1.是否為今天
2.是否為昨天
3.根據(jù)時(shí)間戳獲取時(shí)間
4.根據(jù)時(shí)間獲取時(shí)間戳
5.獲取當(dāng)前時(shí)間的時(shí)間戳
6.計(jì)算兩個(gè)日期間隔天數(shù)
7.獲取當(dāng)前是星期幾
8.獲取當(dāng)前是幾月幾日
9.計(jì)算時(shí)間差
10.字符串轉(zhuǎn)日期格式
11.將世界時(shí)間轉(zhuǎn)化為中國區(qū)時(shí)間

// 是否為今天
- (BOOL)isToday{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];
    NSDate *today = [cal dateFromComponents:components];
    
    components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:self];
    NSDate *otherDate = [cal dateFromComponents:components];
    
    if([today isEqualToDate:otherDate]){
        return YES;
    }
    return NO;
}
// 是否為昨天
- (BOOL)isYesterday{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];
    NSDate *today = [cal dateFromComponents:components];
    NSDate *yesterday = [today dateByAddingTimeInterval: -kDayTimeInterval];
    
    components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:self];
    NSDate *otherDate = [cal dateFromComponents:components];
    
    if([yesterday isEqualToDate:otherDate]){
        return YES;
    }
    return NO;
}
// 根據(jù)時(shí)間戳獲取時(shí)間
+ (NSString *)timeWithTimeStampString:(NSString *)timeStamp{
    // 格式化時(shí)間
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    // 毫秒值轉(zhuǎn)化為秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeStamp doubleValue]/ 1000.0];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}
// 根據(jù)時(shí)間獲取時(shí)間戳
+ (NSString *)timeStampWithDade:(NSDate *)date{
    NSTimeInterval a=[date timeIntervalSince1970]*1000; // *1000 是精確到毫秒,不乘就是精確到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", a]; //轉(zhuǎn)為字符型
    return timeString;
}
// 獲取當(dāng)前時(shí)間的時(shí)間戳
+ (NSInteger)getNowTimestamp{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; //
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    
    NSDate *datenow = [NSDate date];//現(xiàn)在時(shí)間
    
    //時(shí)間轉(zhuǎn)時(shí)間戳的方法:
    NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];
    
    return timeSp;
}
// 計(jì)算兩個(gè)日期間隔天數(shù)
+ (NSInteger)getDaysFrom:(NSString *)beginDate To:(NSString *)endDate{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    
    // 將字符串日期 轉(zhuǎn)換為 NSDate 類型,并去掉時(shí)分秒信息
    NSDate *fromDate = [NSDate worldTimeToChina:[formatter dateFromString:[beginDate substringToIndex:10]]];
    NSDate *toDate = [NSDate worldTimeToChina:[formatter dateFromString:[endDate substringToIndex:10]]];

    NSTimeInterval time = [toDate timeIntervalSinceDate:fromDate];
    NSInteger days = ((int)time) / (3600 * 24);

    return days;
}
// 獲取當(dāng)前是星期幾
+ (NSInteger)getNowWeekday {
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday |
    NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDate *now = [NSDate date];
    // 在真機(jī)上需要設(shè)置區(qū)域,才能正確獲取本地日期,中國區(qū)代碼:zh_CN
    calendar.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    comps = [calendar components:unitFlags fromDate:now];
    
    return [comps weekday];
}
// 獲取當(dāng)前是幾月幾日
+ (NSString *)getTodayStr{
    NSCalendar *cal = [NSCalendar currentCalendar];
    
    NSDateComponents *components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];
    NSDate *today = [cal dateFromComponents:components];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"MM月dd日";
    return [formatter stringFromDate:today];
}
// 計(jì)算時(shí)間差
+ (NSTimeInterval)timeInterValWithDate:(NSString *)featureDate{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 設(shè)置日期格式,可以根據(jù)自己的需求隨時(shí)調(diào)整,否則計(jì)算的結(jié)果為 nil
    formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    // 將字符串日期 轉(zhuǎn)換為 NSDate 類型
    NSDate *endDate = [formatter dateFromString:featureDate];
    
    return [endDate timeIntervalSinceDate:[NSDate new]];
}
// 字符串轉(zhuǎn)日期格式
+ (NSDate *)stringToDate:(NSString *)dateString withDateFormat:(NSString *)format{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    
    NSDate *date = [dateFormatter dateFromString:dateString];
    return date;
}
// 將世界時(shí)間轉(zhuǎn)化為中國區(qū)時(shí)間
+ (NSDate *)worldTimeToChina:(NSDate *)date{
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    NSInteger interval = [timeZone secondsFromGMTForDate:date];
    NSDate *localeDate = [date  dateByAddingTimeInterval:interval];
    return localeDate;
}

內(nèi)容整理自網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系刪除。

聯(lián)系作者:簡(jiǎn)書·DH_Fantasy 新浪微博·DH_Fantasy
版權(quán)聲明:自由轉(zhuǎn)載-非商用-非衍生-保持署名(CC BY-NC-ND 3.0

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

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

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