How to format data from string variable - iphone

I have a NSString that keeps date:
1900-01-01T11:00:00
I need to format it, so I have a formatter:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss a"];
How to format this string now?
NSString* formatedDateString = [df ???];

Create a date formatter, that returns a date object for for the given string, create a second date formatter, that returns a string in the desired format from that date.
NSDateformatter *inputFormatter = [[NSDateformatter alloc] init];
[inputFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
NSDate *date = [inputFormatter dateFromString:dateString];
NSDateformatter *outputFormatter = [[NSDateformatter alloc] init];
[outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[outputFormatter setDateFormat:#"dd/MM/yyyy hh:mm:ss a"];
NSString *outputString = [outputFormatter stringFromDate:date];
But you could also let the output date formatter decide the style in respect to the locale:
NSDateformatter *outputFormatter = [[NSDateformatter alloc] init];
[outputFormatter setDateStyle:NSDateFormatterShortStyle];
[outputFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *outputString = [outputFormatter stringFromDate:date];
for a american locale, you should now cat the desired format.
if you want to enforce format "dd/MM/yyyy hh:mm:ss a" for the user, you should also set the locale of the output date formatter.
a complete command line example.
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSString *dateString1 = #"2012-12-31T11:00:00";
NSString *dateString2 = #"2012-12-31T14:00:00";
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[inputFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[outputFormatter setDateFormat:#"dd/MM/yyyy hh:mm:ss a"];
NSDate *date1 = [inputFormatter dateFromString:dateString1];
NSDate *date2 = [inputFormatter dateFromString:dateString2];
NSString *outputString1 = [outputFormatter stringFromDate:date1];
NSString *outputString2 = [outputFormatter stringFromDate:date2];
NSLog(#"%#", outputString1);
NSLog(#"%#", outputString2);
}
return 0;
}
If you dont set the locale like [outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]]; it will lead do a ugly mix of formats with user's sefault locale, if it not happend to be 'en_US'
ie in German: 31/12/2012 02:00:00 nachm. This is a format not used in german.
To support user's language and locale you should instead use the different styles.
The q&a rmaddy provided indicates, that there are rare cases, where the parse format gets rewritten. To avoid this, set the input formatted's format to a certain locale, like [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];

You need to create two date formatter, one for parsing, one for formatting; Your parsing format is "yyyy-MM-ddThh:mm:ss" and your output format is "dd/MM/yyyy hh:mm:ss a". So, something like this :
NSString * targetString = #"1900-01-01'T'11:00:00";
NSDateFormatter *parseFormat = [[NSDateFormatter alloc] init];
[parseFormat setDateFormat:#"yyyy-MM-ddThh:mm:ss"];
NSDate * date = [parseFormat dateFromString:targetString];
NSDateFormatter * outputFormat = [[NSDateFormatter alloc] init];
[outputFormat setDateFormat:#"dd/MM/yyyy hh:mm:ss a";
NSString * outputString = [parseFormat stringFromDate:date];

example for getting current date :
-(void) getDate
{
NSString *theDate;
NSString *theTime;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"HH:mm:ss"];
NSDate *now = [[NSDate alloc] init];
theDate = [dateFormat stringFromDate:now];
theTime = [timeFormat stringFromDate:now];
//2012-09-21 02:00:00
self.deviceCurrentTime=[NSString stringWithFormat:#"%# %#",theDate,theTime];
}

Related

String to Date Conversion in iPhone

I have refer previous examples from here. But still problem in null value.
String Value is :==> strDate = "09-05-2012 00:00:00"
I want output as :==> May 09, 2012
My Code:
NSString *strDateFormat;//[strDate substringWithRange:NSMakeRange(0, 10)];
strDateFormat = [NSString stringWithFormat:#"%#", strDate];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
NSDate *disDate = [df dateFromString:strDateFormat];
[df setDateFormat:#"MMM dd, yyyy"];
strDateFormat = [df stringFromDate:disDate];
lblDate.text = [NSString stringWithFormat:#"%#", strDateFormat];
[df release];
It gives (null) value. Because disDate has (null) value.
Why disDate set to nil..?
Any solution..?
You gotta tell the formatter what format to expect when reading a string, too.
NSString *string = #"09-05-2012 00:00:00";
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [formatter dateFromString:string];
[formatter setDateFormat:#"MMM dd, yyyy"];
NSString *formattedString = [formatter stringFromDate:date];
-(NSString*)getStringWithFormat:(NSString*)pStrDate fromFormat:(NSString*)pFromFormat toFormat:(NSString*)pToFormat
{
NSDateFormatter *dtFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dtFormatter setDateFormat:pFromFormat];
NSDate *fromFormatDate = [dtFormatter dateFromString:pStrDate];
[dtFormatter setDateFormat:pToFormat];
return [dtFormatter stringFromDate:fromFormatDate];
}
and call it like this
NSString *strDate = [self getStringWithFormat:#"09-05-2012 00:00:00" fromFormat:#"dd-MM-yyyy HH:MM:SS" toFormat:#"MMM dd, yyyy"];
This is common function, You can use it for any formats, Just you have to pass that format in argument.
NSString *strDateFormat; //[strDate substringWithRange:NSMakeRange(0, 10)];
strDateFormat = [NSString stringWithFormat:#"%#", strDate];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMM dd, yyyy"];
// ###### Set dateformat also here according to strDateFormat
NSDate *disDate = [df dateFromString:strDateFormat];
[df setDateFormat:#"MMM dd, yyyy"];
strDateFormat = [df stringFromDate:disDate];
lblDate.text = [NSString stringWithFormat:#"%#", strDateFormat];
[df release];
you have not set the formater pattern
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setFormatterBehavior:NSDateFormatterBehavior10_4];
[df setDateFormat:#"MMM dd, yyyy"];
NSDate *convertedDate = [df dateFromString:origDate];
[df release];

NSDate and NSString cocoa

For my iPhone app I got to convert NSDate object to string, and then convert the string back to NSDate object.. Can someone help me out? thank you!
Use to convert from NSDate to NSString
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSString *stringFromDate = [formatter stringFromDate:myNSDateInstance];
Use to convert from NSString to NSDate.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSDate *myDate = [df dateFromString: stringFromDate];
You need to check out NSDateFormatter this does exactly this, both directions.
convert NSDate to NSString
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy"];
//Optionally for time zone converstions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"..."]];
NSString *stringFromDate = [formatter stringFromDate:myNSDateInstance];
Convert NSString to NSDate
NSString *dateString = #"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];

NSDateFormatter and yyyy-MM-dd

I'm trying to take a NSString date in the format "2/22/11" and convert it to this format: 2011-02-22
This is my code:
NSDate *dateTemp = [[NSDate alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
dateTemp = [dateFormat dateFromString:newInvoice.date];
newInvoice.date = [dateFormat stringFromDate:dateTemp];
newInvoice.date starts as an NSString equal to "2/22/11". dateTemp ends up NIL. newInvoice.date ends up NIL as well.
I can't for the life of me figure out why.
You are facing this problem because your date formatter is not correct.Suppose your newInvoice.date variable store "11:02:23" the your dateFormatter should be #"yy:MM:dd" and if your newInvoice.date variable store"2/22/11" then your dateFormatter should be #"MM/dd/yy"
NSDate *dateTemp = [[NSDate alloc] init];
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:#"MM/dd/yy"];
[dateFormat2 setDateFormat:#"yyyy-MM-dd"];
dateTemp = [dateFormat1 dateFromString:newInvoice.date];
newInvoice.date = [dateFormat2 stringFromDate:dateTemp];
both format should be according to your requirement to get the correct result
This should work fine. I have set Date style. You can change NSDateFormatterShortStyle to something else. Also check your newInvoice.date format.
NSDate *dateTemp = [[NSDate alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle: NSDateFormatterShortStyle];
dateTemp = [dateFormat dateFromString: newInvoice.date];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
newInvoice.date = [dateFormat stringFromDate:dateTemp];

Date Formatting in iphone

I am having a date as 2010-08-02 which is 2nd August 2010 now i want to dispaly this as Monday,8 August ,2010
Thanks for the help
You should take a look at NSDateFormatter and if neccessary create your own formatter from a String like in the second link (The last listing).
http://developer.apple.com/iphone/library/documentation/cocoa/reference/foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
http://developer.apple.com/iphone/library/documentation/cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"EEEE',' d MMMM yyyy"];
NSDate *date = [inputFormatter dateFromString:#"2010-08-02"];
NSString *dateString = [outputFormatter stringFromDate:date];
NSLog(#"%#", dateString);
[inputFormatter release];
[outputFormatter release];

How to handle different date time formats using NSDateFormatter

I have a String with a datetime format: "YYYY-MM-DD HH:MM:SS".
I use this in my source code:
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"%e. %B %Y"];
NSString *test = [formatter stringFromDate:#"2010-01-10 13:55:15"];
I want to convert from "2010-01-10 13:55:15" to "10. January 2010".
But my implementation does not work.
What's wrong here?
Thanks a lot in advance & Best Regards.
Updated source code:
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"%Y-%m-%d %H:%M:%S"];
NSString *test1 = [formatter stringFromDate:#"2010-01-10 13:55:15"];
NSDateFormatter *formatter1 = [[[NSDateFormatter alloc] init] autorelease];
[formatter1 setDateFormat:#"%d. %M4 %Y"];
NSString *test2 = [formatter1 stringFromDate:test1];
A date formatter can only handle one format at a time. You need to take this approach:
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [f dateFromString:#"2010-01-10 13:55:15"];
NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
[f2 setDateFormat:#"d. MMMM YYYY"];
NSString *s = [f2 stringFromDate:date];
s will now be "10. January 2010"
Here are a few examples of working with data formatters from my code. You should be able to take any one of these functions and tweak it for your format.
USAGE
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [Constants getTitleDateFormatter];
NSString *dateString = [dateFormatter stringFromDate:today];
[dateFormatter release];
FUNCTIONS
+ (NSDateFormatter *) getDateFormatterWithTimeZone {
//Returns the following information in the format of the locale:
//YYYY-MM-dd HH:mm:ss z (Z is time zone)
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
return dateFormatter;
}
+ (NSDateFormatter *)dateFormatterWithoutYear {
NSDateFormatter *dateFormatter = [Constants getDateFormatterWithTimeZone];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *format = [dateFormatter dateFormat];
format = [format stringByReplacingOccurrencesOfString:#"/yy" withString:#""];
NSRange secondSpace;
secondSpace.location = format.length-2;
secondSpace.length = 1;
format = [format stringByReplacingCharactersInRange:secondSpace withString:#""];
[dateFormatter setDateFormat:format];
return dateFormatter;
}
+ (NSDateFormatter *) dateFormatterMonthDayOnly {
NSDateFormatter *dateFormatter = [Constants getDateFormatterWithTimeZone];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *format = [dateFormatter dateFormat];
format = [format stringByReplacingOccurrencesOfString:#"/yy" withString:#""];
NSRange range;
range.location = 0;
range.length = 3;
format = [format substringWithRange:range];
[dateFormatter setDateFormat:format];
return dateFormatter;
}
+ (NSDateFormatter *) getTitleDateFormatter {
//Returns the following information in the format of the locale:
//MM-dd-yyyy hh:mm:ssa
NSDateFormatter *dateFormatter = [Constants getDateFormatterWithTimeZone];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
NSString *format = [dateFormatter dateFormat];
NSRange secondSpace;
secondSpace.location = format.length-2;
secondSpace.length = 1;
format = [format stringByReplacingOccurrencesOfString:#"/" withString:#"-"];
format = [format stringByReplacingCharactersInRange:secondSpace withString:#""];
[dateFormatter setDateFormat:format];
return dateFormatter;
}
First off, make sure you set the behavior to 10.4 - more modern, works better in my experience.
[dateTimeFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
Next, you can't use the same format to parse and format if they have 2 different string representations, so use 2 formatters, or change the string format between parsing and then formatting.
Also make sure you consider the formatting options:
http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1
lowercase is e is the day of week, lowercase d is the day of the month.
For month, use MMMM, not B.
You want to use [NSFormatter dateFromString:] to convert your string-based date to an NSDate instance. From there you want to use stringFromDate with the NSDate, not the string as you have written above. I'm not sure about using the same NSDateFormatter for both parsing and formatting - you may need two separate instances to handle the different format styles.