gpt4 book ai didi

ios - 从 iOS 中的字符串中删除毫秒

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:06:01 25 4
gpt4 key购买 nike

尝试从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com