How to get a string with the Date and time in XCode / Cocoa Touch

There are times  you look for something simple, and get very frustrated. One of those for me was getting a freaking time stamp on my app.

After a bit of chasing around, I did find this in Apple’s documentation, in case you are looking for it.

// code to get the date and time  in a string.
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
    NSDate *date = [NSDate date];
    NSString *formattedDateString = [dateFormatter stringFromDate:date];

Note there are other constant you can use besides NSDateMediumStyle.

Check out the NSDateFormatter Class Reference for the whole story

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s