5

I am Trying to assign the 24 hour formate to UIDATEPICKERVIEW but i have not exactly idea to implement it.

in my view.h file

 @property(nonatomic,retain) IBOutlet UIDatePicker *picker_alarm;

in my view.m file

picker_alarm.date=[NSDate date];  

in my .xib file

i have taken uidatepicker and its Mode Property to Time in viewcontroller.

Here is the my **Output**

Kumar KL
  • 15,315
  • 9
  • 38
  • 60
Jignesh Kanani
  • 223
  • 1
  • 4
  • 18
  • **http://stackoverflow.com/questions/11151187/show-time-in-12-and-24-hour-format-in-uidatepicker-on-the-basis-of-app-settings** && **https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDatePicker_Class/Reference/UIDatePicker.html#//apple_ref/occ/instp/UIDatePicker/timeZone** – Kumar KL Jan 29 '14 at 11:40
  • Possible duplicate of: http://stackoverflow.com/questions/11257127/setting-nsdate-in-a-timepicker-with-24-12-hours-format-xcode – CW0007007 Jan 29 '14 at 11:40

4 Answers4

12
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"NL"];
[picker_alarm setLocale:locale];
nerowolfe
  • 4,787
  • 3
  • 20
  • 19
  • actually it does in the end. so thanks if(isTimeFormat12) { NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"US"]; [self.dpTime setLocale:locale]; } else { NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"UK"]; [self.dpTime setLocale:locale]; } – Zsolt Oct 01 '14 at 13:42
4

You can also use the storyboard to change time format to 24 hour.

Follow these steps:

  1. go to storyboard
  2. select you datePicker
  3. select attributes inspector
  4. Change "Locale" property to English(Europe)

enter image description here

and in your code use following snippet:

NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"HH:mm"];
somelabel.text = [outputFormatter stringFromDate:_pickerDate.date];
Jabbar
  • 590
  • 1
  • 7
  • 21
1

You cannot force the date picker to show 24 hours time. That depends on the locale property

Doc says:

UIDatePickerModeTime, // Displays hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. 6 | 53 | PM)

[datePicker setLocale:[NSLocale systemLocale]];

For testing this change the system time to 24hour mode. And check the picker in simulator

Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
0

Create DatePicker with setting it custom Locale

    let datePicker = UIDatePicker()
    datePicker.datePickerMode = UIDatePickerMode.time 
    datePicker.locale = Locale.init(identifier: "NL")  //or: Locale.init(identifier:en_GB") // using Great Britain

Grab the time from above DatePicker,

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "HH:mm"
    lblTime.text = dateFormatter.string(from: sender.date)
ViJay Avhad
  • 2,684
  • 22
  • 26