Xcode Swift Am/Pm Time to 24 Hour Format

Xcode Swift am/pm time to 24 hour format

Just convert it to a date using NSDateFormatter and the "h:mm a" format and convert it back to a string using the "HH:mm" format. Check out this date formatting guide to familiarize yourself with this material.

Sample Image

let dateAsString = "6:35 PM"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "h:mm a"
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // fixes nil if device time in 24 hour format
let date = dateFormatter.dateFromString(dateAsString)

dateFormatter.dateFormat = "HH:mm"
let date24 = dateFormatter.stringFromDate(date!)

how to make 24 hours time format from string in Swift

Date has no format, so only can change the string converted from the date

Swift 4

 let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
let newDateString = dateFormatter.string(from: yourDate)

for different date format, you can check nsdateformatter.com

How can you properly support the iOS 12 hour / 24 hour time override in Date & Time settings for DateFormatters?

From the documentation for dateFormat on DateFormatter:

You should only set this property when working with fixed format
representations, as discussed in Working With Fixed Format Date
Representations. For user-visible representations, you should use the
dateStyle and timeStyle properties, or the
setLocalizedDateFormatFromTemplate(_:) method if your desired format
cannot be achieved using the predefined styles; both of these
properties and this method provide a localized date representation
appropriate for display to the user.

I still don't really understand why Foundation bothers to modify the date format if you're not using a fixed locale on your date formatter, but it seems the documentation does not recommend using the custom date format for non-fixed format representations.

setLocalizedDateFormatFromTemplate appears to be what you need if you can't just rely on dateStyle and timeStyle, although it doesn't appear to automatically change with a user's 24 hour time setting, so one could check to see if 24 hour time is on before specifying a template…

This method is supposed to allow you to specify a format without losing out on the formatting that is specific to each locale.

EDIT response from Apple Developer Relations (same idea, but more detail):

The correct way to handle this is to avoid specifying "hh" or "HH"
based on locale and instead using the "jj" skeleton symbol in
conjunction with -[NSDateFormatter setLocalizedDateFormatFromTemplate:].

From the "hour" section of the Date Field Symbol Table
(https://www.unicode.org/reports/tr35/tr35-dates.html#dfst-hour):

Input skeleton symbol In such a context, it requests the
preferred hour format for the locale (h, H, K, or K), as determined by
the preferred attribute of the hours element in the supplemental data.

By using "jj" you can get a format which best matches the user's
current settings:

NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setLocalizedDateFormatFromTemplate:@"jj:mm"];
NSLog(@"%@: 'jj:mm' => '%@' ('%@')", formatter.locale.localeIdentifier, formatter.dateFormat, [formatter stringFromDate:[NSDate date]]);

results in

  • en_US (12-hour): "en_US: 'jj:mm' => 'h:mm a' ('1:44 PM')
  • en_US (24-hour): "en_US: 'jj:mm' => 'HH:mm' ('13:44')
  • en_GB (12-hour): "en_GB: 'jj:mm' => 'h:mm a' ('1:44 pm')
  • en_GB (24-hour): "en_GB: 'jj:mm' => 'HH:mm' ('13:44')

This allows you to match your preferred format while keeping with the
user's preferred time settings.

Show AM/PM in capitals in swift

You can set your DateFormatter amSymbol and pmSymbol as follow:

Xcode 8.3 • Swift 3.1

let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "h:mm a 'on' MMMM dd, yyyy"
formatter.amSymbol = "AM"
formatter.pmSymbol = "PM"

let dateString = formatter.string(from: Date())
print(dateString) // "4:44 PM on June 23, 2016\n"

Convert hours form 12 hour time to 24 hour time Swift and save as an integer

  1. calendar.component(.hour, from: someDate) already gives you the time of day in 24 hour time so there's nothing else to do to solve your question.
  2. Not sure why you are adding 5 to the hour. You set the timezone to UTC so the date will be treated as the UTC timezone. Then you add 5 to that result. That's kind of strange. If you just want the current hour in the user's locale timezone, don't change the calendar's timezone and don't add 5 to the hour.
  3. Don't use NSDate or NSCalendar. This is Swift. Use Date and Calendar.

Updated code:

@IBAction func setTime() {
var date = Date()
//pickTimes()
var calendar = Calendar.current
var currentHour = calendar.component(.hour, from: date)
let currentMinutes = calendar.component(.minute, from: date)
let currentSeconds = calendar.component(.second, from: date)
timeText.text = ("\(currentHour):\(currentMinutes):\(currentSeconds)")
}

But it would be simpler to use a DateFormatter and set the timeStyle to .medium or maybe .long and format Date() into a string. This will give a properly localized time string.



Related Topics



Leave a reply



Submit