timeintervalsince1970 是秒还是毫秒 ios
展开全部
- (int)intervalSinceNow: (NSString *) theDate
{
NSDateFormatter *date=[[NSDateFormatter alloc] init];
[date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *d=[date dateFromString:theDate];
NSTimeInterval late=[d timeIntervalSince1970]*1;
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval now=[dat timeIntervalSince1970]*1;
NSString *timeString=@"";
NSTimeInterval cha=now-late;
if (cha/86400>1)
{
timeString = [NSString stringWithFormat:@"%f", cha/86400];
timeString = [timeString substringToIndex:timeString.length-7];
return [timeString intValue];
}
return -1;
}
面例计算相差几
计算其数值
cha/3600<1 钟
if (cha/3600>1&&cha/86400<1)
01
// 获取前期
02
NSDate *date = [NSDate date];
03
04
// 打印结: 前间 date = 2013-08-16 09:00:04 +0000
05
NSLog(@"前间 date = %@",date);
06
07
// 获取某期始往前或者往久期处60代表60秒需要获取前60改-60即
08
date = [[NSDate alloc] initWithTimeInterval:60 sinceDate:[NSDate date]];
09
10
//打印结:前间 往60s间date = 2013-08-16 09:01:04 +0000
11
NSLog(@"前间 往60s间date = %@",date);
PS:测试间午5点前间却午9点相差8区问题
解决办:
1
NSTimeZone *zone = [NSTimeZone systemTimeZone];
2
3
NSInteger interval = [zone secondsFromGMTForDate: date];
4
5
NSDate *localDate = [date dateByAddingTimeInterval: interval];
6
7
// 打印结 确前间 localDate = 2013-08-16 17:01:04 +0000
8
NSLog(@"确前间 localDate = %@",localDate);
{
NSDateFormatter *date=[[NSDateFormatter alloc] init];
[date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *d=[date dateFromString:theDate];
NSTimeInterval late=[d timeIntervalSince1970]*1;
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval now=[dat timeIntervalSince1970]*1;
NSString *timeString=@"";
NSTimeInterval cha=now-late;
if (cha/86400>1)
{
timeString = [NSString stringWithFormat:@"%f", cha/86400];
timeString = [timeString substringToIndex:timeString.length-7];
return [timeString intValue];
}
return -1;
}
面例计算相差几
计算其数值
cha/3600<1 钟
if (cha/3600>1&&cha/86400<1)
01
// 获取前期
02
NSDate *date = [NSDate date];
03
04
// 打印结: 前间 date = 2013-08-16 09:00:04 +0000
05
NSLog(@"前间 date = %@",date);
06
07
// 获取某期始往前或者往久期处60代表60秒需要获取前60改-60即
08
date = [[NSDate alloc] initWithTimeInterval:60 sinceDate:[NSDate date]];
09
10
//打印结:前间 往60s间date = 2013-08-16 09:01:04 +0000
11
NSLog(@"前间 往60s间date = %@",date);
PS:测试间午5点前间却午9点相差8区问题
解决办:
1
NSTimeZone *zone = [NSTimeZone systemTimeZone];
2
3
NSInteger interval = [zone secondsFromGMTForDate: date];
4
5
NSDate *localDate = [date dateByAddingTimeInterval: interval];
6
7
// 打印结 确前间 localDate = 2013-08-16 17:01:04 +0000
8
NSLog(@"确前间 localDate = %@",localDate);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询