I accessing a web service after i parsing that that code i got AiringTime of US EST 2011-10-17T14:00:00Z remaining code i written below
NSString *string = [[dateArray objectAtIndex:0] objectForKey:#"AiringTime"];
NSArray *stringArray1 = [string componentsSeparatedByString:#"T"];
NSString *string1 = [stringArray1 objectAtIndex:1];
NSArray *stringArray2 = [string1 componentsSeparatedByString:#"Z"];
NSString *dateInString = [stringArray2 objectAtIndex:0];
nowDate =[stringArray1 objectAtIndex:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh:mm:ss"];
NSDate *dateFromString = [formatter dateFromString:dateInString];
[formatter release];
above NSDate *dateFromString given me appropriate output like 14:00 from morning to evening 6:30PM. After evening 6:30PM dateFromString gave mi nil value even though dateInString has an appropriate value?? Why? and How it happen?
I mean,
NSDate *dateFromString = [formatter dateFromString:dateInString];
this line not working, dateFromString gave me nil output.
I wasting my last 3 days evening time due to this strange problem. Please help me.
Thanks in advance.
See here, the example format is exactly the one you are dealing with.
Instead of splitting and manipulating the string yourself, simply do the following:
NSString *string = [[dateArray objectAtIndex:0] objectForKey:#"AiringTime"];
//Time in according to GMT+
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dateFromString = [formatter dateFromString:string];
[formatter release];
If you are only interested in the time then NSDate is meaningless. What do you want to do with it afterwards?
Related
I'm trying to convert a string date (2011-06-08T08:05:00.000-08:00) into a NSDate using the following code:
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
//Translate 2011-06-08T08:05:00.000-08:00 into 2011-06-08T08:05:00.000-0800
stringDate = [stringDate stringByReplacingOccurrencesOfString:#":" withString:#"" options:0 range:NSMakeRange([aDate length] - 5, 5)];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSSZZZ"];
NSDate *dateFromString = [dateFormatter dateFromString:stringDate];
At this point, dateFromString is 2011-06-08 16:05:00 +0000. What I actually wanted was for dateFromString to be 2011-06-08 08:05:00 -0800. What am I doing wrong?
Thanks!
So this will not have a direct answer and will assume that your string format will be the same. What we are going to extract the timezone part (last 5 characters) and then calculate how many seconds we are off from GMT. NSTimeZone has a convenience method timeZoneForSecondsFromGMT: that will help us get what we need. This is what you need to add to the code in the question.
NSString * zoneString = [stringDate substringFromIndex:([stringDate length] - 5)];
NSTimeInterval timeInterval = [[zoneString substringToIndex:3] intValue] * 3600;
timeInterval += [[zoneString substringFromIndex:3] intValue] * 60;
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:timeInterval]];
[formatter setDateFormat:#"yyyy'-'MM'-'dd' 'HH':'mm':'ss ZZZ"];
NSLog(#"%#", [formatter stringFromDate:dateFromString]);
Hopefully this helps you. If you've found a better answer already, let us know.
Original Answer
One thing about the NSDate is that the time returned is always in GMT. You can't change that. You will have to use an NSDateFormatter to print it right. Something like this,
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"PST"]];
[formatter setTimeStyle:NSDateFormatterFullStyle];
[formatter setDateStyle:NSDateFormatterFullStyle];
NSLog(#"%#", [formatter stringFromDate:[NSDate date]]);
So setting the timezone for the formatter that does dateForString: won't help. You will need to create a new one when you want them with a different timezone.
I'm working on an assignment that allows the user to display events that are happening 'today'. I have parsed the XML file and stored the contents into an array. The contents of the XML file consists of a title, description, date etc. The dates are in NSString format and I want to convert them into NSDates and compare them with today's date before displaying them in a UITableView.
I'm new to obj-c and I've searched online for help on NSDate, but I couldn't find what I need. Any links, advice or help on this is really appreciated. Thanks in advance (:
suppose dateString contains the date in string format
first get date from string:-
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/mm/yyyy"];
NSDate *dateprevious = [formatter dateFromString:dateString];
Now get today date
NSDate *date=[NSDate date];
[formatter setDateFormat:#"dd"];
NSString *dateOfGame =[formatter stringFromDate:dateprevious];
NSString *todaydate =[formatter stringFromDate:date];
[formatter release];
if([todaydate isEqualToString:dateknown])
{
NSLog(#"date matched");
}
Depending on the format of the string, you can use this:
+ (id)dateWithNaturalLanguageString:(NSString *)string
To compare two dates you will find here a lot of usefull answers :)
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy , hh:mm a"];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSDate *date = [[dateFormatter datefromString:date] retain];
[dateFormatter release];
You can use this one
Have a look at NSDateFormatter
It has a method called dateFromString
Like you could do the following:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/mm/yyyy"];
NSDate *date = [formatter dateFromString:#"5/5/2011"];
I have the following date: 2011-04-29T14:54:00-04:00
When it runs through the following code to convert it to a date, the date is null for some reason:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:localDate];
[dateFormatter release];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-mm-dd"];
NSLog(#"%#", [formatter stringFromDate:date]);
Any help would be appreciated
SOLVED:
Ok, I figured it out. For some reason, this method doesn't work.
NSDate *date = [dateFormatter dateFromString:localDate];
This works instead. Hope it helps someone!
NSError *error = nil;
NSDate *date = nil;
[dateFormatter getObjectValue:&date forString:localDate range:nil error:&error];
Ok, I figured it out. For some reason, this method doesn't work.
NSDate *date = [dateFormatter dateFromString:localDate];
This works instead. Hope it helps someone!
NSError *error = nil;
NSDate *date = nil;
[dateFormatter getObjectValue:&date forString:localDate range:nil error:&error];
Try setting the timezone:
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
and you might need to quote the -'s, :'s, and the Z in your format string (maybe not the -'s and :'s, but I think at least the Z):
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
Other than those, that is how my date formatter is configured and it works fine.
NSDateFormatter seems to only implement the canonical formats for timezones with 'Z' as described in UTS #35. With no leniency in parsing.
TimeZoneID+/-offset with hours and minutes separated by colon, eg. 2011-04-29T14:54:00GMT-04:00
+/-offset with no separation between hours and minutes, eg. 2011-04-29T14:54:00-0400
Try changing the format of the timezone in your localDate string.
I need to convert a date in this string format:
"2011-01-12T14:17:55.043Z"
to a number like 1294841716 (which is the number of seconds [not milliseconds] since Jan. 1st, 1970). Is there an easy way to do this parsing?
Update: Here is the code I've got so far:
NSString *dateString = #"2011-01-12T14:17:55.043Z";
NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
[inFormat setDateFormat:#"yyyy-MM-ddTHH:mm:ss.nnnZ"];
NSDate *parsed = [inFormat dateFromString:dateString];
long t = [parsed timeIntervalSinceReferenceDate];
But t comes back as 0 every time.
Use NSDateFormatter to get a NSDate then use - (NSTimeInterval)timeIntervalSince1970 to get the seconds since 1970.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [formatter dateFromString:#"2011-01-12T14:17:55.043Z"];
NSLog(#"Date: %#", date);
NSLog(#"1970: %f", [date timeIntervalSince1970]);
NSLog(#"sDate: %#", [formatter stringFromDate:date]);
[formatter release];
This might be a silly question, but I can't seem to find the answer on here or in the documentation.
I want to convert an NSString such as #"9/22/2010 3:45 PM" to an NSDate.
I know to use NSDateFormatter, but the problems are
The month could be one or two digits
Likewise, the date could be one or two digits
Hours could be one or two digits
What do I do about AM/PM?
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy h:mm a"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat release];
there is no problem in 2 digit day or 2 digit month.
This must help you.
You can parse an NSString into an NSDate using the NSDateFormatter class. See the documentation for more info:
Instances of NSDateFormatter create string representations of NSDate (and NSCalendarDate) objects, and convert textual representations of dates and times into NSDate objects.
I was having the same problem, not sure why NSDateFormatter isn't working for me (iOS5 - Xcode 4.3.2) but this worked out for me:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *dateString = #"05-06-2012";
NSDate *date;
NSError *error = nil;
if (![dateFormatter getObjectValue:&date forString:dateString range:nil error:&error]) {
NSLog(#"Date '%#' could not be parsed: %#", dateString, error);
}
[dateFormatter release];