您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

MMM dd,yyyy hh:mm:ss a的正确date.format是什么?以及如何转换为dd-mm-yyyy HH:ii

MMM dd,yyyy hh:mm:ss a的正确date.format是什么?以及如何转换为dd-mm-yyyy HH:ii

在日期及其文本表示形式之间进行转换的DateFormatter

您可以在此处找到更多Locale标识符。

并尝试此代码

let dateStr = "Wed, 26 Jul 2017 18:10:02 +0530"
if let date = Date(fromString: dateStr, format: "MMM dd, yyyy hh:mm:ss a") {
        debugPrint(date.toString(format: "dd-MM-yyyy HH:ii"))
}

extension Date {

    // Initializes Date from string and format
    public init?(fromString string: String, format: String, identifier: String = Locale.current.identifier) {
        let formatter = DateFormatter()
        formatter.dateFormat = format
        formatter.locale = Locale(identifier: identifier)
        if let date = formatter.date(from: string) {
            self = date
        } else {
            return nil
        }
    }

    // Converts Date to String, with format
    public func toString(format: String, identifier: String = Locale.current.identifier) -> String {
        let formatter = DateFormatter()
        formatter.locale = Locale(identifier: identifier)
        formatter.dateFormat = format
        return formatter.string(from: self)
    }
}

extension String {
    // Converts String to formated date string, with inputFormat and outputFormat
    public func toDate(form inputFormat: String, to outputFormat: String, identifier: String = Locale.current.identifier) -> String? {
        return Date(fromString: self, format: inputFormat, identifier: identifier)?.toString(format: outputFormat, identifier: identifier)
    }

    // Converts String to Date, with format
    func toDate(format: String, identifier: String = Locale.current.identifier) -> Date? {
        return Date(fromString: self, format: format, identifier: identifier)
    }
}
其他 2022/1/1 18:17:35 有438人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶