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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市冒黑,隨后出現(xiàn)的幾起案子田绑,更是在濱河造成了極大的恐慌,老刑警劉巖薛闪,帶你破解...
    沈念sama閱讀 217,185評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件辛馆,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡豁延,警方通過查閱死者的電腦和手機(jī)昙篙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來诱咏,“玉大人苔可,你說我怎么就攤上這事〈” “怎么了焚辅?”我有些...
    開封第一講書人閱讀 163,524評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長苟鸯。 經(jīng)常有香客問我同蜻,道長,這世上最難降的妖魔是什么早处? 我笑而不...
    開封第一講書人閱讀 58,339評(píng)論 1 293
  • 正文 為了忘掉前任湾蔓,我火速辦了婚禮,結(jié)果婚禮上砌梆,老公的妹妹穿的比我還像新娘默责。我一直安慰自己贬循,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評(píng)論 6 391
  • 文/花漫 我一把揭開白布桃序。 她就那樣靜靜地躺著杖虾,像睡著了一般。 火紅的嫁衣襯著肌膚如雪媒熊。 梳的紋絲不亂的頭發(fā)上奇适,一...
    開封第一講書人閱讀 51,287評(píng)論 1 301
  • 那天,我揣著相機(jī)與錄音泛释,去河邊找鬼滤愕。 笑死,一個(gè)胖子當(dāng)著我的面吹牛怜校,可吹牛的內(nèi)容都是我干的间影。 我是一名探鬼主播,決...
    沈念sama閱讀 40,130評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼茄茁,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼魂贬!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起裙顽,我...
    開封第一講書人閱讀 38,985評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤付燥,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后愈犹,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體键科,經(jīng)...
    沈念sama閱讀 45,420評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評(píng)論 3 334
  • 正文 我和宋清朗相戀三年漩怎,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了勋颖。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,779評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡勋锤,死狀恐怖饭玲,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情叁执,我是刑警寧澤茄厘,帶...
    沈念sama閱讀 35,477評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站谈宛,受9級(jí)特大地震影響次哈,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜吆录,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評(píng)論 3 328
  • 文/蒙蒙 一亿乳、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦葛假、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至恢氯,卻和暖如春带斑,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背勋拟。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評(píng)論 1 269
  • 我被黑心中介騙來泰國打工勋磕, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人敢靡。 一個(gè)月前我還...
    沈念sama閱讀 47,876評(píng)論 2 370
  • 正文 我出身青樓挂滓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親啸胧。 傳聞我的和親對(duì)象是個(gè)殘疾皇子赶站,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評(píng)論 2 354

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