how to concatenate NSString and NSDateFormatter - iphone

I need to concatenate a NSString with the NSString output of NSDateFormatter. My code is below.
Please check where I am going wrong. I have to concatenate DEST PATH with datestring.
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy HH-mm"];
NSString * dateString = [formatter stringFromDate:[NSDate date]];
NSString *DEST_PATH=[NSHomeDirectory() stringByAppendingString:#"/Documents/Movie1];
// below is for concatenate
result =[result stringByAppendingString:DEST_PATH];
result=[result stringByAppendingString:dateString];

It appears as though you never instantiate the NSString result. I would change the above to the following:
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy HH-mm"];
NSString * dateString = [formatter stringFromDate:[NSDate date]];
NSString *DEST_PATH=[NSHomeDirectory() stringByAppendingString:#"/Documents/Movie1];
NSString* result=[DEST_PATH stringByAppendingPathComponent:dateString];
If you use the stringByAppendingPathComponent method it will automatically add the slash for you.
Also, using all capital letters for identifiers usually denotes a c style propocessor macro constant, so using it as a variable may confuse some people.

NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy HH-mm"];
NSString * dateString = [formatter stringFromDate:[NSDate date]];
NSString *DEST_PATH=[NSHomeDirectory() stringByAppendingString:#"/Documents/Movie1];
// below is for concatenate
NSString *resultStr=[NSString stringWithFormat:#"%#%#",DEST_PATH,dateString];
Hope this helps you.

Related

Date format does not work properly

I take date into string. date format is like this and string is
datenotification=20-02-2012
Now I want to change this into
20-feb-2012
but for checking purposes I just place that into a dateformatter and print that but it shows me an incorrect date
NSDateFormatter* currentdateformate = [[NSDateFormatter alloc] init];
[currentdateformate setDateFormat:#"dd-MM-YYYY"];
NSDate *d=[currentdateformate dateFromString:dateNotification];
NSString *str3=[currentdateformate stringFromDate:d];
[currentdateformate release];
Hey Check this example
NSString *string = #"12-02-2000";
NSDateFormatter* currentdateformate = [[NSDateFormatter alloc] init];
[currentdateformate setDateFormat:#"dd-MM-yyyy"];
NSDate *d=[currentdateformate dateFromString:string];
[currentdateformate setDateFormat:#"dd-MMM-yyyy"];
NSString *str3=[currentdateformate stringFromDate:d];
[currentdateformate release];
NSLog(#"S %#",str3);
output S 12-feb-2012

Need help for specific date format

I have next date string,
2011-08-30T11:44:54.345-04:00
Help me to compose right format to parse it with dateFromString:
You need to eliminate the last colon (timezone, "-04:00" -> "-0400"), as Z will take something like "-0800", then you can give this a try:
NSString *dateString = #"2011-08-30T11:44:54.345-04:00";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-ddEHH:mm:ss.SSSZ"];
NSDate *date = [dateFormat dateFromString:dateString];
[dateFormat release];
For more information on date formatting, refer to this page.
This is following the tilo's answer but with few things added.
NSString *dateString = #"2011-08-30T11:44:54.345-04:00";
NSRange range = [dateString rangeOfString:#":" options:NSBackwardsSearch];
dateString = [dateString stringByReplacingOccurrencesOfString:#":" withString:#"" options:0 range:range];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-ddEHH:mm:ss.SSSZ"];
NSDate *date = [dateFormat dateFromString:dateString];
[dateFormat release];
You would get the required format.

String to a Format yyyy-MM-dd HH:mm:ss Iphone

I have an nsstring ,see below
NSString *Mydate=#"9/8/2011"; in month/day/year format.
I want this string to be in the format yyyy-MM-dd HH:mm:ss.
for eg: 2011-09-08 15:51:57
so that i need to show the string in a label in the later format.
Thanks all.
Try below code with valid input string that will help you.
NSString *dateStr = #"9/8/2011 11:10:9";
NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
[dtF setDateFormat:#"d/M/yyyy hh:mm:s"];
NSDate *d = [dtF dateFromString:dateStr];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:s"];
NSString *st = [dateFormat stringFromDate:d];
NSLog(#"%#",st);
[dtF release];
[dateFormat release];
You may use NSDateFormatter

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

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.