如何将 objective c 中的秒转换为 UnixtimeStamp?
How to convert seconds into UnixtimeStamp in objective c?
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
// find next date where the minutes are zero
NSDate *nextHour = [calendar nextDateAfterDate:now matchingUnit:NSCalendarUnitMinute value:0 options:NSCalendarMatchNextTime];
// get the number of seconds between now and next hour
NSDateComponents *componentsToNextHour = [calendar components:NSCalendarUnitSecond fromDate:now toDate:nextHour options:0];
//NSLog(@"%ld", componentsToNextHour.second);
NSString *dec = [NSString stringWithFormat:@"%ld", (long)componentsToNextHour.second];
在上面的代码中,我将当前日期和时间转换为秒,但我无法将这些秒转换为 unixtimestamp,非常感谢任何有关如何进行此操作的想法。
我不确定我是否理解你的问题。如果您要求下一个小时开始的 Unix 时间戳,请执行以下操作:
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
// find next date where the minutes are zero
NSDate *nextHour = [calendar nextDateAfterDate:now matchingUnit:NSCalendarUnitMinute value:0 options:NSCalendarMatchNextTime];
time_t unixTimestampAtWhichNextHourStarts = (time_t)nextHour.timeIntervalSince1970;
NSLog(@"ts=%ld", (long)unixTimestampAtWhichNextHourStarts);
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
// find next date where the minutes are zero
NSDate *nextHour = [calendar nextDateAfterDate:now matchingUnit:NSCalendarUnitMinute value:0 options:NSCalendarMatchNextTime];
// get the number of seconds between now and next hour
NSDateComponents *componentsToNextHour = [calendar components:NSCalendarUnitSecond fromDate:now toDate:nextHour options:0];
//NSLog(@"%ld", componentsToNextHour.second);
NSString *dec = [NSString stringWithFormat:@"%ld", (long)componentsToNextHour.second];
在上面的代码中,我将当前日期和时间转换为秒,但我无法将这些秒转换为 unixtimestamp,非常感谢任何有关如何进行此操作的想法。
我不确定我是否理解你的问题。如果您要求下一个小时开始的 Unix 时间戳,请执行以下操作:
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
// find next date where the minutes are zero
NSDate *nextHour = [calendar nextDateAfterDate:now matchingUnit:NSCalendarUnitMinute value:0 options:NSCalendarMatchNextTime];
time_t unixTimestampAtWhichNextHourStarts = (time_t)nextHour.timeIntervalSince1970;
NSLog(@"ts=%ld", (long)unixTimestampAtWhichNextHourStarts);