gpt4 book ai didi

ios - 使用 NSDateFormatter 将字符串转换为日期

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:57:46 26 4
gpt4 key购买 nike

我正在尝试将此字符串 '2015-05-08T09:44:25.343' 格式转换为日期 'dd/MM/YYYY' 格式

我做错了什么?我的约会对象是零。

// convert 2015-05-08T09:44:25.343 to dd/MM/yyyy

NSString *str = @"2015-05-08T09:44:25.343";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];
NSDate *date = [dateFormat dateFromString:str];

NSLog(@"%@",date);
NSString *convertedString = [dateFormat stringFromDate:date];
NSLog(@"%@", convertedString);

最佳答案

NSString *str = @"2015-05-08T09:44:25.343";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"];// this string must match given string @"2015-05-08T09:44:25.343"

NSDate *date = [dateFormat dateFromString:str];

NSLog(@"%@",date);
[dateFormat setDateFormat:@"MM/dd/yyyy"];// this match the one you want to be
NSString *convertedString = [dateFormat stringFromDate:date];
NSLog(@"%@", convertedString);

关于ios - 使用 NSDateFormatter 将字符串转换为日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30733619/

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