xcode swift am/pm time to 24 hour format

convert 24 hour to 12 hour swift
date format with am pm swift 4
swift detect 24 hour time
swift date show am pm
swift date format
datepicker 24 hour format swift
dateformatter am/pm
swift dateformatter time

I am trying to convert a am/pm format time to a 24 hour format time

6:35 PM to 18:35

I tried this piece of code on playground but it doesnt seem to work if I put the time alone

let dateAsString = "02/12/15, 6:35 PM"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH"
let date = dateFormatter.dateFromString(dateAsString) //returns nil

Does anyone know how to accomplish this?

Thank you.

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.

let dateAsString = "6:35 PM"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "h:mm a"
let date = dateFormatter.dateFromString(dateAsString)

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

The curious case of the 24-hour time format in Swift, NSDateFormatter Issue - Getting wrong date format Objective-C On iOS, the user can override the default AM/PM versus 24-hour time setting (via Settings >  Questions: I am trying to convert a am/pm format time to a 24 hour format time 6:35 PM to 18:35 I tried this piece of code on playground but it doesnt seem to work if I put the time alone let dateAsString = "02/12/15, 6:35 PM" let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "HH" let date

Swift 3

Time format 24 hours to 12 hours

let dateAsString = "13:15"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"

let date = dateFormatter.date(from: dateAsString)
dateFormatter.dateFormat = "h:mm a"
let Date12 = dateFormatter.string(from: date!)
print("12 hour formatted Date:",Date12)

output will be 12 hour formatted Date: 1:15 PM

Time format 12 hours to 24 hours

let dateAsString = "1:15 PM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "h:mm a"

let date = dateFormatter.date(from: dateAsString)
dateFormatter.dateFormat = "HH:mm"

let Date24 = dateFormatter.string(from: date!)
print("24 hour formatted Date:",Date24)

output will be 24 hour formatted Date: 13:15

NSDateFormatter Issue, H, 17, The 24-hour hour. HH, 17, The 24-hour hour padding with a zero if there is only 1 digit. a, PM, AM / PM for 12-hour time formats. Minute. m, 25, The minute,  The 24-hour hour. HH: 17: The 24-hour hour padding with a zero if there is only 1 digit. a: PM: AM / PM for 12-hour time formats: Minute: m: 25: The minute, with no padding for zeroes. mm: 25: The minute with zero padding. Second: s: 1: The seconds, with no padding for zeroes. ss: 01: The seconds with zero padding. Time Zone: zzz: EST: The 3 letter name of the time zone.

Swift 3 *

Code to convert 12 hours (i.e. AM and PM) to 24 hours format which includes-

Hours:Minutes:Seconds:AM/PM to Hours:Minutes:Seconds

func timeConversion24(time12: String) -> String {
    let dateAsString = time12
    let df = DateFormatter()
    df.dateFormat = "hh:mm:ssa"

    let date = df.date(from: dateAsString)
    df.dateFormat = "HH:mm:ss"

    let time24 = df.string(from: date!)
    print(time24)
    return time24
}

Input

07:05:45PM

Output

19:05:45

Similarly

Code to convert 24 hours to 12 hours (i.e. AM and PM) format which includes-

Hours:Minutes:Seconds to Hours:Minutes:Seconds:AM/PM

func timeConversion12(time24: String) -> String {
    let dateAsString = time24
    let df = DateFormatter()
    df.dateFormat = "HH:mm:ss"

    let date = df.date(from: dateAsString)
    df.dateFormat = "hh:mm:ssa"

    let time12 = df.string(from: date!)
    print(time12)
    return time12
}

Input

19:05:45

Output

07:05:45PM

How To Format Date Time In Swift, I am trying to convert a am/pm format time to a 24 hour format time6:35 PM to 18: 35I tried this piece of code on playground but it doesnt seem  The screen shot choices looked a little different by 2020-02-28, but I figured out that clicking on “Short Date” and choosing the one without AM or PM gave me 24 hour time. Reply Caspian says:

Below is the swift 3 version of the solution -

let dateAsString = "6:35:58 PM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "h:mm:ss a"
let date = dateFormatter.date(from: dateAsString)

dateFormatter.dateFormat = "HH:mm:ss"
let date24 = dateFormatter.string(from: date!)
print(date24)

xcode swift am / pm время в 24-часовом формате – 7 Ответов, I am trying to convert a am/pm format time to a 24 hour format time 6:35 PM to 18: 35. I tried this piece of code on playground but it doesnt seem  24-Hour Time Format. Many places in the world use the 24-hour time format. 24-hour time format is similar regular AM/PM time, except that you keep counting up after you get past 12 PM (noon). So 1 PM in 24-hour format is 13:00, 2 PM is 14:00, and so on. All you need to do is add 12 to any time in the PM to get 24-hour format time.

Swift version 3.0.2 , Xcode Version 8.2.1 (8C1002) (12 hr format ):

func getTodayString() -> String{

        let formatter = DateFormatter()
        formatter.dateFormat = "h:mm:ss a "
        formatter.amSymbol = "AM"
        formatter.pmSymbol = "PM"

        let currentDateStr = formatter.string(from: Date())
        print(currentDateStr)
        return currentDateStr 
}

OUTPUT : 12:41:42 AM

Feel free to comment. Thanks

NSDateFormatter.com, Я пытаюсь преобразовать время формата am/pm в 24-часовой формат let Date24 = dateFormatter.string(from: date!) print("24 hour formatted Date:",Date24 ) dateFormat = "HH:mm:ss" //Setting the format for the time save. let time  iOS Cocoa Touch view that shows a time in 12-hour or 24-hour format. In 12-hour format the AM/PM text can be placed either to the right or below the time. The time and AM/PM fonts and font sizes can be different.

How NSDateFormatter can support both 12 and 24h format , Sunday, Jul 5, 2020, EEEE, MMM d, yyyy. 07/05/2020, MM/dd/yyyy. 07-05-2020 14:03, MM-dd-yyyy HH:mm. Jul 5, 2:03 PM, MMM d, h:mm a. July 2020 Step 1: Following above steps, a list of 24-hour format time is displayed in B column. Now in column C->C1, enter the formula ‘=TEXT(A1,”HH:MM:SS AM/PM”)’. Verify that 24-hour format is converted back to 12-hour format. Step 2: Use the same way to fill C2 and C3.

swift, articles, code samples, open source projects and anything else related to iOS, macOS, watchOS, or tvOS … text in the textfield supporting both 12h and 24h format (showing am/pm when 12h format). You can detect if the user is using 12/24 hour time with something like this. Found this in release notes of Xcode 12. Converting a DateTime to this format allows you to avoid local date time format issues when passing DateTime values between systems. Time using users 24/12 hour

func timeUpdate(sender: NSDate) { let timeSave = NSDateFormatter() //Creating first object to update time label as 12hr format with AM/PM timeSave.timeStyle  The above table was generated with the en_US locale, which means it uses the US English style of formatting date and time: AM/PM, and month/day/year. Compare this to different countries and locales around the world, that use a 24-hour clock, and day-month-year, and you see why locales are so important!

Comments
  • import UIKit let date = Date() print(" Date:",date) let dateFormatter = DateFormatter() dateFormatter.dateFormat = "h:mm a" let Date12Hr = dateFormatter.string(from: date) print("12 hour formatted Date:",Date12) this work fine in playground or Simulator ..while running in device its not converted ???
  • This code gives nil if device time in 24 hour format. Any advice?
  • try this Add following line after dateFormatter declaration. dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale! This resolve my issue.
  • When changing from 24-hour to 12-hour format, I need to add dateFormatter.locale = Locale(identifier: "en_US_POSIX") to force it to ignore device locale.
  • it is just a conversion of the date!
  • You can try setting device to 24-hour format. The code above doesn't work in my case.
  • This question is about going from 12 hour time to 24 hour time, this answer deals with going in the other direction.
  • Not correct answer!! You are confused because you are testing with 12:41:42. Try 1pm or greater.
  • hi if a user input 6:11AM, how can i convert to 2019-03-09 13:38:32 +0000 in this format
  • so you have to use current date with time ( input provided by user)
  • this work fine in playground and Simulator ..while running in device its not converted
  • Shouldn't it be (hour >= 12) - since 12.00 noon is 12 p.m. and 00.00 midnight is 12 a.m.
  • This isn't military time.