iOS中NSDate常用轉換操作整合


//當前時間格式化, 例:YYYY-MM-dd-EEEE-HH:mm:ss
+ (NSString *)getCurrentDataWithDateFormate:(NSString *)formate
{
    NSDate *now = [NSDate date];
    return [self dateFormattingWithDate:now toFormate:formate];
}

//任意NSDate格式化
+ (NSString *)dateFormattingWithDate:(NSDate *)date toFormate:(NSString *)formate
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:formate];
    return [formatter stringFromDate:date];
}

//獲取當天0點時間
+ (NSDate *)returnToDay0Clock
{
    NSDate *now = [NSDate date];
    NSCalendar *calender = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *dateComponent = [calender components:unitFlags fromDate:now];
    int hour = (int)[dateComponent hour];
    int minute = (int)[dateComponent minute];
    int second = (int)[dateComponent second];
    //當前時分秒:hour,minute,second
    //返回當前時間(hour * 3600 + minute * 60 + second)之前的時間,即為今天凌晨0點
    NSDate *nowDay = [NSDate dateWithTimeIntervalSinceNow: - (hour * 3600 + minute * 60 + second)];
    long long inter = [nowDay timeIntervalSince1970] * 1000;
    NSDate *newDate = [NSDate dateWithTimeIntervalSince1970:inter / 1000];
    return newDate;
}

//獲取當天24點時間
+ (NSDate *)returnToDay24Clock
{
    NSDate *now = [NSDate date];
    NSCalendar *calender = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *dateComponent = [calender components:unitFlags fromDate:now];
    int hour = (int)[dateComponent hour];
    int minute = (int)[dateComponent minute];
    int second = (int)[dateComponent second];
    //一天是60分鍾 * 60秒 * 24小時 = 86400秒
    NSDate *nextDay = [NSDate dateWithTimeIntervalSinceNow: - (hour * 3600 + minute * 60 + second) + 86400];
    return nextDay;
}

//獲取當前秒數
+ (long long)getCurrentDateSecond
{
    return [[NSDate date] timeIntervalSince1970];
}

//NSDate轉秒
+ (long long)dateTosecond:(NSDate *)date
{
    return [date timeIntervalSince1970];
}

//秒轉NSDate
+ (NSDate *)secondToDate:(long long)second
{
    return [NSDate dateWithTimeIntervalSince1970:second];
}

//是否是12小時制; YES:12小時制 / NO:24小時制
+ (BOOL)is12HourSystem
{
    NSString *formatStringForHour = [NSDateFormatter dateFormatFromTemplate:@"j" options:0 locale:[NSLocale autoupdatingCurrentLocale]];
    NSRange contains = [formatStringForHour rangeOfString:@"a"];
    BOOL is12HourSystem = contains.location != NSNotFound;
    return is12HourSystem;
}

//朋友圈/聊天 時間顯示樣式
+ (NSString *)dateDisplayResult:(long long)secondCount
{
    NSDate *date = [self secondToDate:secondCount];
    NSCalendar *calender = [NSCalendar currentCalendar];
    //判斷是否是今天
    if ([calender isDateInToday:date]) {

        long long dateSecondCount = [[NSDate date] timeIntervalSinceDate:date];
        if (dateSecondCount < 60) {
            return @"剛剛";
        }
        if (dateSecondCount < (60 * 60)) {
            return [NSString stringWithFormat:@"%d分鍾前",(int)(dateSecondCount / 60)];
        }
        return [NSString stringWithFormat:@"%d小時前",(int)(dateSecondCount / (60 * 60))];
    }

    //判斷是否是昨天
    NSString *formatterString = @" HH:mm";
    if ([calender isDateInYesterday:date]) {
        formatterString = [@"昨天" stringByAppendingString:formatterString];
    } else {
        //判斷是否是一年內
        formatterString = [@"MM-dd" stringByAppendingString:formatterString];
        //判斷是否值一年之前
        NSDateComponents *component = [calender components:NSCalendarUnitYear fromDate:date toDate:[NSDate date] options:NSCalendarWrapComponents];

        if (component.year >= 1) {
            formatterString = [@"YYYY-" stringByAppendingString:formatterString];
        }
    }
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:formatterString];
    formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en"];
    return [formatter stringFromDate:date];
}

//比較兩個NsDate對象的時間差
+ (CompareResult *)compareDateDifferenceDate1:(NSDate *)date1 date2:(NSDate *)date2
{
    CompareResult *result = [[CompareResult alloc] init];
    result.value = (fabs([date1 timeIntervalSinceDate:date2]));
    result.trend = [date1 compare:date2];
    return result;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM