在iOS中获取当前日期的中午12点时间戳



我想要当前日期的时间戳作为中午 12 点的参考。我们可以得到当前的时间戳,例如

[[NSDate Date] timeIntervalSince1970];

但是我想要今天中午 12 点的时间戳。提前致谢

我刚刚用了这个.这将为您提供中午 12 点的时间戳。因为它是根据 [NSDate 日期] 计算的,它会根据设备的时区为您提供。

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *today12Noon = [calendar dateBySettingHour:12 minute:0 second:0 ofDate:[NSDate date] options:0];
double dif = [[NSDate date] timeIntervalSince1970] - [today12Noon timeIntervalSince1970];
double timeStampWithReference12Noon = [[NSDate date] timeIntervalSince1970] - dif ;

将 Date2 设置为设备中的当前时间。

NSDate *date1 = [NSDate dateWithString:@"2010-01-01 00:00:00 +0000"];
NSDate *date2 = [NSDate dateWithString:@"2010-02-03 00:00:00 +0000"];
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
int numberOfDays = secondsBetween / 86400;
NSLog(@"There are %d days in between the two dates.", numberOfDays);

最新更新