有趣的问题让我感到高兴。
我收到从服务器到设备的字符串时间。然后我将其转换为NSDate。 当设备设置为显示24小时时,生活很好。
现在我在设置为12小时的设备上测试它。一切都停止了。日期将返回null
我第一次
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"HH:mm"];
self.startTime = [dateFormat dateFromString:(NSString *)self.startTime];
完美适用于显示24小时但不是12小时的设备。
然后我尝试了
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"hh:mm"];
self.startTime = [dateFormat dateFromString:(NSString *)self.startTime];
这项工作正常至中午12点,然后所有日期都返回为空
更新
我也尝试添加“a”但这仍然会导致返回null
if (startDate == nil)
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"hh:mm a"];
startDate = [dateFormat dateFromString:(NSString *)self.startTime];
}
更新2
添加本地,添加:添加全部的ss仍然无法正常工作
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
dateFormat.locale = twelveHourLocale;
[dateFormat setDateFormat:@"hh:mm a"];
startDate = [dateFormat dateFromString:(NSString *)self.startTime];
答案 0 :(得分:18)
关闭......我认为你需要:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:@"HH:mm"];
startDate = [dateFormatter dateFromString:(NSString *)self.startTime];
答案 1 :(得分:7)
你可以试试这个
12 to 24 hour format
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:@"hh:mm a"];
NSDate* newDate = [df dateFromString:LocationTrackingStartTime];
[df setDateFormat:@"HH:mm"];
newDate = [df stringFromDate:newDate];
24 to 12 hour format
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:@"yyyy-mm-dd hh:mm:ss"];
NSDate* newDate = [df dateFromString:[df stringFromDate:[NSDate date]]];
[df setDateFormat:@"hh:mm a"];
newDate = [df stringFromDate:[NSDate date]];
答案 2 :(得分:4)
你必须添加am / pm:
NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
dateFormat.locale = twelveHourLocale;
[dateFormat setDateFormat:@"hh:mm: a"];
答案 3 :(得分:2)
将24小时格式更改为12小时格式,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//Set 24 hours date format
dateFormatter.dateFormat = @"HH:mm";
//Here @"your24hours date string" is your string date with 24 hours format
NSDate *date = [dateFormatter dateFromString:@"your24hours date string"];
//Set 12 hours date format
dateFormatter.dateFormat = @"hh:mm a";
NSString *strDate = [dateFormatter stringFromDate:date];
NSLog (@"%@", strDate);
//Output is in 12 hours date format.
答案 4 :(得分:1)
您想再次将NSDate
强制转换为NSString
再转换为NSDate
。
首先,您必须将其转换为NSString
,而不是强制它!
我也支持12小时和24小时格式,我也没有问题。
答案 5 :(得分:1)
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:@"MM/dd/yyyy hh:mm a"];
NSDate* newDate = [df dateFromString:strDate4Convert];
[df setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
NSString *newTimeStr = [df stringFromDate:newDate];
答案 6 :(得分:1)
You just need to set the locale to take the am pm format. And everything else as usual.
NSDateFormatter * df = [[NSDateFormatter alloc]init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[df setLocale:locale];
[df setDateStyle:NSDateFormatterNoStyle];
[df setTimeStyle:NSDateFormatterShortStyle];
NSLog(@"%@", [df stringFromDate:pickUpDate]);