I can't convert my NSString to NSDate. here's the code:
+ (NSDate *) stringToNSDate: (NSString *) dateString{
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"dateString: %# datefromstring: %#", dateString, dateFromString);
return dateFromString;
}
//dateFromString is nil.
What should I do? Thanks!
btw, dateString will always contain a string-ed date in this format: "2012/02/10 08:01:25 +0000"
From the information you've given, the problem appears to be that your dateString separates the date components with slashes (\), but your date formatter is expecting dashes (-).
Edit
To answer your other question, you can use stringByReplacingOccurrencesOfString:withString:.
For example:
NSString *date1 = #"2012/12/10 ...";
NSString *date2 = [date1 stringByReplacingOccurrencesOfString:#"/" withString:#"-"];
Note that this will replace every / with -, so just be careful about timezones etc.
Try this instead:
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy/MM/dd HH:mm:ss ZZZ"];
Related
I am trying this since last two hours and finaly left it out for you guys :P
I need to convert this String Mon, 14 May 2012, 12:00:55 +0200
into Date dd/mm/yyyy hh:ss format..
Any kind of help towards the goal will be really appreciated.
What I have tried
I have tried using NSDateFormatter but I am unable to figure out the exact format of the above date.
This [dateFormatter setDateFormat:#"EEEddMM,yyyy HH:mm:ss"]; is how I have tried and many other formats too
Eg:
NSString *finalDate = #"Tue, 29 May 2012, 14:24:56 +0200";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEEddMM,yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:finalDate];
Here the date alwasys comes as nil
The most important part of date formatting is often forgotten, tell the NSDateFormatter the input language:
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"EEE, dd MMMM yyyy, HH:mm:ss Z"];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"EN"];
NSDate *date = [dateFormatter dateFromString:#"Mon, 14 May 2012, 12:00:55 +0200"];
NSLog(#"date: %#", date);
I've checked the output: date: 2012-05-14 10:00:55 +0000
Be aware that the HH in the date formatter is for 24hr.
Now I would suggest not to use a fixed output scheme, but use one of the NSDateFormatterStyle:
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"date: %#", dateString);
Which in american english will output: 5/14/12 12:00 PM
This code is valid for ARC if you are not using ARC add autorelease to the NSLocale and release the NSDateFormatter after you are done with it.
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"dd/mm/yyyy hh:ss"];
NSString *formatterDate = [inputFormatter stringFromDate:inDate];
[inputFormatter release];
get inforamtion about all Date Formate
my Blog Link is http://parasjoshi3.blogspot.in/2012/01/date-formate-info-for-iphone-sdk.html
and also small function is bellow.....
-(NSString *) dateInFormat:(NSString*) stringFormat {
char buffer[80];
const char *format = [stringFormat UTF8String];
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer, 80, format, timeinfo);
return [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
}
////////Like.......
NSString *mydate = [self dateInFormat:#"%Y%m%d-%H%M%S"];
hope,this help you....
I am having an issue with this message. It doesn't seem to be able to convert to NSDate with my dateString
//dateString = #"2012-03-24 00:00:00 +0000"
+ (NSDate *)dateFromString:(NSString *)dateString {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *result = [dateFormatter dateFromString:dateString]; //Invalid CFStringRef
[dateFormatter release];
return result;
}
If the time zone is specified with an offset +0000 in
NSString *dateString = #"2012-03-24 00:00:00 +0000";
you have to use:
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZ"];
Notice the double 'Z' and the removal of 'T'.
Check here for more information.
Your date string doesn't have a Z on the end (which the format expects) and it does have a time zone offset (which the format does not expect).
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 am doing an application where I am using the following link http://iosdevelopertips.com/cocoa/date-formatters-examples-take-3.html to convert the date from string.
I am using below code to same by following above tutorial link. when I NSlog PresentDateTime it gives 20120111 13:03:49.263+05:30.
so I am taking this format and want to convert into the format of "EEEE MMMM d, YYYY h:mm a, zzz".
To do so when I assign the same to the variable
NSString *today = presentDateTime;
when I NSLog Datestring I am getting result NULL can I know where I am going wrong? please help me to solve this issue by suggesting what changes to be done
//code
-(NSString *)DateFormat:(NSString *) presentDateTime
{
presentDateTime = [presentDateTime stringByReplacingOccurrencesOfString:#"T" withString:#" "];
presentDateTime = [presentDateTime stringByReplacingOccurrencesOfString:#" +" withString:#"+"];
presentDateTime = [presentDateTime stringByReplacingOccurrencesOfString:#"-" withString:#""];
NSLog(#"Present date::::%#",presentDateTime);
NSString *today = presentDateTime;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMdd HH:mm:ss:SSSZZZ"];
NSDate *date = [dateFormat dateFromString:today];
[dateFormat setDateFormat:#"EEEE MMMM d, YYYY h:mm a, zzz"];
NSString *dateString = [dateFormat stringFromDate:date];
[dateFormat release];
NSLog(#"dateString:::Date>>>>:%#::::\n%#",dateString, presentDateTime);
return dateString;
Your date format pattern for parsing the date is wrong/incomplete. For the string 20120111 13:03:49.263+05:30 the format would be YYYYMMdd HH:mm:ss.SSSZZZ. See this table for the format string patterns.
Edit: After playing around with the code, it turns out that the NSDateParser chokes on the timezone +05:30. It expects +0530, without the colon. Here's how to remove it:
NSString *today = #"20120111 13:03:49.263+05:30";
NSRange colon = [today rangeOfString:#":" options:NSBackwardsSearch];
if (colon.location != NSNotFound) {
today = [today stringByReplacingCharactersInRange:colon withString:#""];
}
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYYMMdd HH:mm:ss.SSSZZZ"];
NSDate *date = [dateFormat dateFromString:today];
NSLog(#"%#", date);
When you parse your original date string you need to specify the entire format, like this:
[dateFormat setDateFormat:#"yyyyMMdd HH:mm:ss.SSSZZZ"]
Compare your code with the following
NSString *dateString = #"6:30 18 Oct";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm dd/MMM"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
Its just sample to follow. and figure out what is going wrong by printing it in nslog.
Your date variable is always nil because your date format (yyyyMMdd) is incomplete as your input string today contains extra information (20120111 13:03:49.263+05:30). So you can just crop unnecessary characters:
if ([presentDateTime count] < 8) return nil;
NSString *today = [presentDateTime substringToIndex:8];
Or you should specify full date format as mentioned by #jonkroll
Getting a NSString as follows:
NSString *lastSyncDate = [[items objectAtIndex:0] objectForKey:#"LastSyncDate"];
This value is: 1/1/2010 12:00:00 AM
I am using the following to convert to a date:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeStyle:NSDateFormatterFullStyle];
[df setDateFormat:#"MMMM d, y HH:mm:ss ZZZ"];
NSDate *mySyncDate = [df dateFromString:lastSyncDate];
This gives me a null value?
Your date format doesn't match the format of the string.
Try
[df setDateFormat:#"M/d/y hh:mm:ss a"];
or
[df setDateFormat:#"M/d/y h:mm:ss a"]; // if the hours aren't zero-padded
The format strings are a Unicode standard described at http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
I agree with Seamus Campbell. However, be aware that setting the time style on the formatter (NSDateFormatter) will have no effect on the result returned by dateFromString.