Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Current Time in string in Custom format in objective c

I want current time in following format in a string.

dd-mm-yyyy HH:MM

How?

like image 978
Sagar Kothari Avatar asked Nov 06 '09 01:11

Sagar Kothari


People also ask

How do I get the current date in a string in Objective C?

Just 3 steps. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; Set the date format in which you want your string.

How do I get the current date and time in Objective C?

NSDate and NSDateFormatter classes provide the features of date and time. NSDateFormatter is the helper class that enables easy conversion of NSDate to NSString and vice versa. A simple example to show conversion of NSDate to NSString and back to NSDate is shown below.

How do I print a string in Objective C?

You can use %@ for all objects including NSString. This will in turn call the objects description method and print the appropriate string.

How do you find the length of a string in Objective C?

int len = [myString length];


1 Answers

You want a date formatter. Here's an example:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"dd-MM-yyyy HH:mm"];  NSDate *currentDate = [NSDate date]; NSString *dateString = [formatter stringFromDate:currentDate]; 
like image 76
Carl Norum Avatar answered Oct 13 '22 19:10

Carl Norum