如何从根据区域设置格式化的NSString获取NSDate(时间)?让我解释。根据设备的区域设置,我必须将HS中的myString转换为NSDate:ss(意大利格式)
NSLocale *locale = [NSLocale autoupdatingCurrentLocale];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle: NSDateFormatterShortStyle];
[dateFormatter setDateStyle: NSDateFormatterFullStyle];
[dateFormatter setLocale: [[NSLocale alloc] initWithLocaleIdentifier: [locale objectForKey: NSLocaleIdentifier]]];
我的NSString可以是:@“2014-01-01 6:15 PM”或@“2014-01-01 18:15”或@“2014-01-01 6:15 MUU”或???? 我的结果必须是:@“01/01/2014 18:15”
[dateFormatter setDateFormat:@"????"];
NSDate *myDate = [dateFormatter dateFromString:myNSString];
如何获取区域格式以正确设置格式化程序?
答案 0 :(得分:2)
你好,你必须写下这样的东西:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm";
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Italy"]];//include you're location from what country time you want
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"New NSDate (NSDate): %@", [dateFormatter stringFromDate:date]);
答案 1 :(得分:0)
NSString* stringDate = @"January 1, 2000 at 9:10 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSDate *tempDate = [dateFormatter dateFromString:stringDate];
[dateFormatter setDateFormat:@"HH:mm"];
stringDate = [dateFormatter stringFromDate:tempDate];