我有一个实例,DateTime我想将其格式化为String.我怎么做?我想把日期变成一个字符串,比如"2013-04-20".
我无法找到解决方案,我从firebase获取数据,其中一个字段是时间戳,看起来像这样 - > 1522129071.在swift我能够转换它并使其可读但我不能由于我不知道所有的类和谷歌没有帮助什么不是,所以将代码转换为飞镖.
SWIFT:
func readTimestamp(timestamp: Int) {
let now = Date()
let dateFormatter = DateFormatter()
let date = Date(timeIntervalSince1970: Double(timestamp))
let components = Set<Calendar.Component>([.second, .minute, .hour, .day, .weekOfMonth])
let diff = Calendar.current.dateComponents(components, from: date, to: now)
var timeText = ""
dateFormatter.locale = .current
dateFormatter.dateFormat = "HH:mm a"
if diff.second! <= 0 || diff.second! > 0 && diff.minute! == 0 || diff.minute! > 0 && diff.hour! == 0 || diff.hour! > 0 && diff.day! == 0 {
timeText = …Run Code Online (Sandbox Code Playgroud) 我正在获取数据云存储,并尝试使用以下代码在我的应用中显示。
new Text(timeago.format(document.data['tripDoc']['docCreatedOn'])),
Run Code Online (Sandbox Code Playgroud)
我正在使用timeagodart包进行格式化。但是,在更新到最新的Cloud Firestore插件后,出现此错误-
Another exception was thrown: type 'Timestamp' is not a subtype of type 'DateTime'
Run Code Online (Sandbox Code Playgroud)
无法理解如何将“ TimeStamp”对象解析为“ DateTime”。因为timeago插件需要DateTime对象格式的数据。