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

How to calculate remaining Days, Hours, Mins and seconds from given date?

在我的应用程序中,我需要从来自 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]);

谁能帮帮我。

你在这里犯了错误,你的 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]);

调用以下方法并传递您的日期

+(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;

}