Objective-C Formating Time in String with stringWithFormat:

April 12, 2009 § Leave a comment

This is the simplest way to convert the date and time in string using 
stringWithFormat: method, formatting it with correct digits.

The digit after "%0" assigns the no. of digit. 

%02 assigns 2 digits to hours, 2 digits to min, and 2 digit to sec.

// timeString "T15:10:30-08:00"
timeString = [NSString stringWithFormat:@"T%02d:%02d:%02d%@",
             [dateComponents hour],
             [dateComponents minute],
             [dateComponents second],
             timeOffsetString];

// dateString "2006-11-17T15:10:30-08:00"
  NSString *dateString = [NSString stringWithFormat:@"%04d-%02d-%02d%@",
             [dateComponents year],
             [dateComponents month],
             [dateComponents day],
             timeString];
Advertisements

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

What’s this?

You are currently reading Objective-C Formating Time in String with stringWithFormat: at Web Builders.

meta

%d bloggers like this: