CLLocationManager 时间格式?
CLLocationManager time format?
我有一个应用程序可以将用户的位置保存到后端数据库,然后读取这些信息以供其他地方使用。
我正在使用以下代码获取位置时间戳并将其保存到我的服务器:
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)
任何人都可以帮助使这个 "time formatted" 输出正常工作吗?
是不是setDateFormat设置不对?我不确定 'time from server' 末尾的“0000”是什么?
尝试使用此格式:
@"yyyy-MM-dd HH:mm:ss zzz"
现在这对我来说很好用:
This is swift in the picture, but the important part is the date format string. That is language independent and I just wanted to show you that it was working. Replacing the format string in ObjC will work just fine.
我有一个应用程序可以将用户的位置保存到后端数据库,然后读取这些信息以供其他地方使用。
我正在使用以下代码获取位置时间戳并将其保存到我的服务器:
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)
任何人都可以帮助使这个 "time formatted" 输出正常工作吗?
是不是setDateFormat设置不对?我不确定 'time from server' 末尾的“0000”是什么?
尝试使用此格式:
@"yyyy-MM-dd HH:mm:ss zzz"
现在这对我来说很好用:
This is swift in the picture, but the important part is the date format string. That is language independent and I just wanted to show you that it was working. Replacing the format string in ObjC will work just fine.