I am getting the rfc3399 formatted date string through google calender web services. I want to convert this string into NSDate object so that I can add an event about this to the local calendar. I found the below method in Apple's documentation regarding NSDateFormatter , but its not working
Date String - 2012-05-23T18:30:00.000-05:00
- (NSString *)userVisibleDateTimeStringForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString {
/*
Returns a user-visible date time string that corresponds to the specified
RFC 3339 date time string. Note that this does not handle all possible
RFC 3339 date time strings, just one of the most common styles.
*/
NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
[rfc3339DateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// Convert the RFC 3339 date time string to an NSDate.
NSDate *date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];
NSString *userVisibleDateTimeString;
if (date != nil) {
// Convert the date object to a user-visible date string.
NSDateFormatter *userVisibleDateFormatter = [[NSDateFormatter alloc] init];
assert(userVisibleDateFormatter != nil);
[userVisibleDateFormatter setDateStyle:NSDateFormatterShortStyle];
[userVisibleDateFormatter setTimeStyle:NSDateFormatterShortStyle];
userVisibleDateTimeString = [userVisibleDateFormatter stringFromDate:date];
}
return userVisibleDateTimeString;
}
Please help me , I have tried changing the date format quite a lot times but no success.
try with this method..
-(NSDate *)convertStringToDate:(NSString *) date {
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSDate *nowDate = [[[NSDate alloc] init] autorelease];
[formatter setDateFormat:#"YYYY-MM-DD'T'HH:mm:ssZ"];// set format here which format in string date
/// also try this format #"yyyy-MM-dd'T'HH:mm:ss.fffK"
date = [date stringByReplacingOccurrencesOfString:#"+0000" withString:#""];
nowDate = [formatter dateFromString:date];
// NSLog(#"date============================>>>>>>>>>>>>>>> : %#", nowDate);
return nowDate;
}
call this method like bellow..
NSDate *date = [self convertStringToDate:rfc3339DateTimeString];
also for try with some rfc Date formate with fffK for this format see this bellow. link..
how-do-i-parse-and-convert-datetimes-to-the-rfc-3339-date-time-format
i hope this answer is helpful
The issue is the date contains (-05:00 )':' in the timezone value ... you have to remove it from the string then the above method will work fine......The new date string should look like below
2012-05-23T18:30:00.000-0500 - This should be the right format .
Related
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.
I have the following NSString:
NSString * dateString = #"2012-01-24T14:59:01Z";
I want to create an NSDate from that string. I looked up the NSDate Class Reference and thought of using dateWithNaturalLanguageString::
Creates and returns an NSDate object set to the date and time
specified by a given string.
+ (id)dateWithNaturalLanguageString:(NSString *)string
Parameters
string
A string that contains a colloquial specification of a date,
such as “last Tuesday at dinner,” “3pm December 31, 2001,” “12/31/01,”
or “31/12/01.”
Return Value
A new NSDate object set to the current
date and time specified by string.
However, when I'm trying to use it like this:
NSDate * date = [NSDate dateWithNaturalLanguageString:dateString];
I'm getting the following error:
No known class method for selector 'dateWithNaturalLanguageString:'
NSDateFormatter class will help you with this problem. And there are many questions about this already, for example, here is the first one: Convert NSString->NSDate?
http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
Try using the constructor dateWithString constructor or (if that gives you a similar error), try using an NSDateFormatter as described here.
The dateWithNaturalLanguageString: class method is only implemented on Mac OS X, not on iOS, that is why you're getting the error.
To achieve what you're looking for, you need the NSDateFormatter class. The class is quite heavy, so you'll want to read the documentation first to understand how best to use it.
Found exactly what I was looking for here. It's an RFC 3339 date-time.
- (NSString *)userVisibleDateTimeStringForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString
// Returns a user-visible date time string that corresponds to the
// specified RFC 3339 date time string. Note that this does not handle
// all possible RFC 3339 date time strings, just one of the most common
// styles.
{
NSString * userVisibleDateTimeString;
NSDateFormatter * rfc3339DateFormatter;
NSLocale * enUSPOSIXLocale;
NSDate * date;
NSDateFormatter * userVisibleDateFormatter;
userVisibleDateTimeString = nil;
// Convert the RFC 3339 date time string to an NSDate.
rfc3339DateFormatter = [[[NSDateFormatter alloc] init] autorelease];
enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease];
[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
[rfc3339DateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];
if (date != nil) {
// Convert the NSDate to a user-visible date string.
userVisibleDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
assert(userVisibleDateFormatter != nil);
[userVisibleDateFormatter setDateStyle:NSDateFormatterShortStyle];
[userVisibleDateFormatter setTimeStyle:NSDateFormatterShortStyle];
userVisibleDateTimeString = [userVisibleDateFormatter stringFromDate:date];
}
return userVisibleDateTimeString;
}
Ha ii,I know how to formate a date in iphone,but i have to set the date as given by the image below.My code is
NSDate* date = [NSDate date];
//Create the dateformatter object
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//Set the required date format
[formatter setDateFormat:#"yyyy:mm:dd"];
//Get the string date
NSString* str = [formatter stringFromDate:date];
//Display on the console
// NSLog(str);
//Set in the lable
[_lbldateLabel setText:str];
How to modify my above code to get the current date as given by the image?
Thanks in advance.
try this
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//Set the required date format
[formatter setDateFormat:#"MMM dd/yy"];
//Get the string date
NSString* str = [formatter stringFromDate:date];
I need to convert this string in to the date:
02-09-2011 20:54:18
I am trying the `dateFromString` method but every time it is returning (null), what NSDateFormatter should I use?, I have tried
`[formatter setDateFormat:#"yyyy-MM-dd"]` and `[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"]`.
Here is my code:
NSString *finalDate = #"02-09-2011 20:54:18";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSDate *dateString = [formatter dateFromString:finalDate];
NSLog(#"%#",dateString);
Thanks.
You can convert a NSString containing a date to a NSDate using the NSDateFormatter with the following code:
// Your date as a string
NSString *finalDate = #"02-09-2011 20:54:18";
// Prepare an NSDateFormatter to convert to and from the string representation
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// ...using a date format corresponding to your date
[dateFormatter setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
// Parse the string representation of the date
NSDate *date = [dateFormatter dateFromString:finalDate];
// Write the date back out using the same format
NSLog(#"Month %#",[dateFormatter stringFromDate:date]);
Surely it's easier to search than it is to ask?
How do I convert from NSString to NSDate?
I have the date function.And I have to compare the previous date with current data in iphone
For current date I have code:-
NSDate* date = [NSDate date];
//Create the dateformatter object
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//Set the required date format
[formatter setDateFormat:#"yyyy-MM-dd"];
//Get the string date
NSString* str = [formatter stringFromDate:date];
//Display on the console
NSLog(str);
//Set in the lable
[dateLabel setText:str];
But I want to implement the compare the current date & previous date in iphone
Thanks in advance
You can compare dates using NSDate's comparison methods:
- (NSDate *)earlierDate:(NSDate *)anotherDate;
- (NSDate *)laterDate:(NSDate *)anotherDate;
- (NSComparisonResult)compare:(NSDate *)other;
- (BOOL)isEqualToDate:(NSDate *)otherDate;
Check NSDate class reference to know more..