比较从选择器和当前日期和时间中选择的日期和时间

时间:2011-08-26 04:10:07

标签: iphone objective-c

我正在使用日期和时间选择器。使用以下代码我从选择器中获取时间和日期,它在控制台中显示当前日期和时间。

- (void)eventForDatePicker:(id)sender {



    UIDatePicker *datePicker = (UIDatePicker *)sender;  

    NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
    df.dateStyle = NSDateFormatterMediumStyle; 
   [df setDateFormat:@"yyyy-MM-dd HH:mm"]; 
   // [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
// [df setDateFormat:@"dd hh-mm-ss"];

  //  message = [[NSString alloc] initWithFormat:@"%@", df];

   message = [NSString stringWithFormat:@"%@", 
                           [df stringFromDate:datePicker.date]]; 
    NSLog(@"%@sssssssssssssssssfff",message);



    NSUserDefaults *prefs4 = [NSUserDefaults standardUserDefaults];
    [prefs4 setObject:message forKey:@"name"];
    [prefs4   synchronize];




-(void)switchChanged:(id)sender {

NSUserDefaults *prefs5 = [NSUserDefaults standardUserDefaults];
        NSString *mess = [prefs5 stringForKey:@"name"];



        [datearray addObject:mess];



        NSLog(@"is isssssss%@",datearray);


        NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];

        [currentDefaults setObject:[NSKeyedArchiver archivedDataWithRootObject:datearray] forKey:@"datesaving"];
        [currentDefaults synchronize];



}    

  //Now in anotherview , retrieving the contents in date array and by calling a function a text is displaying.
  NSDate* date = [NSDate date];

    //Create the dateformatter object

    NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];

    //Set the required date format

    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];

    //Get the string date

     str = [formatter stringFromDate:date];

    //Display on the console

    NSLog(@"fdf %@",str);




    NSUserDefaults *currentDefaults2 = [NSUserDefaults standardUserDefaults];
    NSData *dataRepresentingSavedArray = [currentDefaults2 objectForKey:@"datesaving"];
    NSMutableArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];

    if(oldSavedArray!=nil){
    datess = [[NSMutableArray alloc] initWithArray:oldSavedArray];


    }





       NSLog(@"%doooooooooooooooo",[datess count]);

       NSLog(@"%@llllllllllll111111111111",datess);





-(void)check1
{
      //NSLog(@"%@llllllllllll",[datess objectAtIndex:0]);


    for(int j=0;j<[newinx count];j++)
    {


        if([str isEqualToString:[datess objectAtIndex:j]])
        {


                //tv.text  =[Tips objectAtIndex:day];
            tv.text=@"helloo";

        }

但是这个比较不起作用。任何人都可以帮助我。谢谢。提前。

4 个答案:

答案 0 :(得分:0)

为什么intValue中有NSLog(@"kykykykyky%d",[[datess objectAtIndex:0]intValue]);?如果你摆脱它(如果你已经在NSUserdefaults中正确保存了消息字符串),它应该打印与以前相同的字符串。

答案 1 :(得分:0)

为什么不保存NSDate中由UIDatePicker返回的NSUserDefaults对象?把它放在那里&amp;用于比较 - 不需要归档。它会更简单。

仅在您想要显示时才转换为NSDateFormatter

答案 2 :(得分:0)

NSDate *now = [NSDate date];
Compare the 2 dates with:

   if ([d compare:now] == NSOrderedDescending) {
        // d is later than now
   }else{
        // d is earlier than or equal to now
   }

答案 3 :(得分:0)

你错了......

NSLog(@"kykykykyky%d",[[datess objectAtIndex:0]intValue]);

你的datess数组包含字符串对象,那么如果你想在NSDate个对象中转换它,那么就可以使用NSDateFormatter

来自datess数组的第一个NSLog对象为 -

NSLog(@"date str = %@",[datess objectAtIndex:0]);

确认日期字符串正确保存。

然后创建一个NSDateFormatter对象并将其日期格式设置为 -

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];

[dateFormatter setDateFormat:@"MM-dd-yyyy hh:mm a"];

或者您可以使用蚂蚁格式样式。然后使用此NSDate作为

创建一个NSDateFormatter对象
NSDate *date = [dateFormatter dateFromString:[datess objectAtIndex:0]];

它会给你一个NSDate对象。

比较两个NSDate对象使用 -

- (BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate
{   
    if ([date compare:beginDate] == NSOrderedAscending)
        return NO;

    if ([date compare:endDate] == NSOrderedDescending) 
        return NO;

    return YES;
}