Конвертировать 12-часовую дату UTC в 24-часовой формат местного часового пояса в Swift - PullRequest
0 голосов
/ 20 ноября 2018

В соответствии с инструментом преобразования UTC в IST , если строка даты UTC 12Hr равна = "2018-12-31 07:35 PM", то ее необходимо преобразовать в "2019-01-01 01:05" (24-часовой формат в IST) [примечание: здесь 12Hr ISTдата конвертирования: «2019-01-01 01:05 AM»]

Так как же преобразовать эту 12-часовую дату UTC в 24-часовой формат IST dateFormat?

текущий код:

static func convertUTCdateToLocalTimezoneDate(dateString: String, fromDateFormat: String, toDateFormat: String) -> String {
        if dateString.count > 0 {
            let dateFormatter = DateFormatter()
            let is24Hr: Bool = AppUtility.isSystemTimeFormatIs24Hr(dateString: AppUtility.getIphoneCurrnetTime())
            let fromDateFormatLocal = is24Hr == true ? kDateIn24Format : kDateIn12Format
            dateFormatter.dateFormat = fromDateFormatLocal
            dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
            let date = dateFormatter.date(from: dateString)
            var timeStamp = ""
            dateFormatter.dateFormat = toDateFormat
            dateFormatter.timeZone = NSTimeZone.local
            timeStamp = dateFormatter.string(from: date!)
            return timeStamp
        }
        return ""
    }

static func isSystemTimeFormatIs24Hr(dateString: String) -> Bool {
    let formatter = DateFormatter()
    formatter.locale = NSLocale.current
    formatter.dateStyle = .none
    formatter.timeStyle = .short
    let amRange: NSRange? = (dateString as NSString).range(of: formatter.amSymbol)
    let pmRange: NSRange? = (dateString as NSString).range(of: formatter.pmSymbol)
    let is24h: Bool = amRange?.location == NSNotFound && pmRange?.location == NSNotFound
    return is24h
}

Ответы [ 2 ]

0 голосов
/ 14 января 2019

Следующий код решит вашу проблему

    func dateFormatter() -> Date? {
        let inputDateString = "2018-12-31 07:30:00 PM"

        let outFormatter = DateFormatter()
        outFormatter.locale = Locale(identifier: "UTC")
        outFormatter.timeZone = TimeZone(abbreviation: "UTC")
        outFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss a"

        let outDate = outFormatter.date(from: inputDateString)

        outFormatter.locale = Locale.current
        outFormatter.timeZone = TimeZone.current
        outFormatter.dateFormat = "yyyy-MM-dd hh:mm a"

        let date12HrsString = outFormatter.string(from: outDate!)
        print("12 Hrs Format: \(date12HrsString)")

        outFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let date24HrsString = outFormatter.string(from: outDate!)
        print("24 Hrs Format: \(date24HrsString)")

        return outDate
    }
0 голосов
/ 20 ноября 2018
func getDateFrom(_ dateString: String, withFormat formatString: String, timeZone: String = "UTC", locale: String = "UTC") -> Date? {
    var dateToReturn: Date?
    let formatter = DateFormatter()
    formatter.timeZone = NSTimeZone(abbreviation: timeZone) as TimeZone?
    formatter.locale = Locale(identifier: locale)
    formatter.dateFormat = formatString
    dateToReturn = formatter.date(from: dateString)
    return dateToReturn
}

func getStringFrom(_ date: Date, withFormat formatString: String, timeZone: String = "UTC", locale: String = "UTC") -> String? {
    var stringToReturn: String?
    let formatter = DateFormatter()
    formatter.timeZone = NSTimeZone(abbreviation: timeZone) as TimeZone?
    formatter.locale = Locale(identifier: locale)
    formatter.dateFormat = formatString
    stringToReturn = formatter.string(from: date)

    return stringToReturn
}
let string12H = "2018-12-31 07:35 PM"

if let date = getDateFrom(string12H, withFormat: "yyyy-MM-dd hh:mm a") {
    let dateStringWith12HFormat = getStringFrom(date, withFormat: "yyyy-MM-dd hh:mm a", timeZone: "IST", locale: "IST")
    let dateStringWith24HFormat = getStringFrom(date, withFormat: "yyyy-MM-dd HH:mm", timeZone: "IST", locale: "IST")
}
...