views:

272

answers:

1

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!

+1  A: 

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

KennyTM
Ok great, this is surprisingly convenient. And thanks for the suggestion, unfortunately I'm too new to know what a .plist is or how to work with dictionaries. Or what a dictionary is, for that matter. If you could point me in the right direction there that would be great, because I'd like to make this as easy and efficient as possible. Otherwise, you answered my question perfectly and really quickly, so thanks a lot! I'll go ahead and try this out right now
Arthur Skirvin
BAM! I just adapted this and displaying the current date in the label works perfectly. Again, thanks a lot!
Arthur Skirvin
@Arthur: Welcome. A dictionary is also known as associative arrays or hash maps in other languages. `.plist` is a serialization format to store arrays, dictionaries, strings, numbers, dates and binary data.
KennyTM
Cool then, I'll be looking into that.
Arthur Skirvin