I am pulling data from an RSS Feed. One of the keys in the feed is is a string representing the date and time the item was created.
I am trying to convert this string value to an NSDate. The string value is returned from the RSS feed as: 2009-11-18T22:08:00+00:00
I tried the following code to no avail:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyyMMdd HH:mm"];
NSDate *myDate = [df dateFromString: [[storedDates objectAtIndex:indexPath.row] objectForKey: #"UsersDate"]];
Ideally; on top of converting the value to a NSDate value, I would also like to format it using the localised date format on the handset.
Any pointers would be a great help.
Kind Regards
To retrieve the dateFormat:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *myDate = [df dateFromString: [[storedDates objectAtIndex:indexPath.row] objectForKey: #"UsersDate"]];
You can use the predefined formats if you would like to format time and date according to the user's locale:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
NSLog(#"%#",[dateFormatter stringFromDate:someDate]);
[dateFormatter release]; // don't forget to release the dateformatter
Check the documentation to see which formatter suits you best.
Actually date form in RSS feed may differ, so best way to parse it is following
[NSDate dateFromInternetDateTimeString:processedText formatHint:DateFormatHintRFC822];
This is not a standard function, library for it can be found here https://gist.github.com/953664
Related
I want to ask that how i get the device date and time format in my like device setting am/pm or 24 hrs what time and what format is set???
Like if i use this format
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"mm-dd-yyyy"];
insertCmd = [insertCmd stringByAppendingString:formatter setDateFormat:#"MM.dd.yyyy"];
it set the date format but i want the setting user set in device?
Try this,
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSLog(#"date = %#",[dateFormatter stringFromDate:date]);
This is for converting current date to string. It should be based on the currentLocale of the device. You dont have to set anything. However I am not sure what you meant by the code in your question.
This is what mentioned in documentation,
The format for these date and time styles is not exact because they
depend on the locale, user preference settings, and the operating
system version. Do not use these constants if you want an exact
format, for example if you are parsing an external data file which
contains date information in a fixed format. There are several
different “lengths” of the formats:
Or:
NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setFormat:#"mm-dd-yyyy"];
NSString* dateString = [formatter stringFromDate:date];
insertCmd = [insertCmd stringByAppendingString:dateString];
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'm new to iPhone development. I want to set default date to NSDate Object as string. I don't see any easy way or method...
I think there might be a method in NSCalender? If there's such a method, please tell me.
Thanks in advance.
I'm not totally clear on what you are asking, but to create an instance of an NSDate object with the current date, one calls:
NSDate * myDate = [NSDate date];
If you are saying that you have a c-string or NSString that needs to be parsed to initialize an NSDate object, that's another question.
I have some code posted here:
How get a datetime column in SQLite with Objective C
that shows how to create NSDates from NSStrings using NSDateFormatter.
If you want to create an NSDate from a string, you need to use an NSDateFormatter to do it. It's important to note that the formatter will use the current locale's time zone when constructing the date, unless you put a time-zone in as part of the format. For more information about constructing time zones, see NSTimeZone.
For example, to create a date using the ubiquitous format '2011-01-16 00:00' in UTC, you would do:
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm"];
// Only certain abbreviations are okay, like UTC. See docs for more info
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate* midnight_26_jan_2011_utc = [formatter dateFromString:#"2011-01-26 00:00"];
// this will display in your system locale
// (for me, it shows 2011-01-25 19:00 +0500 because I'm America/New_York time)
NSLog(#"date: %#", midnight_26_jan_2011_utc);
[formatter release];
Edit: Added time to format string.
You will need to look at the NSDate and NSDateFormatter classes. Here's a simple example of how to use them:
NSString* defaultDateString = [NSString stringWithFormat:#"2011-01-22 15:30:00"];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate* defaultDate = [dateFormatter dateFromString:defaultDateString];
[dateFormatter release];
and if you wanted to get the string from a date you can just use:
NSString* defaultDateString = [dateFormatter stringFromDate:defaultDate];
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
[DateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"]; //here,you can set the date format as you need
NSDate *now = [[[NSDate alloc] init]autorelease];
NSString *theDate = [DateFormatter stringFromDate:now];
Now, you can use the string the date. :)
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init]autorelease];
[dateFormatter setDateFormat:#"yyyy-mm-dd"];
NSDate *yourDate = [dateFormatter dateFromString:#"2011-01-26"];
I have this string...
2010-08-24T16:00:00-05:00
and I'd like to extract the time portion from it (i.e. 16:00) and convert it to its 12-hour equivalent (i.e. 04:00 pm). I'm trying to use NSDateFormatter to accomplish this, but it's not working...
NSDateFormatter* dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:#"hh:mm a"];
NSDate *date1 = [dateformatter dateFromString:[listOfTimes objectAtIndex:0]];
[dateformatter release];
Can I use NSDateFormatter with this date format? If not, how can I extract the time and convert it to its 12-hour time equivalent?
Thanks!
The problem has to do with parsing the colon. I asked the same question and the solution is here: How to parse a date string into an NSDate object in iOS?
I think you should be able to do something like the following.
// create the date formatter object
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate* date = [formatter dateFromString:dateString];
// set up the new date format
[formatter setDateFormat:#"hh:mm:ss"];
NSString *twelveHourTime = [formatter stringFromDate:date];
[formatter release];
Update: Fixed the dateFormatter string format. I had the line below, but the Z seems to be unnecessary. Timezones always screw me up. :-/
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
This answer needs to be updated. As of iOS 10 the system provided NSISO8601DateFormatter is available for this particular format.
hello all I have the date in string format "2009-07-06T02:05:11.000+10:00" which i have taken from xml.. Now i need to convert that into date format and use it.How could I do this
Thanks all
See the doc for NSDateFormatter
Roughly as follows:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"DATE FORMAT HERE !!!"];
NSDate *date = [dateFormatter dateFromString: #"DATE TEXT STRING"];