您的位置:首页 > 移动开发 > IOS开发

iOS 当前时间距离过去某个时间点的秒数

2015-05-27 17:44 253 查看
做蓝牙开发的时候要设置同步时间,以秒为单位,当前时间距离2000-01-01 00:00:00的秒数。网上搜了好久的资料都没找到合适的,然后研究了一下。具体写法如下:

- (NSTimeInterval)get2000yearToNowSecond

{

  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // 设置时间格式

NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"GMT"];

[dateFormatter setTimeZone:timeZone]; //设置时区 +8:00

NSString *someDayStr= @"2000-01-01 00:00:00"; // 设置过去的某个时间点比如:2000-01-01 00:00:00

NSDate *someDayDate = [dateFormatter dateFromString:someDayStr];

NSDate *currentDate = [NSDate date];

NSTimeInterval time=[currentDate timeIntervalSinceDate:someDayDate]; //当前时间距离2000-01-01 00:00:00的秒数

NSLog(@"%lld",(long long int)time);

return time;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: