dateFromString dosn't work properly

Discussion in 'iOS Programming' started by skunkio, May 18, 2010.

  1. skunkio macrumors newbie

    Joined:
    Apr 25, 2010
    #1
    Hi all,
    as per my object i have some problems using dateFromString that seems returns always a NSDate object null or another date different from the date i'm using.
    To set a date on a DataPicker i use the code below:

    Code:
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    
    NSDate *myDate = [df dateFromString: self.selectedDate];
    
    NSLog(@"data 1 %@", self.selectedDate);
    NSLog(@"data 2 %@", myDate);
    		
    if (myDate) {
            dtpDate.date = myDate;
    }
    
    this is the NSLog result

    Code:
    data 1 2010-05-12 21:45:00
    data 2 1970-01-01 01:00:00 +0100
    
    If use setDateFormat before create myDate, it's always null

    Code:
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    
    [df setDateFormat:@"yyyy-MM-dd hh:mm"];
    		
    NSDate *myDate = [df dateFromString: self.selectedDate];
    		
    NSLog(@"data 1 %@", self.selectedDate);
    NSLog(@"data 2 %@", myDate);
    		
    if (myDate) {
    	dtpDate.date = myDate;
    }
    
    this is the result

    Code:
    data 1 2010-05-12 21:45:00
    data 2 (null)
    
    If i use NSlocale

    Code:
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    		
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [df setLocale:locale];
    		
    NSDate *myDate = [df dateFromString: self.selectedDate];
    		
    NSLog(@"data 1 %@", self.selectedDate);
    NSLog(@"data 2 %@", myDate);
    		
    if (myDate) {
    	dtpDate.date = myDate;
    }
    
    there isn't effect.
    myDate is null if use setDateFormat otherwise the date is wrong.
    How can i set a DataPicket using a date of mine?


    Ciao,
    stè
     
  2. skunkio thread starter macrumors newbie

    Joined:
    Apr 25, 2010
    #2
    this code works

    Code:
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    		
    NSDate *myDate = [df dateFromString: self.selectedDate];
    		
    NSLog(@"data 1 %@", self.selectedDate);
    NSLog(@"data 2 %@", myDate);
    		
    if (myDate) {
    	dtpDate.date = myDate;
    }
    
    Code:
    data 1 2010-05-12 21:45:00
    data 2 2010-05-12 21:45:00 +0200
    

    Ciao,
    stè
     

Share This Page