尝试从字符串(日期)中删除毫秒数
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之类的事情(毫秒数)
" 06/05/2016 13:17:34"
请建议......
答案 0 :(得分:5)
复制并粘贴此代码完美运行
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
日期规则格式化程序是您必须设置与字符串相同的日期格式,当您从字符串获取日期时,如果不匹配则您将获得空白
答案 1 :(得分:1)
试试这个: -
NSString *dateString = @"2016-05-16 13:17:34.674194";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.SS";
NSDate *yourDate = [dateFormatter dateFromString:dateString];
dateFormatter.dateFormat = @"dd-MMM-yyyy HH:mm:ss";
NSLog(@"%@",[dateFormatter stringFromDate:yourDate]);
答案 2 :(得分:0)
完美的工作
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]);