如何计算给定日期的剩余天数、小时数、分钟数和秒数?

Posted

技术标签:

【中文标题】如何计算给定日期的剩余天数、小时数、分钟数和秒数?【英文标题】:How to calculate remaining Days, Hours, Mins and seconds from given date? 【发布时间】:2017-01-25 12:53:15 【问题描述】:

在我的应用程序中,我需要获取来自 JSON 响应的给定日期的天数、小时数、分钟数和秒数。以下是我得到的日期和时间:

"date_time" = "2017-01-31 08:30:00";

并且需要计算剩余天数:6小时:18分钟:24秒:30

结束日期将是今天的日期。我尝试了下面的代码,但它给了我几天的负数(我认为它需要 UTC 时区)并且还尝试了很多关于 SO 的答案。

NSString *start = [[self.eventsArray valueForKey:@"date_time"]objectAtIndex:0];


NSDateFormatter *date = [[NSDateFormatter alloc] init];
[date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *end = [date stringFromDate:[NSDate date]];


NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *startDate = [f dateFromString:start];
NSDate *endDate = [f dateFromString:end];


NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitDay
                                                    fromDate:startDate
                                                      toDate:endDate
                                                     options:0];
NSLog(@"day : %ld H: %ld M : %ld S:%ld", [components day], (long)[components hour],(long)[components minute],(long)[components second]);

请有人帮我困住。

【问题讨论】:

startDate 和 endDate 的值为 dateFromString:start 和 end 将是今天的日期。 看看NSDateComponentsFormatter 【参考方案1】:

您在这里犯了错误,您的fromDate 应该是今天的日期,即[NSDate date],而toDate 将是您的startDate。所以你的代码应该是这样的。

NSString *end = [[self.eventsArray valueForKey:@"date_time"]objectAtIndex:0];

NSDateFormatter *date = [[NSDateFormatter alloc] init];
[date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];    

NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//Start date
NSDate *startDate = [NSDate date];
NSDate *endDate = [f dateFromString:end];


NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond
                                                    fromDate:startDate
                                                      toDate:endDate
                                                     options:0];
NSLog(@"day : %ld H: %ld M : %ld S:%ld", [components day], (long)[components hour],(long)[components minute],(long)[components second]);

【讨论】:

你的意思是说'NSDate *startDate = [f dateFromString:start]; NSDate *endDate = [f dateFromString:end];' @Nij 您的起始日期是今天的日期,这样您就可以获得铰削天数。 @Nij 您还需要添加小时分钟和秒的组件,检查已编辑的答案。 感谢您的关注,这是我的错误,仍然得到这样的结果 'day : 5 H: 9223372036854775807 M : 9223372036854775807 S:9223372036854775807' 成功了 :) 我可以知道如何将该值更新为 tableviewcell 的标签吗?【参考方案2】:

调用下面的方法并传递你的日期

+(NSMutableString*) timeLeftSinceDate: (NSDate *) dateT 

    NSMutableString *timeLeft = [[NSMutableString alloc]init];

    NSDate *today10am = [NSDate date];

    today10am = [self getPickerDateForString:[self getPickerDateForDate:today10am]];

    NSInteger seconds = [today10am timeIntervalSinceDate:dateT];

    NSInteger days = (int) (floor(seconds / (3600 * 24)));
    if(days) seconds -= days * 3600 * 24;

    NSInteger hours = (int) (floor(seconds / 3600));
    if(hours) seconds -= hours * 3600;

    NSInteger minutes = (int) (floor(seconds / 60));
    if(minutes) seconds -= minutes * 60;

    if(days) 
        [timeLeft appendString:[NSString stringWithFormat:@"%ld Days ago", (long)days]];
    else if(hours) 
        [timeLeft appendString:[NSString stringWithFormat: @"%ld h ago", (long)hours]];
    else if(minutes) 
        [timeLeft appendString: [NSString stringWithFormat: @"%ld m ago",(long)minutes]];
    else if(seconds) 
        [timeLeft appendString:[NSString stringWithFormat: @"Just now"]];
    

    return timeLeft;


+ (NSString *)getPickerDateForDate:(NSDate *)dateVal

    NSString * stringVal;
    if (!dateFormatter)
    
        dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];

    stringVal = [dateFormatter stringFromDate:dateVal];
    return stringVal;

+ (NSDate *)getPickerDateForString:(NSString *)dateString

    NSDate * stringVal;
    if (!dateFormatter)
    
        dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    dateFormatter.timeZone = [NSTimeZone systemTimeZone];

    stringVal = [dateFormatter dateFromString:dateString];
    return stringVal;


【讨论】:

以上是关于如何计算给定日期的剩余天数、小时数、分钟数和秒数?的主要内容,如果未能解决你的问题,请参考以下文章

ActionScript 3.0 + 计算两个日期之间的时间跨度?

第六章第二十五题(将毫秒数转换成小时数分钟数和秒数)(Convert milliseconds to hours, minutes, and seconds) - 编程练习题答案

第六章第二十五题(将毫秒数转换成小时数分钟数和秒数)(Convert milliseconds to hours, minutes, and seconds) - 编程练习题答案

MySql计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数

MySql计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数

MySql计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数