Not able to convert date from string - iphone

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

Related

About JSON Date Conversion error

I get JSON Parse Date, I want to use NSDateFormatter,but not success.
1.JSON Get console Log
IncidentReceiveTime = "/Date(1353914100000+0800)/";
2.my code
NSString *dateStr =[NSString stringWithFormat:#"%#",[[[DateSortArry objectAtIndex:0] objectForKey:#"IncidentReceiveTime"]stringByReplacingOccurrencesOfString:#"/" withString:#""]];
NSLog(#"dateStr:%#",dateStr);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:#"yyyy-MM-ddHH"];
NSDate *date = [dateFormatter dateFromString:dateStr];
NSLog(#"date:%#",date);
3.NSDateFormatter console log
dateStr:Date(1361943694000+0800)
date:(null)
Refer to my answer in :
iPhone:How to convert Regular Expression into Date Format?
Here I am posting this code from my answer in the above link as if in case in future the above link goes down then this answer doesn't become useles:
- (NSDate*) getDateFromJSON:(NSString *)dateString
{
// Expect date in this format "/Date(1268123281843)/"
int startPos = [dateString rangeOfString:#"("].location+1;
int endPos = [dateString rangeOfString:#")"].location;
NSRange range = NSMakeRange(startPos,endPos-startPos);
unsigned long long milliseconds = [[dateString substringWithRange:range] longLongValue];
NSLog(#"%llu",milliseconds);
NSTimeInterval interval = milliseconds/1000;
return [NSDate dateWithTimeIntervalSince1970:interval];
}
Once you get NSDate object from above method:
NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];
[dateForm setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSString *dateStr = [dateForm stringFromDate:<YourNSDateObject>];
[dateForm setDateFormat:#"<Your Desired Date Format>"];
NSDate *desireddate = [dateForm dateFromString:dateStr];
Hope this helps.

Getting date in specified format [duplicate]

This question already has answers here:
Passing a formatted NSDate
(3 answers)
Closed 8 years ago.
I am getting current date in format "2013-02-10 09:26:52 +0000" by using [NSDate date].
I want date in format like "Feb 10".
Can anyone suggest correct dateFormat?
Use NSDateFormatter class:
NSDateFormatter * myFormatter = [[NSDateFormatter alloc] init];
[myFormatter setDateFormat:#"ddMMyy" options:0 locale:[NSLocale currentLocale]];
NSString * formattedDate = [myFormatter stringFromDate:[NSDate date]];
Here you have info to choose the output format for setDateFormat: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
Hi set the date formatter like this
[dateFormatter setDateFormat:#"MMM d, yyyy hh:mm:ss"];
hope it will work
It is working in my case :
NSDateFormatter* dateFormatterBegin = [[NSDateFormatter alloc] init];
[dateFormatterBegin setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"];
NSString *dateStringBegin = #"2013-02-10 09:26:52 +0000";
NSDate* datebegin = [dateFormatterBegin dateFromString:[dateStringBegin stringByReplacingOccurrencesOfString:#":" withString:#"" options:0 range:NSMakeRange(dateStringBegin.length-3, 1)]];
[dateFormatterBegin setDateFormat:#"MMM-dd"];
NSString *beginDate = [[NSString alloc] initWithFormat:#"%s%#","Start Date : ",[dateFormatterBegin stringFromDate:datebegin]];
NSLog(#"Your date is : %#",beginDate);
[dateFormatterBegin release];
[beginDate release];
NSString *string = #"2013-02-10 09:26:52 +0000"; // The date in a string
//Convert the string to date with the same format
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"]; // Set format
NSDate *date = [dateFormat dateFromString:string];
//Create the new date format using NSDateFormatter
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:#"MMM dd"]; // Set format
//Convert the date using dateFormat1
NSDate *formatDate= [dateFormat1 dateFromString:date]; // date with MMM dd

Can't convert from NSString to NSDate

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"];

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 get date from a string (iPhone)?

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?