Ios 如何从nsdate(timeintervalsince1970)创建时间戳
我已经搜索了一段时间,没有找到一个很好的答案。目前,当用户发布照片时,它会获取一个nsdate(timeIntervalSince1970),并在上传到firebase时将其作为一个值。一个问题是,当前我将nsdate更改为时间戳或分钟/小时/天的方式,因为它发布后没有变为良好,它不正确到一天过去的时间,它只显示它发布的时间。我希望我能得到一个答案或引用一种算法,它可以将nsdate(timeIntervalSince1970)更改为时间戳或字符串,该时间戳或字符串只给出它发布的时间,并且每24小时将其更改为1天。我猜我提前道歉,因为我问了一个简单的问题。这是我目前的代码:Ios 如何从nsdate(timeintervalsince1970)创建时间戳,ios,swift,Ios,Swift,我已经搜索了一段时间,没有找到一个很好的答案。目前,当用户发布照片时,它会获取一个nsdate(timeIntervalSince1970),并在上传到firebase时将其作为一个值。一个问题是,当前我将nsdate更改为时间戳或分钟/小时/天的方式,因为它发布后没有变为良好,它不正确到一天过去的时间,它只显示它发布的时间。我希望我能得到一个答案或引用一种算法,它可以将nsdate(timeIntervalSince1970)更改为时间戳或字符串,该时间戳或字符串只给出它发布的时间,并且每24
if let seconds = posts[visibli.row].timeStamp {
let time = NSDate(timeIntervalSince1970: TimeInterval(seconds))
let formatter = DateFormatter()
formatter.dateFormat = "hh:mm a"
labelTime.text = formatter.string(from: time as Date)
}
谢谢你的帮助 您要查找的是
datecomponentsformter
。您可以查看不同的格式化程序Foundation
产品
如果希望有一个字符串表示自给定日期以来经过的时间量,可以说:
if let seconds = posts[visibli.row].timeStamp {
let time = Date(timeIntervalSince1970: TimeInterval(seconds))
let dateComponentsFormater = DateComponentsFormatter()
dateComponentsFormater.unitsStyle = .positional
let string = dateComponentsFormater.string(from: time, to: Date())
labelTime.text = string
}
当前时间戳为字符串。
public class func getCurrentTimeStamp()->String{
return String(describing: NSNumber(value: round(NSDate().timeIntervalSince1970 * 1000)))
}
此函数使用获取当前时间戳。FYI-不要使用
NSDate
。只需使用Date
。有没有办法消除字符串中的秒数?beyowulfDateComponentsFormer.allowedUnits=[.day、.hour、.minute]
可用于指定所需的单位。