检查时间和日期是否在特定日期和时间之间 [英] Check if the time and date is between a particular date and time

查看:102
本文介绍了检查时间和日期是否在特定日期和时间之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个变量;

startDate - Wed Apr 12
endDate - 11月11日星期三
timeDuration - 从晚上11点开始上午11点。 (这是一个字符串,11AM是开始时间,12PM是结束时间)

startDate - Wed Apr 12 endDate - Wed Jun 11 timeDuration - 11.00 am from 11.00 pm. (this is a string, 11AM is the start time and 12PM is the end time)

1。)现在,我需要获取当前日期和时间( [NSDate date] )并检查当前日期是否在 startDate endDate 然后检查当前时间是否在 timeDuration 之间(如 11.00am从晚上11点开始

1.) Now, i need to get the current date and time ( [NSDate date] ) and check if the current date is between the startDate and endDate and then check if the current time is between the timeDuration (as in between 11.00am from 11.00pm)

2。)如果当前时间介于持续时间之间(在上午11点之间从晚上11点开始 )我需要计算以下情景的剩余时间;

2.) If the current time is between the time duration (as in between 11.00 am from 11.00 pm) I need to calculate the time remaining to the following scenarios;

a。)如果时间是上午10点,我需要计算到开始时间的时间上午11时00分。 (答案是 1小时)。

a.) If the time is 10am, i need to calculate the time to the start time which is 11.00am. (the answer is 1 hour).

b。)如果时间 11.45 AM ,因为时间大于开始时间( 11.00am ),我需要计算剩余时间到结束时间(是 11.00pm )。

b.) If the time is 11.45AM, since the time is greater than the start time (which was 11.00am) i need to calculate the time remaining to the end time (which is 11.00pm).

c。)如果时间 11.45pm ,因为时间大于结束时间( 11.00pm ),我需要打印一个说超时的NSLog。

c.) If the time is 11.45pm, since the time is greater than the end time (which is 11.00pm), i need to print a NSLog saying that 'Time Exceeded'.

推荐答案

基本思路是转换 startDate endDate 到实际的 NSDate 中,并将它们与当前日期进行比较。然后,如果当前日期在适当的范围内,则将开始和结束时间添加到当前日期,以便为开始和结束时间提供特定时间点。获得这些日期后,您可以使用NSDate的 compare timeIntervalSinceDate 方法来获取所需的值:

The basic idea is to convert your startDate and endDate into actual NSDate's and compare them to the current date. Then if the current date is in the proper range, you add the start and end times onto the current date to come up with a specific point in time for the starting and ending times. Once you have those dates, you can use the compare and timeIntervalSinceDate methods of NSDate to get the values that you need:

// NOTE:  NO error checking is being done in this code.

// Starting variables  
NSString *startDateString     = @"Wed Apr 12";
NSString *endDateString       = @"Wed Jun 11";
NSString *timeDurationString = @"11.00 am from 11.00 pm";

// Get the current date
NSDate *currentTime        = [NSDate date];
NSCalendar *cal            = [NSCalendar currentCalendar];
NSDateComponents 
         *currentDateComps = [cal components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit 
                                    fromDate:currentTime];

// Add the current year onto our date string
NSString *startDateStringWithYear = [NSString stringWithFormat:@"%@ %d", startDateString, currentDateComps.year];
NSString *endDateStringWithYear   = [NSString stringWithFormat:@"%@ %d", endDateString, currentDateComps.year];

// Calculate NSDate's for start/endDate
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEE MM dd yyyy"];
NSDate *startDate = [formatter dateFromString:startDateStringWithYear];
NSDate *endDate   = [formatter dateFromString:endDateStringWithYear];

// Return if today's date is not between the start and end dates
if ([startDate compare:currentTime] == NSOrderedDescending || 
    [endDate compare:currentTime] == NSOrderedAscending) 
{
    return;
}

// Break the timeDurationString into separate words
NSArray *timeDurationStringWords = [timeDurationString componentsSeparatedByString:@" "];

// Calculate NSDate's for the start time for today:
NSString *startTimeString = [timeDurationStringWords objectAtIndex:0];
currentDateComps.hour     = [[startTimeString substringToIndex:2] intValue];
currentDateComps.minute   = [[startTimeString substringFromIndex:3] intValue];
if ([[timeDurationStringWords objectAtIndex:1] isEqualToString:@"pm"]) 
{
    currentDateComps.hour += 12;
}
NSDate *startTime         = [cal dateFromComponents:currentDateComps];

// And now the end time for today
NSString *endTimeString   = [timeDurationStringWords objectAtIndex:3];
currentDateComps.hour     = [[endTimeString substringToIndex:2] intValue];
currentDateComps.minute   = [[endTimeString substringFromIndex:3] intValue];
if ([[timeDurationStringWords objectAtIndex:4] isEqualToString:@"pm"]) 
{
    currentDateComps.hour += 12;
}
NSDate *endTime           = [cal dateFromComponents:currentDateComps];

// Now we have what we really want:  A specific start time, current time, and end time.

// Check to see if we are waiting to start:
if ([startTime compare:currentTime] == NSOrderedDescending) {
    NSTimeInterval minutesToStartTime = [startTime timeIntervalSinceDate:currentTime] / 60;
    NSLog(@"Start time is in %02d+%02d", (int)(minutesToStartTime / 60), (int)minutesToStartTime % 60);
    return;
}

// Check to see if we are late:
if ([endTime compare:currentTime] == NSOrderedAscending) {
    NSLog(@"Time Exceeded");
    return;
}

// We are between the two times:
NSTimeInterval minutesToEndTime = [endTime timeIntervalSinceDate:currentTime] / 60;
NSLog(@"End time is in %02d+%02d", (int)(minutesToEndTime / 60), (int)minutesToEndTime % 60);

这篇关于检查时间和日期是否在特定日期和时间之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆