Make App Pie

Training for Developers and Artists

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: Logo

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: