ios - 将日期转换为具有不同日期格式 ios 的字符串?

标签 ios objective-c nsdate

我搜索了一些堆栈问题以将字符串转换为日期并且工作正常,但是我将我的字符串日期转换为 NSDate 而不是返回 nil。这是我的代码:

   NSString *dateStr = @"2016-07-29 09:05";
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-ddTHH:mm:ss.SSSZ"];
    NSDate *date = [dateFormat dateFromString:dateStr];

我想将此日期转换为 yyyy-MM-ddTHH:mm:ss.SSSZ 此格式。

这里有什么问题?

最佳答案

喜欢

NSString *dateStr = @"2016-07-29 09:05";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// set the date format related to what the string already you have
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:dateStr];
 [dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
 // again add the date format what the output u need
 [dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSString *finalDate = [dateFormat stringFromDate:date];
NSLog(@"finalDate==%@",finalDate);

输出

enter image description here

关于ios - 将日期转换为具有不同日期格式 ios 的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38783186/

相关文章:

ios - macOS 和 iOS 目标是否应该共享相同的授权文件?

ios - SWRevealViewController - 切换到另一个时如何保留前 View

objective-c - NSInputStream 获取缓冲区 : length: doesn't work?

iphone - 如何格式化字符串变量中的数据

iOS在未来创建日期而忽略夏令时

ios - cocoa touch 如何将标签设置为 NSDate

ios - 禁用自动隐藏字幕 ios Storyboard

没有搜索的 iOS 范围栏

ios - 如何将 UIExtendedSRGBColorSpace 与我的颜色值匹配

ios - 将 NSDates 天设置为相同的值