我有保留一個的NSString日期:如何從字符串變量格式化數據
1900-01-01T11:00:00
我需要格式化,所以我有一個格式化:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];
如何現在格式化這個字符串?
NSString* formatedDateString = [df ???];
我有保留一個的NSString日期:如何從字符串變量格式化數據
1900-01-01T11:00:00
我需要格式化,所以我有一個格式化:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];
如何現在格式化這個字符串?
NSString* formatedDateString = [df ???];
創建日期格式化器,一個返回日期對象用於給定的字符串,創建第二個日期格式化器,返回在期望的字符串格式。
NSDateformatter *inputFormatter = [[NSDateformatter alloc] init];
[inputFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
NSDate *date = [inputFormatter dateFromString:dateString];
NSDateformatter *outputFormatter = [[NSDateformatter alloc] init];
[outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[outputFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];
NSString *outputString = [outputFormatter stringFromDate:date];
但你也可以讓輸出日期格式決定中關於語言環境的風格:
NSDateformatter *outputFormatter = [[NSDateformatter alloc] init];
[outputFormatter setDateStyle:NSDateFormatterShortStyle];
[outputFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *outputString = [outputFormatter stringFromDate:date];
了美國語言環境,你現在應該貓所需的格式。
如果要強制格式「DD/MM/YYYY HH:mm:ss的一個」用戶,你還應該設置輸出格式日期的語言環境。
一個完整的命令行示例。
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *dateString1 = @"2012-12-31T11:00:00";
NSString *dateString2 = @"2012-12-31T14:00:00";
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[inputFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[outputFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];
NSDate *date1 = [inputFormatter dateFromString:dateString1];
NSDate *date2 = [inputFormatter dateFromString:dateString2];
NSString *outputString1 = [outputFormatter stringFromDate:date1];
NSString *outputString2 = [outputFormatter stringFromDate:date2];
NSLog(@"%@", outputString1);
NSLog(@"%@", outputString2);
}
return 0;
}
如果你不設置像[outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
的語言環境會導致做的格式醜陋的混合與用戶的sefault語言環境,如果沒有happend是「EN_US」
即德國:31/12/2012 02:00:00 nachm.
這是一個格式不在德文中使用。
爲了支持用戶的語言和語言環境,您應該使用不同的樣式。
提供的q&a rmaddy表明,有罕見的情況下,當解析格式被改寫。爲了避免這種情況,設置輸入格式的格式在一定的語言環境,如[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
例如用於獲取當前日期:
-(void) getDate
{
NSString *theDate;
NSString *theTime;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm:ss"];
NSDate *now = [[NSDate alloc] init];
theDate = [dateFormat stringFromDate:now];
theTime = [timeFormat stringFromDate:now];
//2012-09-21 02:00:00
self.deviceCurrentTime=[NSString stringWithFormat:@"%@ %@",theDate,theTime];
}
您需要創建兩個日期格式,一個用於分析,一個用於格式化;您的解析格式爲「yyyy-MM-ddThh:mm:ss」,輸出格式爲「dd/MM/yyyy hh:mm:ss a」。所以,這樣的事情:
NSString * targetString = @"1900-01-01'T'11:00:00";
NSDateFormatter *parseFormat = [[NSDateFormatter alloc] init];
[parseFormat setDateFormat:@"yyyy-MM-ddThh:mm:ss"];
NSDate * date = [parseFormat dateFromString:targetString];
NSDateFormatter * outputFormat = [[NSDateFormatter alloc] init];
[outputFormat setDateFormat:@"dd/MM/yyyy hh:mm:ss a";
NSString * outputString = [parseFormat stringFromDate:date];
日期,我在初始字符串爲「1900-01-01T11:00:00」你的代碼「日期」爲空,因此「outputString」也是空 – 1110
我的格式是不正確的 – vikingosegundo
這是一場噩夢,我仍然得到空 – 1110