Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current time on the iphone in a chosen format

The following should give me an NSDate object of 9:30:

NSString *dateString = @"09-30";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh-mm"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];

How do I get the current time in the same format?

like image 845
Eric Brotto Avatar asked Dec 05 '11 12:12

Eric Brotto


People also ask

How do I display the time on my iPhone?

By default, the date and time, visible on the Lock Screen, are set automatically based on your location. If you want to change them—for example, when you're traveling—you can adjust them. Go to Settings > General > Date & Time.

How do I get the current time shortcut?

To insert the current time, press Ctrl+Shift+; (semi-colon). To insert the current date and time, press Ctrl+; (semi-colon), then press Space, and then press Ctrl+Shift+; (semi-colon).

How do I get the time and date on my home screen iPhone?

Answer: A: Answer: A: Time is at the top left of your home screen, if your phone is locked, tap the home screen and it will display time and date.


2 Answers

NSDate *currentTime = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh-mm"];
NSString *resultString = [dateFormatter stringFromDate: currentTime];
like image 190
Denis Avatar answered Sep 30 '22 15:09

Denis


NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh-mm"];
NSDate *currentDate = [NSDate date];
NSString *currentDateString = [dateFormatter stringFromDate:currentDate];
like image 24
Mathieu Hausherr Avatar answered Sep 30 '22 14:09

Mathieu Hausherr