iOS開發(fā)之時(shí)間戳、NSString、NSDate三者的相互轉(zhuǎn)化

嗨,時(shí)間好快,馬上又要春季了...
話不多說 . . .
話說工作之余 . . . 其實(shí)是項(xiàng)目經(jīng)常用到的一個(gè)整合吧 . . .
決定把這個(gè)整合分享給大家 . . .
勿噴 . . . 只是對(duì)自己項(xiàng)目常用的整合出來分享給大家 . . .
希望能夠快速幫助到大家 . . .
如若有更好的方法歡迎多多指教. . .

喜歡的朋友們可以點(diǎn)點(diǎn)贊打打賞加加好友常常交流共勉一起進(jìn)步咯

//
//  NSDate+Util.h
//  zxl
//
//  Created by zxl on 17/1/9.
//  Copyright ? 2016年 zxl. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSDate (Util)

///當(dāng)前時(shí)間時(shí)間戳
+(NSInteger)getNowTimeInterval;

///時(shí)間戳轉(zhuǎn)字符串
+(NSString *)timeIntervalToString:(NSInteger)timeInterval;

///字符串轉(zhuǎn)時(shí)間戳
+(NSInteger)stringToTimeInterval:(NSString *)string;

///字符串轉(zhuǎn)NSDate
+(NSDate *)stringToDate:(NSString *)string;

///NSDate轉(zhuǎn)字符串
+(NSString *)dateToString:(NSDate *)date;

///以前時(shí)間距離現(xiàn)在多久
+(NSString *)timeFromToNow:(NSInteger)formerTimeInterval;

///將來時(shí)間距離現(xiàn)在多久
+(NSString *)willTimeToNow:(NSInteger)willTimeInterval;

/// 今天、明天、后天的最晚時(shí)間戳
+(NSInteger)indexDayTimeInterval:(NSInteger)dayCount

@end
//
//  NSDate+Util.m
//  zxl
//
//  Created by zxl on 17/1/9.
//  Copyright ? 2016年 zxl. All rights reserved.
//

#import "NSDate+Util.h"

@implementation NSDate (Util)

#pragma mark 當(dāng)前時(shí)間時(shí)間戳
+(NSInteger)getNowTimeInterval
{
    NSDate *date = [NSDate date];
    NSInteger timeInterval = [date timeIntervalSince1970];
    return timeInterval;
}

#pragma mark 時(shí)間戳轉(zhuǎn)字符串
+(NSString *)timeIntervalToString:(NSInteger)timeInterval
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *string = [dateFormatter stringFromDate:date];
    return string;
}

#pragma mark 字符串轉(zhuǎn)時(shí)間戳
+(NSInteger)stringToTimeInterval:(NSString *)string
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:string];
    NSInteger timeInterval = [date timeIntervalSince1970];
    return timeInterval;
}

#pragma mark 字符串轉(zhuǎn)NSDate
+(NSDate *)stringToDate:(NSString *)string
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:string];
    return date;
}

#pragma mark NSDate轉(zhuǎn)字符串
+(NSString *)dateToString:(NSDate *)date
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *string = [dateFormatter stringFromDate:date];
    return string;
}

#pragma mark 以前時(shí)間距離現(xiàn)在多久
+(NSString *)timeFromToNow:(NSInteger)formerTimeInterval
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:formerTimeInterval];
    
    NSTimeInterval  timeInterval = [date timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    if (timeInterval < 0) {//將來時(shí)間
        result = [self willTimeToNow:formerTimeInterval];
    }
    else if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"剛剛"];
    }
    else if((temp = timeInterval/60) < 60){
        result = [NSString stringWithFormat:@"%ld分鐘前",temp];
    }
    else if((temp = temp/60) < 24){
        result = [NSString stringWithFormat:@"%ld小時(shí)前",temp];
    }
    else if((temp = temp/24) < 30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    else if((temp = temp/30) < 12){
        result = [NSString stringWithFormat:@"%ld個(gè)月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    return  result;
}

#pragma mark 將來時(shí)間距離現(xiàn)在多久
+(NSString *)willTimeToNow:(NSInteger)willTimeInterval
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:willTimeInterval];
    //方法一
    //NSDate *willDate = [[NSDate date] laterDate:date];
    //NSTimeInterval timeInterval = [willDate timeIntervalSinceNow];
    //方法二
    NSTimeInterval timeInterval = [date timeIntervalSinceDate:[NSDate date]];
    
    long temp = 0;
    NSString *result;
    if (timeInterval < 0) {//以前時(shí)間
        result = [self timeFromToNow:willTimeInterval];
    }
    else if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"剛剛"];
    }
    else if((temp = timeInterval/60) < 60){
        result = [NSString stringWithFormat:@"%ld分鐘后",temp];
    }
    else if((temp = temp/60) < 24){
        result = [NSString stringWithFormat:@"%ld小時(shí)后",temp];
    }
    else if((temp = temp/24) < 30){
        result = [NSString stringWithFormat:@"%ld天后",temp];
    }
    else if((temp = temp/30) < 12){
        result = [NSString stringWithFormat:@"%ld個(gè)月后",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年后",temp];
    }
    return  result;
}

#pragma mark 今天、明天、后天的最晚時(shí)間戳
+(NSInteger)indexDayTimeInterval:(NSInteger)dayCount
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [calendar components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[NSDate date]];
    [components setDay:([components day]+dayCount)];
    //當(dāng)前日期
    NSDate *indexDate = [calendar dateFromComponents:components];
    //日期轉(zhuǎn)字符串
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dayString = [dateFormatter stringFromDate:indexDate];
    //字符串日期+一天最晚時(shí)間
    NSString *end = [NSString stringWithFormat:@"%@ 23:59:59",dayString];
    //字符串轉(zhuǎn)時(shí)間戳
    NSDateFormatter *endFormatter = [[NSDateFormatter alloc] init];
    [endFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *endDate = [endFormatter dateFromString:end];
    NSInteger endTimeInterval = [endDate timeIntervalSince1970];
    return endTimeInterval;
}

@end
最后編輯于
?著作權(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)容