search
LoginSignup
1

More than 5 years have passed since last update.

posted at

iOSの時計が12時間表記のときにアプリ側で24時間表記にする

サンプルソース

DateFormatter+format.swift
extension DateFormatter {

    class func allFormat(date: Date) -> String {
        let format = DateFormatter()
        format.dateFormat = "yyyy/MM/dd HH:mm"
        format.calendar = Calendar(identifier: Calendar.Identifier.gregorian)
        format.locale = NSLocale.system
        format.timeZone = NSTimeZone.system
        return format.string(from: date)
    }
}

上記のようなextensionを作ってやればいいかなと

呼び出す場合は以下

ViewController.swift
class ViewController: UIViewController {
    ・・・
    func hoge() {
        print(DateFormatter.allFormat(date: Date()))
    }
}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
1