iOS獲取NSDate的年月日

開發(fā)中我們有的時候會對日期時間進行大小比較,時間格式化,時間的年份,月份,天進行處理,關(guān)于獲取NSDate的的年月日目前有兩種解決方式:
1.NSDateFormat獲取

    NSDate *date =[NSDate date];//簡書 FlyElephant
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

    [formatter setDateFormat:@"yyyy"];
    NSInteger currentYear=[[formatter stringFromDate:date] integerValue];
    [formatter setDateFormat:@"MM"];
    NSInteger currentMonth=[[formatter stringFromDate:date]integerValue];
    [formatter setDateFormat:@"dd"];
    NSInteger currentDay=[[formatter stringFromDate:date] integerValue];
    
    NSLog(@"currentDate = %@ ,year = %ld ,month=%ld, day=%ld",date,currentYear,currentMonth,currentDay);

2.NSDateComponents獲取

    NSDate  *currentDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
    
    NSInteger year=[components year];
    NSInteger month=[components month];
    NSInteger day=[components day];
    NSLog(@"currentDate = %@ ,year = %ld ,month=%ld, day=%ld",currentDate,year,month,day);

以上兩種方式都能輕松獲取年月日,iOS默認的NSDate是格林尼治時間,比中國時區(qū)的時間少8個小時,處理過日期的都知道8個小時的誤差存在,我們在獲取年月日的時候不要加上8個小時,iOS系統(tǒng)會自動幫我們自動加上八個小時,以下是個人測試代碼:

    //簡書 FlyElephant
    NSString *dateString = @"2016-03-31 23:59:00";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *mydate=[formatter dateFromString:dateString];
    [formatter setDateFormat:@"yyyy"];
    NSInteger currentYear=[[formatter stringFromDate:mydate] integerValue];
    [formatter setDateFormat:@"MM"];
    NSInteger currentMonth=[[formatter stringFromDate:mydate]integerValue];
    [formatter setDateFormat:@"dd"];
    NSInteger currentDay=[[formatter stringFromDate:mydate] integerValue];
    
    NSLog(@"currentDate = %@ ,year = %ld ,month=%ld, day=%ld",mydate,currentYear,currentMonth,currentDay);
    
    
    NSString *dateStr= @"2016-04-01 00:01:00";
    NSDateFormatter *nextformatter = [[NSDateFormatter alloc] init] ;
    [nextformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *date=[nextformatter dateFromString:dateStr];
    [nextformatter setDateFormat:@"yyyy"];
    NSInteger year=[[nextformatter stringFromDate:date] integerValue];
    [nextformatter setDateFormat:@"MM"];
    NSInteger month=[[nextformatter stringFromDate:date]integerValue];
    [nextformatter setDateFormat:@"dd"];
    NSInteger day=[[nextformatter stringFromDate:date] integerValue];
    
    NSLog(@"currentDate = %@ ,year = %ld ,month=%ld, day=%ld",date,year,month,day);

最終輸出的結(jié)果:

currentDate = 2016-03-31 15:59:00 +0000 ,year = 2016 ,month=3, day=31
currentDate = 2016-03-31 16:01:00 +0000 ,year = 2016 ,month=4, day=1

NSDate對象存放的日期始終是UTC的標準時間,如果想獲取本地時間可以根據(jù)時間偏移量計算,不過最終的時區(qū)還是GMT時區(qū).

- (NSDate *)getCurrentLocalDate:(NSDate *)date {
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
    NSTimeZone *destinationTimeZone = [NSTimeZone localTimeZone];

    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:date];

    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:date];
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    
    NSDate *destinationDateNow = [[NSDate alloc] initWithTimeInterval:interval sinceDate:date];
    
    return destinationDateNow;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容