简体   繁体   English

从iOS中的String中删除milliSeconds

[英]Remove milliSeconds from String in iOS

Trying to remove milli seconds from String(date) 试图从String(日期)中删除毫秒

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]);

and i'm getting null 而且我变得空了

i'm expecting o/p something like (with out milli seconds) 我期待o / p之类的东西(毫秒秒)

"06/05/2016 13:17:34" “2016/06/05 13:17:34”

Please suggest... 请建议......

Copy and paste this Code working Perfectly 复制并粘贴此代码完美运行

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]);

Here is Output 这是输出

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 规则格式化器是你必须设置与字符串相同的日期格式,当你从字符串获取日期时,如果不匹配,那么你将得到null

try this:- 尝试这个:-

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]);

working Perfect 工作完美

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]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM