gpt4 book ai didi

ios - 将 NSDate 转换为另一种格式

转载 作者:行者123 更新时间:2023-11-28 21:36:47 27 4
gpt4 key购买 nike

我有一个 NSDate 记录到控制台(在我的机器上使用我的设置),如下所示:

2015-12-30 18:10:27 +0000

我如何将它转换为可以像这样存储在远程服务器上的字符串?

2015-12-30 18:10:27

由于服务器必须为多个客户端提供服务,因此服务器上的日期始终采用一致的格式非常重要。

我的理解是 NSFormatter 使用因客户端而异的本地设置,因此我不能依赖它始终提供相同的字符串格式来保存在服务器上,因此我不知所措:

这是我目前的代码:

    NSDate *date = self.datePicker.date;
NSLog(@"date is:%@",date); //logs as above => 2015-12-30 18:10:27 +0000
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *datestring = [dateFormatter stringFromDate:date];
NSLog(@"datestring%@",datestring); //logs => 2015-12-30T13:10:27.000Z

这不是我想要的,我也不相信这会始终在不同的手持设备上创建相同的格式以保存在服务器上。

如有任何建议,我们将不胜感激。

最佳答案

您可以使用 UNIX 时间戳:

NSString *timestamp = [NSString stringWithFormat:@"%f",[date timeIntervalSince1970] * 1000];

或者您可以设置 NSDateFormatter 的自定义日期格式:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
NSString *datestring = [dateFormatter stringFromDate:date];

关于ios - 将 NSDate 转换为另一种格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33595256/

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