Swift 显示用户发送消息时设备的当前时间
当用户发送消息时,如何显示消息附近的时间Swift 显示用户发送消息时设备的当前时间,swift,Swift,当用户发送消息时,如何显示消息附近的时间 我尝试过使用TimeInterval()和DateFormatter(),但未能成功实现它们。另外,我正在为消息单元格使用xib文件。您可以使用日期和日期格式化程序获取当前日期和时间 let formatter = DateFormatter() formatter.dateFormat = "HH:mm:ss" formatter.string(from: Date()) // Example: 19:38:20 formatter.dateFor
我尝试过使用
TimeInterval()
和DateFormatter()
,但未能成功实现它们。另外,我正在为消息单元格使用xib文件。您可以使用日期和日期格式化程序获取当前日期和时间
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm:ss"
formatter.string(from: Date()) // Example: 19:38:20
formatter.dateFormat = "yyyy/MM/dd"
formatter.string(from: Date()) // Example: 2019/07/07
// Or you can use dateStyle and timeStyle.
formatter.dateStyle = .long
formatter.timeStyle = .medium
您可以使用Date和DateFormatter获取当前日期和时间
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm:ss"
formatter.string(from: Date()) // Example: 19:38:20
formatter.dateFormat = "yyyy/MM/dd"
formatter.string(from: Date()) // Example: 2019/07/07
// Or you can use dateStyle and timeStyle.
formatter.dateStyle = .long
formatter.timeStyle = .medium
你的后端是什么?如果您使用的是firebase,您可以在帖子中添加一个名为“timestamp”的变量,从中您可以检索该帖子制作的时间,然后您可以轻松获得本地时间,并从发布时间中减去它。给我更多关于堆栈的信息,我可能会给你更多信息。你的后端是什么?如果您使用的是firebase,您可以在帖子中添加一个名为“timestamp”的变量,从中您可以检索该帖子制作的时间,然后您可以轻松获得本地时间,并从发布时间中减去它。给我更多关于堆栈的信息,我可能会给你更多信息。在消息数据模型中,你可以将messageTime的属性存储为日期对象,而不是使用DateTransfrom从服务器获取的字符串。现在,当您想要显示该消息日期时,您可以将该messageTime日期对象转换为一个字符串,该字符串采用您自己的格式,以便使用DateFormatter进行显示。我在这里使用映射器将json对象映射到我的数据模型对象
public class Message:Mappable{
public var text: String
public var messageTime: Date
public var id: String
required public init() {
super.init()
}
required convenience public init?(map: Map) { self.init() }
open override func mapping(map: Map) {
text <- map["text"]
messageTime <- (map["created_at"], DateTransform())
id <- map["id"]
}
}
Then fetch the 'message' object.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm a" //07:30 PM
let messageTimeString = dateFormatter.string(from: message.messageTime)
公共类消息:可映射{
公共变量文本:字符串
公共var消息时间:日期
公共变量id:String
必需的公共init(){
super.init()
}
必需的公共init?(map:map){self.init()}
打开覆盖函数映射(映射:映射){
text在消息数据模型中,可以将messageTime的属性存储为日期对象,而不是字符串(从服务器获取)使用DateTransfrom。现在,当您想要显示该消息日期时,您可以将该messageTime日期对象转换为使用DateFormatter显示的具有您自己格式的字符串。我在这里使用映射器将json对象映射到我的数据模型对象
public class Message:Mappable{
public var text: String
public var messageTime: Date
public var id: String
required public init() {
super.init()
}
required convenience public init?(map: Map) { self.init() }
open override func mapping(map: Map) {
text <- map["text"]
messageTime <- (map["created_at"], DateTransform())
id <- map["id"]
}
}
Then fetch the 'message' object.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm a" //07:30 PM
let messageTimeString = dateFormatter.string(from: message.messageTime)
公共类消息:可映射{
公共变量文本:字符串
公共var消息时间:日期
公共变量id:String
必需的公共init(){
super.init()
}
必需的公共init?(map:map){self.init()}
打开覆盖函数映射(映射:映射){
文本使用Date()
而不是TimeInterval()
使用Date()
而不是TimeInterval()
不要硬编码格式。使用dateStyle和timeStyle可以使结果正确显示在用户的区域设置中。不要硬编码格式。使用dateStyle和timeStyle可以使结果正确显示在用户的区域设置中。