我正在转换此日期字符串 "05/20/2015 05:27 pm"
进入NSDate
在格式化程序的帮助下,但它返回 nil
.
-(NSDate *)dateFromString:(NSString *)strDate withFormat:(NSString *)strDateFormat
{
NSDate *date = nil;
[self.dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
if (strDate) {
if (strDateFormat) {
[self.dateFormatter setDateFormat:strDateFormat];
}
date = [self.dateFormatter dateFromString:strDate];
}
return date;
}
我正在使用上面的函数,其中我将字符串传递为 "05/20/2015 05:27 pm"
格式为@"dd/MM/yyyy HH:mm:ss"
.
最佳答案
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm/dd/yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *stringDate = [dateFormatter dateFromString:@"05/20/2015 05:27 pm"];
NSLog(@"%@", stringDate);
关于ios - 如何将像 "05/20/2015 05:27 pm"这样的日期字符串转换为 NSDate 然后转换为时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30349068/