iPhone: Displaying Date in a Label and String Manipulation
In order to learn some basics I've undertaken creating an app that displays two quotes a day. I'm having trouble figuring out how to work with the current date, which will be pretty necessary if I'm going to get this thing running.
My current plan is to name my .txt files with the quotes by day, and then get my filePath string to conform to the names of these files based on what day it is. For example, if it was January 30th, I'd have to find a way to turn the date into the string "01.30a" and "01.30b". ('a' and 'b' because of the whole two quotes a day thing). Any other regularized form would work, of course. This would require me to get the date into the string with a specific form, and to add a single character to the string. How might I accomplish this?
My second question is similar: How can I get the date into a string in the form "mm/dd/yy"? (so as to display it in a label)
Clearly I'm pretty new to this, and any help would be greatly appreciated! 开发者_开发百科Thanks!
The date can be formatted into a string using NSDateFormatter
.
NSDateFormatter* fmtr = [[NSDateFormatter alloc] init];
[fmtr setDateFormat:@"MM/dd/yy"];
NSString* label_str = [fmtr stringFromDate:date];
[fmtr setDateFormat:@"MM.dd"];
NSString* key_str = [fmtr stringFromDate:date]; // append "a" & "b" yourself.
[key_fmtr release];
(Suggestion: It's easier to use a .plist
, which can be converted to a dictionary with internal functions, then parsing a .txt
.)
精彩评论