Unable to parse date from string using NSDateFormatter [closed] - iphone

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am stuck for a while parsing date in string using NSDateFormatter
Date in string is in below format
1/1/2011 12:00:00 AM
Below is the code I use to parse
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss"];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSDate *date = [[NSDate alloc]init];
date = [dateFormatter dateFromString:#"1/1/2011 12:00:00 AM"];
This code works with date #"12/31/2011" and format #"MM/dd/yyyy"
Please help

use my bellow custom method and parse that date into another format...
-(NSString *)changeDateFormat:(NSString*)stringDate dateFormat:(NSString*)dateFormat getwithFormat:(NSString *)getwithFormat{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:dateFormat];
NSDate *date = [dateFormatter dateFromString:stringDate];
[dateFormatter setDateFormat:getwithFormat];
NSString *convertedString = [dateFormatter stringFromDate:date];
//NSLog(#"Converted String : %#",convertedString);
return convertedString;
}
use this method like bellow..
NSString *strSDate = [self changeDateFormat:#"1/1/2011 12:00:00 AM" dateFormat:#"dd/MM/yyyy hh:mm:ss a" getwithFormat:#"dd/MM/yyyy"];
NSLog(#"Converted String : %#",strSDate);
Set any Format which you want in getwithFormat parameter and set format in dateFormat parameter which you have with your Date string...
OUTPUT IS => Converted String : 01/01/2011

Try this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
//First format date from string
NSDate *date = [dateFormatter dateFromString:#"01/01/2011 12:00:00 AM"];
//Set date formatter to specific format
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
//Log the value
NSLog(#"%#",[dateFormatter stringFromDate:date]);

Related

Convert string to date(0512 should be converted to May 2012) [duplicate]

This question already has answers here:
Convert string to date in my iPhone app
(4 answers)
Closed 10 years ago.
How to convert a string to date? I know we can use dateFormatter to do this but I am stuck in between. I have format 0512 which should be converted to May 2012.
Try this,
NSString *finalDate = #"0512";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMyy"];
NSDate *date = [dateFormatter dateFromString:finalDate];
[dateFormatter setDateFormat:#"MMM yyyy"];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
Try this
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"MMyy"];
NSDate *dateObj= [dateFormatter dateFromString:#"0512"];
NSLog(#"%#",dateObj);
[dateFormatter setDateFormat:#"EEEE MMMM d, YYYY"];
NSLog(#"%#",[dateFormatter stringFromDate:dateObj]);
Try this code may be helped you..
NSString *dateStr = #"0512";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMyy"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"MMMM YYYY"];
dateStr = [dateFormat stringFromDate:date];
[dateFormat release];
NSLog(#"%#",dateStr);
What is so difficult about it?
1. Take out first 2 characters of string
2. Put if else/ convert string to int and use switch to make up month
3. Take the next 2 characters, add them to 2000 and you get year
Now user iOS functions to achieve this :)

NSDateFormatter not working while formatting [duplicate]

This question already has an answer here:
dateformatter problem?
(1 answer)
Closed 8 years ago.
I have a time string, which is as shown below:
2/5/2013 12:00:00 AM
and i want to change this in to MM/dd/yyyy formate.
i used NSDateFormatter, but i am getting a null value. how can i change this??
check your code, must be doing something wrong, this is how you receive the right format..
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yy"];
NSString *dateString = [dateFormatter stringFromDate: [NSDate date]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
NSDate *date = [formatter dateFromString:str];
Read this document for date formatters.

NSDateFormatter in iPhone app? [duplicate]

This question already has answers here:
Formatting NSDate into particular styles for both year, month, day, and hour, minute, seconds
(8 answers)
Closed 9 years ago.
Am receiving date in this format (2012-07-13 09:22:22 +0000). I want to convert this date format to 13-Jul-2012 09:22. And also if the date is today means i want to change Today 09:22 and Yesterday 09:22. Can anyone please help to do this?
EDIT:
I have added some code what i have tried.
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"dd-mm-yyyy hh:mm"];
[dateFormatter1 setFormatterBehavior:NSDateFormatterBehaviorDefault];
[dateFormatter1 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [dateFormatter1 dateFromString:dateStr];// Change Date Format
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDoesRelativeDateFormatting:YES];
NSString *dateString1 = [formatter stringFromDate:date]; // Getting Today or Tomorrow
[formatter release];
NSLog(#"DateString1 : %#", dateString1);
First problem I see is that the date pattern your supplying to the Formatter does not match the date string you claim your getting. You might want to try this:
[dateFormatter1 setDateFormat:#"yyyy-mm-dd hh:mm:ss Z"];
The format your using in the code you supplied has year, month, day reversed and your not converting second.
You can try this:
NSDate *date = [NSDate date]; // date in this format 2012-07-14 17:32:09 +0000
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMM-yyyy hh:mm"];
//[dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; // Optional
NSString *dateStr = [dateFormatter stringFromDate:date];
And next compare your 'date' with current date using NSDate methods for eg. timeIntervalSinceReferenceDate and then based on the result display today/tomorrow.

change date format in iPhone

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....

NSDate from NSString gives null result

I am using following code to generate NSDate -> NSString
+(NSString *)getCurrentTime
{
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy hh:MM:SS a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString* str =[dateFormatter stringFromDate:now];
[dateFormatter release];
NSLog(#"%#",str);
return str;
}
everything is fine in above code. I am using above code to store string in Database. Now while retrieving that string gives me NULL. Following is my code to retrieve date in specific format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:MM:SS a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dt =[dateFormatter dateFromString:crdInfo.swipeTime];
NSLog(#"Date : %#",dt);
[dateFormatter release];
How should I retrieve or store with particular format?? My crdInfo.swipeTime is retrieving String propertly...
First off, why not just store the NSDate object or epoch timestamp? This will give you much more flexibility in the future.
Now to your problem, I suspect it is due to your configuration of the NSDateFormatter, you're saving it in one format and trying to convert it to a date using a different format. Make the formats the same and try again. If you want to display it differently than it is stored you're likely going to need to convert it to and NSDate using the stored format and then again use another date formatter to get it in the format you want it as a string.
As Narayana suggested you need to retrieve the date with same format as you have stored. Retrieve it as below : -
NSDateFormatter *reDateFormatter = [[NSDateFormatter alloc] init];
[reDateFormatter setDateFormat:#"dd-MM-yyyy hh:MM:SS a"];
[reDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dt = [reDateFormatter dateFromString:str];
NSLog(#"The Date : %#",dt);
[reDateFormatter setDateFormat:#"hh:MM:SS a"];
NSString *currentTime = [reDateFormatter stringFromDate:dt];
NSLog(#"%#",currentTime);
Hope it helps you.
Try to format it to dd-MM-yyyy hh:mm:ss a.
You wrote dd-MM-yyyy hh:MM:SS a where MM in hh:MM:SS gives month which is unrecognized in this format and there is no point writing upercase SS for seconds
Hope you understand it.