CLLocationManager的时间格式?

时间:2015-07-06 16:38:23

标签: nsdateformatter cllocationmanager

我有一个应用程序可以将用户的位置保存到后端数据库,然后将其读入以供其他地方使用。

我使用以下代码获取并将位置时间戳保存到我的服务器:

CLLocation *currentLocation = [locations lastObject];
currentLocation.timestamp // this is the timestamp I save to the database.

稍后,我重读了这篇文章,但是我无法将其格式化为可用的日期对象。

我的代码是:

    NSLog(@"time from server = %@", item.timestamp);
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *savedTime = [[NSDate alloc] init];
    savedTime = [dateFormatter dateFromString:item.timestamp];
    NSLog(@"time formatted = %@", savedTime);

当我运行它时,我得到以下输出:

time from server = 2015-07-05 13:32:36  0000
time formatted = (null)

任何人都可以帮助实现这个"时间格式化"输出工作?

是因为setDateFormat不正确吗?我不确定' 0000'在服务器'的时间结束时是

1 个答案:

答案 0 :(得分:1)

尝试使用此格式:

@"yyyy-MM-dd HH:mm:ss zzz"

这对我来说现在很好用:

enter image description here

  

这在图片中很快,但重要的部分是日期格式字符串。这与语言无关,我只想告诉你它正在运行。替换ObjC中的格式字符串就可以了。

相关问题