iOS開發之時間戳、NSString、NSDate三者的相互轉化

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

喜歡的朋友們可以點點贊打打賞加加好友常常交流共勉一起進步咯

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

#import <Foundation/Foundation.h>

@interface NSDate (Util)

///當前時間時間戳
+(NSInteger)getNowTimeInterval;

///時間戳轉字符串
+(NSString *)timeIntervalToString:(NSInteger)timeInterval;

///字符串轉時間戳
+(NSInteger)stringToTimeInterval:(NSString *)string;

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

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

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

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

/// 今天、明天、后天的最晚時間戳
+(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 當前時間時間戳
+(NSInteger)getNowTimeInterval
{
    NSDate *date = [NSDate date];
    NSInteger timeInterval = [date timeIntervalSince1970];
    return timeInterval;
}

#pragma mark 時間戳轉字符串
+(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 字符串轉時間戳
+(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 字符串轉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轉字符串
+(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 以前時間距離現在多久
+(NSString *)timeFromToNow:(NSInteger)formerTimeInterval
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:formerTimeInterval];
    
    NSTimeInterval  timeInterval = [date timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    if (timeInterval < 0) {//將來時間
        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小時前",temp];
    }
    else if((temp = temp/24) < 30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    else if((temp = temp/30) < 12){
        result = [NSString stringWithFormat:@"%ld個月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    return  result;
}

#pragma mark 將來時間距離現在多久
+(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) {//以前時間
        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小時后",temp];
    }
    else if((temp = temp/24) < 30){
        result = [NSString stringWithFormat:@"%ld天后",temp];
    }
    else if((temp = temp/30) < 12){
        result = [NSString stringWithFormat:@"%ld個月后",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年后",temp];
    }
    return  result;
}

#pragma mark 今天、明天、后天的最晚時間戳
+(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)];
    //當前日期
    NSDate *indexDate = [calendar dateFromComponents:components];
    //日期轉字符串
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dayString = [dateFormatter stringFromDate:indexDate];
    //字符串日期+一天最晚時間
    NSString *end = [NSString stringWithFormat:@"%@ 23:59:59",dayString];
    //字符串轉時間戳
    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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容