Date Formatter在Swift中未正常工作。

huangapple go评论67阅读模式
英文:

Date Formatter in Swift is not working properly

问题

我正在使用Swift处理日期。我需要将字符串转换为日期,但出现了一些奇怪的情况。当我在模拟器上运行我的代码并将日期字符串传递给格式化器时,它返回nil。但当我在物理设备上运行我的代码时,它完美运行。

class ChatViewController: MessagesViewController {
    
    var uid: String
    var isNewConversations: Bool
    private var messages = [Message]()
    private var selfSender: Sender?
    var senderUid: String
    var conversation_Id: String?
    
    public static let dateFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        formatter.timeStyle = .long
        formatter.locale = .current
        return formatter
    }()
}

我这样使用它:

let date = ChatViewController.dateFormatter.date(from: dateString)

我的日期输入字符串是:“08-Jan-2023 at 8:57:10 PM GMT+5”

我已经调试了整个代码,但我不知道发生了什么。当我在物理设备上调试时,它完美运行,但当我在模拟器上运行时,它不正常工作,即调试器显示在 'date' 变量中得到了nil。

英文:

I am working with dates in Swift. I have to convert from String to Date but some weird stuff is happening. When I run my code on a simulator and pass my date string to formatter, it returns nil. But when I run my code on a physical device it works perfectly.

class ChatViewController: MessagesViewController {
    
    var uid: String
    var isNewConversations:Bool
    private var messages = [Message]()
    private var selfSender: Sender?
    var senderUid: String
    var conversation_Id: String?
    
    public static let dateFormatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        formatter.timeStyle = .long
        formatter.locale = .current
        return formatter
    }()
}

and I am using it like this :

let date = ChatViewController.dateFromatter.date(from: dateString)

My date input in string is: “08-Jan-2023 at 8:57:10 PM GMT+5”

I just debug the whole code but I am not getting what is happening. When I debug it on physical device it works perfectly but when I run it on a simulator it is not working fine, i.e the debugger shows that in 'date' variables you have got nil.

答案1

得分: 2

永远不要在解析日期字符串时使用 dateStyle 和/或 timeStyle。始终使用 dateFormat。样式可能会根据用户的区域设置和设备上的其他设置而变化。它也可能会根据用户的语言而变化。

由于您需要解析固定格式的日期字符串,您需要提供一个具体的日期格式。您还必须在日期格式化程序中使用特殊的区域设置,因为您的固定格式日期字符串始终是以英文为基础的。

public static let dateFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.locale = Locale(identifier: "en_US_POSIX")
    formatter.dateFormat = "dd-MMM-yyyy 'at' h:mm:ss a Z" // 可能需要在末尾使用 O 而不是 Z。
    return formatter
}()
英文:

Never use dateStyle and/or timeStyle when parsing a date string. Always use dateFormat. Styles can vary based on the user's locale and other settings on their device. It can also vary based on the user's language.

Since you need to parse a fixed format date string you need to provide a specific date format. You must also use a special locale on the date formatter since your fixed format date string is always in English.

public static let dateFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.locale = Locale(identifier: "en_US_POSIX")
    formatter.dateFormat = "dd-MMM-yyyy 'at' h:mm:ss a Z" // Might need O instead of Z at the end.
    return formatter
}()

答案2

得分: -1

这个日期格式化器如何:

```swift
let customFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.locale = Locale(identifier: "en_US_POSIX")
    formatter.dateFormat = "dd-MMM-yyyy' at 'h:mm:ss a ZZZZ"
    return formatter
}()
英文:

How about this date formatter:

let customFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.locale = Locale(identifier: "en_US_POSIX")
    formatter.dateFormat = "dd-MMM-yyyy' at 'h:mm:ss a ZZZZ"
    return formatter
}()

huangapple
  • 本文由 发表于 2023年1月9日 05:03:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定