[英]NSDateFormatter with NSIslamicCalendar doesn't work correctly
[英]NSDateFormatter doesn't work
嗨!
我有一個字符串: 2012-11-07 15:22:06
,我想對它做一個NSDateFormatter
。 我做:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm, MMMM d, yyyy"];
NSString *date = [[NSString alloc] initWithFormat:@"%@", [df dateFromString:myString]];
但是它不起作用,它返回“ (null)
”。
希望有人能幫助我。
謝謝
因此您設置的格式為[df setDateFormat:@“ HH:mm,MMMM d,yyyy”];
字符串myString為@“ 2012-11-07 15:22:06”
=>不匹配
---所以我聚集在一起閱讀您對其他答案的評論:
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
@autoreleasepool {
NSString *myString = @"2012-11-07 15:22:06"; //yyyy-MM-dd HH:mm:ss
//convert to date
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-DD HH:mm:ss"];
NSDate *myDate = [df dateFromString:myString];
NSLog(@"%@", myDate); //2012-01-07 14:22:06 +0000
//output it in natural language
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[df setTimeStyle:NSDateFormatterNoStyle];
[df setDateStyle:NSDateFormatterMediumStyle];
[df setLocale:usLocale];
NSLog(@"%@", [df stringFromDate:myDate]); //Jan 7, 2012
}
}
您的格式與字符串的格式甚至都不接近。 采用:
yyyy-MM-dd HH:mm:ss
使用以下格式:yyyy-MM-dd HH:mm:ss以獲取格式:2012-11-07 15:22:06。
獲取格式:2012年11月7日,請使用以下功能:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSString *string=[dateFormatter stringFromDate:date];
NSDate *newDate=[dateFormatter dateFromString:string];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.