Problem with parsing NSString to NSDate - iphone

I have XML with this value:
<LastModifiedOnDate>2011-04-02T00:00:00</LastModifiedOnDate>
I am trying to parse this on iPhone from NSString to NSDate but have no luck.
NSDateFormatter *formate = [[[NSDateFormatter alloc] init] autorelease];
[formate setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss"];
//NSString *strConcat = #"2010-09-24 17:30:00";
NSDate *date = [formate dateFromString:string];

You don't need all those apostrophes in your format string. The only ones you need are around the T.
NSDateFormatter *formate = [[[NSDateFormatter alloc] init] autorelease];
[formate setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
// I recommend setting a timezone because it will use the system timezone and that
// can cause confusion with your data.
// [formate setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString *strConcat = #"2010-09-24T17:30:00";
NSDate *date = [formate dateFromString:strConcat];
NSLog(#"Date: %#", date); // remember NSDate default to current timezone when logging to console
If you string have fraction of seconds, then add SSS to your string format: [formate setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
For additional information on date formatting check out Unicode Technical Standard #35.

Replace your's SetDataFormat function with below and let me know for the result ...
[formate setDateFormat:#"yyyy-MM-ddTHH:mm:ss"];

NSString *string=#"2011-04-05T12:43:56.537";
NSDateFormatter *formate = [[[NSDateFormatter alloc] init] autorelease];
[formate setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS"];
NSDate *date = [formate dateFromString:string];
its working..

Related

Convert NSString date to NSDate or a specific string

Could you please help me with such converting
I have NSString date like #"2/8/2012 7:21:09 PM"
And I need to have such string in output:
"at 7:21 PM, february 8"
I've tried to use dateFormatter with different date patterns but it always return me null..I really don't understandd where I'm doing wrong :(
NSString *dateString = newsItem.Date;//Which is equal to #"2/8/2012 7:21:09 PM"
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"mm/dd/yyyy hh:mm:ss"];//I'm sure that this patternt is wrong but have no idea how to write the right one
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSLog(#"date:%#", dateFromString);
You'll need to use two different date formatters. First one to convert the string in to a date, the second one to output the date as a string with the specified format.
NSString* dateString = #"2/8/2012 7:21:09 PM";
NSDateFormatter* firstDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[firstDateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate* date = [firstDateFormatter dateFromString:dateString];
NSLog(#"Date = %#",date);
NSDateFormatter* secondDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[secondDateFormatter setDateFormat:#"h:mm a, MMMM d"];
NSString* secondDateString = [NSString stringWithFormat:#"at %#",[secondDateFormatter stringFromDate:date]];
NSLog(#"%#",secondDateString);
The trick is the date format strings. They use a format called the unicode date format, the specification can be found here.

How to remove padding of 0 while using NSdateFormatter?

Like if I am using the code.
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"dd.MM.yyyy"];
NSDate *today = [NSDate date];
NSString *log_date = [df stringFromDate:today];
//Generate Log Current Time.
[df setDateFormat:#"hh:mma"];
NSString *log_currenttime = [[df stringFromDate:today] lowercaseString];
NSLog(#"Log Date %#",log_date);
NSLog(#"Log Current Time %#",log_currenttime);
My log shows me :
Log Date 19.12.2011
Log Current Time 03:18pm
At least I can remove the Time Padding.
Change your format from hh:mma to h:mma
//Generate Log Current Time.
[df setDateFormat:#"h:mma"]; // Changed from hh:mma to h:mma
NSString *log_currenttime=[[df stringFromDate:today] lowercaseString];
NSLog(#"Log Current Time %#",log_currenttime);
This will output
Log Current Time 3:18pm
As Eric wrote, use h:mm will fail for example on czech calendar.
Only way to do that is use system formatter.
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"dd.MM.yyyy"];
[df setTimeStyle:NSDateFormatterNoStyle];
NSDate *today = [NSDate date];
NSString *log_date = [df stringFromDate:today];
//Generate Log Current Time.
[df setTimeStyle:NSDateFormatterShortStyle];
[df setDateStyle:NSDateFormatterNoStyle];
NSString *log_currenttime = [[df stringFromDate:today] lowercaseString];
NSLog(#"Log Date %#",log_date);
NSLog(#"Log Current Time %#",log_currenttime);
The problem with Aadhira's answer is that if the user then switches to metric the #h:mma formatting will override it and make 23:00 look like 11:00pm.
The padding comes from the users settings in Settings-->General-->International-->Region Format. For example if it is set to UK, you will get padding. If it is set to US you won't. You may want to respect this decision in your user interface.

UIImage from a URL that change by date

my question is simple, i want to load a UIImage from an URL, but this URL change programmatically by date.
Example Today the url is http://www.abc.com/2011-10-13/alfa.jpg
tomorrow is http://www.abc.com/2011-10-14/alfa.jpg
the only thing that change is the date part, how can i figure my app load that "alfa.jpg" at current date everytime i start it?
Thanks!
You should use [NSDate date] which returns the current date and time according to the device time (assuming your device time doesn't differ from the actual by more than a day). Then you should format the date according to your url. Something like-
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSString* dateString = [formatter stringFromDate:[NSDate date]];
[formatter release];
Use an NSDateFormatter to generate the part of the string that varies with time.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString * datePart = [dateFormatter stringFromDate:[NSDate date]];
NSString * theURLString = [NSString stringWithFormat:#"http://www.abc.com/%#/alfa.jpg", datePart];
You will have to maintain a mechanism to check whether if the image for the day has been downloaded to avoid downloading it again.
Create a template of the url you are using nstring and a DateFormatter object...
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"yyyy-MM-dd"];
NSString *todayStr = [df stringFromDate:[NSDate date]];
NSString *todayUrl = [NSString stringWithFormat:#"http://www.abc.com/%#/alfa.jpg", todayStr];
todayUrl has the url for today!
Its an easy task just format your URL string using current date like,
NSString *URLString = [[NSString alloc] initWithFormat:#"http://www.abc.com/%#/alfa.jpg", currentDate];
Then use this string for URL request.

change format of system date

i am trying to change my date from system.I am using NSdate to pick date from system and change the format.Can anyone tell me how to change the format of my system date?I am using nsdateformatter to change the date format.I want the dateformat to strip off all the timestamps.
Have you tried setting the time style?
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
Here is a full solution with GMT and NSString conversion too:
NSDate *time = [[NSDate alloc] init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss MM-dd-yyyy"]; //you can play with this structure
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT-05:00"]];
NSString *stringFromDate = [formatter stringFromDate:time];
NSString *strTime = [NSString stringWithFormat:#"%#", stringFromDate];

Time zone added to hour after using nsdateformatter

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.