尝试从 String(date) 中删除毫秒
NSString *dateString = @"2016-05-16 13:17:34.674194";
NSDateFormatter* formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
NSDate *date = [formater dateFromString:dateString];
NSLog(@"%@",[formater stringFromDate:date]);
我得到了空值
我期待 o/p 类似(没有毫秒)
“2016 年 6 月 5 日 13:17:34”
请建议...
最佳答案
复制并粘贴此代码,完美运行
NSString *dateString = @" ";
NSDateFormatter* dateFormatter1 = [[NSDateFormatter alloc] init];
dateFormatter1.dateFormat = @"yyyy-MM-dd HH:mm:ss.SSS";
NSDate *yourDate = [dateFormatter1 dateFromString:dateString];
dateFormatter1.dateFormat = @"dd/MM/yyyy HH:mm:ss";
NSLog(@"%@",[dateFormatter1 stringFromDate:yourDate]);
这是输出
2016-05-16 16:43:53.639 StackLearn[6376:151614] 16/05/2016 13:17:34
Rule of Date formatter is you must set date format same like your string while you are getting date from string , if mismatch then you will get null
关于ios - 从 iOS 中的字符串中删除毫秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37252296/