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)系作者
  • 序言:七十年代末腮介,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子端衰,更是在濱河造成了極大的恐慌叠洗,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,548評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件旅东,死亡現(xiàn)場(chǎng)離奇詭異惕味,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)玉锌,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門名挥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人主守,你說我怎么就攤上這事禀倔。” “怎么了参淫?”我有些...
    開封第一講書人閱讀 167,990評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵救湖,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我涎才,道長(zhǎng)鞋既,這世上最難降的妖魔是什么力九? 我笑而不...
    開封第一講書人閱讀 59,618評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮邑闺,結(jié)果婚禮上跌前,老公的妹妹穿的比我還像新娘。我一直安慰自己陡舅,他們只是感情好抵乓,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著靶衍,像睡著了一般灾炭。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上颅眶,一...
    開封第一講書人閱讀 52,246評(píng)論 1 308
  • 那天蜈出,我揣著相機(jī)與錄音,去河邊找鬼涛酗。 笑死掏缎,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的煤杀。 我是一名探鬼主播眷蜈,決...
    沈念sama閱讀 40,819評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼沈自!你這毒婦竟也來了酌儒?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,725評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤枯途,失蹤者是張志新(化名)和其女友劉穎忌怎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體酪夷,經(jīng)...
    沈念sama閱讀 46,268評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡榴啸,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了晚岭。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鸥印。...
    茶點(diǎn)故事閱讀 40,488評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖坦报,靈堂內(nèi)的尸體忽然破棺而出库说,到底是詐尸還是另有隱情,我是刑警寧澤片择,帶...
    沈念sama閱讀 36,181評(píng)論 5 350
  • 正文 年R本政府宣布潜的,位于F島的核電站,受9級(jí)特大地震影響字管,放射性物質(zhì)發(fā)生泄漏啰挪。R本人自食惡果不足惜信不,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望亡呵。 院中可真熱鬧抽活,春花似錦、人聲如沸政己。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽歇由。三九已至,卻和暖如春果港,著一層夾襖步出監(jiān)牢的瞬間沦泌,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工辛掠, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留谢谦,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,897評(píng)論 3 376
  • 正文 我出身青樓萝衩,卻偏偏與公主長(zhǎng)得像回挽,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子猩谊,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容