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

时间:2017-01-25 12:53:15

标签: ios objective-c nsdate nsdateformatter nsdatecomponents

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

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]);

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

结束日期将是今天的日期。我尝试下面的代码,但它给我几天减去(我认为它需要UTC时区),并在SO上尝试了很多答案。

{
var myDate="26-02-2012";
myDate=myDate.split("-");
var newDate=myDate[1]+"/"+myDate[0]+"/"+myDate[2];
alert(new Date(newDate).getTime());
}

有人请帮助我卡住。

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

调用以下方法并传递日期

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

}