Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

skunkio

macrumors newbie
Original poster
Apr 25, 2010
28
0
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è
 
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è
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.