HH:MM am/pm 未从 nsdate 获取
HH:MM am/pm not fetch from nsdate
我想从 nsdate 中获取 hours:minutes AM/PM。
NSDate *globalDateTime; //globaldate is : 2016-10-20 06:49:00 +0000 (UTC)
NSString *dateString12 = [NSDateFormatter localizedStringFromDate:globalDateTime
dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterFullStyle];
NSLog(@"%@",dateString12);
我得到的结果是 NSString 格式:20/10/16,2:03:00印度标准时间下午
预期结果为 NSString 格式:20/10/16,6:49:00印度标准时间下午
或者有什么方法我只能得到 NSString 格式的预期结果:6:49 PM
dateString12
在 NSLog 中获取的是 UTC 格式或其他格式的内容。它给了我正确的日期但没有给我正确的时间。
请帮助我如何获得合适的时间。
试试这个。
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm a"];// change here as per your need
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
NSString *stringFromDate = [formatter stringFromDate: globalDateTime];
我想从 nsdate 中获取 hours:minutes AM/PM。
NSDate *globalDateTime; //globaldate is : 2016-10-20 06:49:00 +0000 (UTC)
NSString *dateString12 = [NSDateFormatter localizedStringFromDate:globalDateTime
dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterFullStyle];
NSLog(@"%@",dateString12);
我得到的结果是 NSString 格式:20/10/16,2:03:00印度标准时间下午
预期结果为 NSString 格式:20/10/16,6:49:00印度标准时间下午
或者有什么方法我只能得到 NSString 格式的预期结果:6:49 PM
dateString12
在 NSLog 中获取的是 UTC 格式或其他格式的内容。它给了我正确的日期但没有给我正确的时间。
请帮助我如何获得合适的时间。
试试这个。
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm a"];// change here as per your need
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
NSString *stringFromDate = [formatter stringFromDate: globalDateTime];