2017-04-05 29 views
0

我是iOS(Objective-c)編碼的新手,我被困在時間戳上。 我得到時間戳,而JSON解析即。 2017-04-30T14:30+00:00(GMT)。如何從這個時間戳得到日期,小時,分鐘和秒?我在GMT中獲得這種格式,是否有可能將其轉換爲"IST"?怎麼樣?如何從時間戳中獲取日期,時間,分鐘和秒數「2017-04-30T14:30 + 00:00(GMT)」?

+0

你想改變,我認爲這應該有助於區域:http://stackoverflow.com/a/7259553/6203030 http://stackoverflow.com/questions/13138957/convert-date-in-mm -dd-yyyy -forma-in-xcode –

+0

謝謝@AitorPagán的建議。這有助於轉換時區。 –

+0

收藏本頁:http://nsdateformatter.com – diatrevolo

回答

1

日期格式模式
甲日期模式是一個字符,其中的字符特定字符串是從日曆格式化時替換爲日期和時間數據,或用於解析時爲日曆生成數據串。以下是模式中用於顯示給定語言環境的適當格式的字符。下面舉例說明:

enter image description here

- (NSString *)curentDateStringFromDate:(NSDate *)dateTimeInLine withFormat:(NSString *)dateFormat { 
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; 

    [formatter setDateFormat:dateFormat]; 

    NSString *convertedString = [formatter stringFromDate:dateTimeInLine]; 

    return convertedString; 
} 

使用它象下面這樣:

NSString *dateString = [self curentDateStringFromDate:[NSDate date] withFormat:@"dd-MM-yyyy"]; 
NSString *timeString = [self curentDateStringFromDate:[NSDate date] withFormat:@"hh:mm:ss"]; 
NSString *hoursString = [self curentDateStringFromDate:[NSDate date] withFormat:@"h"]; 

Foundation framework,涉及用於此任務的使用(在任一方向上)的類是NSDateFormatterRefer here

下面的代碼將GMT轉換爲IST。

NSString *inDateStr = @"2000/01/02 03:04:05"; 
NSString *s = @"yyyy/MM/dd HH:mm:ss"; 

// about input date(GMT) 
NSDateFormatter *inDateFormatter = [[NSDateFormatter alloc] init]; 
inDateFormatter.dateFormat = s; 
inDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
NSDate *inDate = [inDateFormatter dateFromString:inDateStr]; 

// about output date(IST) 
NSDateFormatter *outDateFormatter = [[NSDateFormatter alloc] init]; 
outDateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"IST"]; 
outDateFormatter.dateFormat = s; 
NSString *outDateStr = [outDateFormatter stringFromDate:inDate]; 

// final output 
NSLog(@"[in]%@ -> [out]%@", inDateStr, outDateStr); 
+0

thanx幫助@ Suresh.D兄弟。格林尼治標準時間到IST完成謝謝你的幫助。但我的主要問題仍然是如何從該時間戳中獲取日期,小時數。如果我能得到很大幫助的幫助。 –

+0

@MaulikDesai更新了我的答案,請去檢查,如果你有答案,請接受答案。提前致謝。 –

+0

這幫助我感謝你的幫助。 –

相關問題