ios 如何在 Swift 中获取 18 位当前时间戳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39829831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get 18-digit current timestamp in Swift?
提问by The Cook
I want to get current timestamp like this:
我想获得这样的当前时间戳:
636110767775716756?
However, when I do :
但是,当我这样做时:
NSDate().timeIntervalSince1970
It returns a value like this:
它返回一个这样的值:
1475491615.71278
How do I access current time stamp ticks in the format I want? I check the dates from here:
回答by Martin R
You seem be looking for what DateTime.Ticks
is in C#, i.e. the
time since 0001-01-01 measured in 100-nanosecond intervals.
您似乎在寻找DateTime.Ticks
C# 中的内容,即自 0001-01-01 以来以 100 纳秒间隔测量的时间。
The code from your provided link Swift: convert NSDate to c# tickscan be translated to Swift easily:
您提供的链接Swift: convert NSDate to c# ticks 中的代码可以轻松转换为 Swift:
// Swift 2:
extension NSDate {
var ticks: UInt64 {
return UInt64((self.timeIntervalSince1970 + 62_135_596_800) * 10_000_000)
}
}
// Swift 3:
extension Date {
var ticks: UInt64 {
return UInt64((self.timeIntervalSince1970 + 62_135_596_800) * 10_000_000)
}
}
Example (Swift 3):
示例(Swift 3):
let ticks = Date().ticks
print(ticks) // 636110903202288256
or as a string:
或作为字符串:
let sticks = String(Date().ticks)
print(sticks)
And while are are at it, the reverse conversion from ticks to Date
would be
虽然是这样,但从滴答到的反向转换Date
将是
// Swift 2:
extension NSDate {
convenience init(ticks: UInt64) {
self.init(timeIntervalSince1970: Double(ticks)/10_000_000 - 62_135_596_800)
}
}
// Swift 3:
extension Date {
init(ticks: UInt64) {
self.init(timeIntervalSince1970: Double(ticks)/10_000_000 - 62_135_596_800)
}
}
Example (Swift 3):
示例(Swift 3):
let date = Date(ticks: 636110903202288256)
回答by Hermann Kuschke
Here is a solution that I find very elegant. I extended NSDate (although it's just Date now in Swift 3) to include a toMillis() func which will give you the Int64 value of the date object that you're working with.
这是一个我觉得非常优雅的解决方案。我扩展了 NSDate(尽管它现在在 Swift 3 中只是 Date)以包含一个 toMillis() 函数,它将为您提供您正在使用的日期对象的 Int64 值。
extension Date {
func toMillis() -> Int64! {
return Int64(self.timeIntervalSince1970 * 1000)
}
}
Usage:
用法:
let currentTimeStamp = Date().toMillis()
Cheers
干杯
回答by vaibhav
In Swift:
在斯威夫特:
if you want to makeit as global variable see below code:
如果要将其设为全局变量,请参阅以下代码:
var Timestamp: String {
return "\(NSDate().timeIntervalSince1970 * 1000)"
}
Then, you can call it
然后,你可以调用它
println("Timestamp: \(Timestamp)")
The *1000 is for miliseconds, if you'd prefer, you can remove that. If keep it as an NSTimeInterval.
*1000 表示毫秒,如果您愿意,可以删除它。如果将其保留为 NSTimeInterval。
var Timestamp: NSTimeInterval {
return NSDate().timeIntervalSince1970 * 1000
}
In Objective C:
在目标 C 中:
If your want to declare as symbolic constant see below code:
如果您想声明为符号常量,请参见以下代码:
#define TimeStamp [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] * 1000]
Then Call it like this:
然后像这样调用它:
NSString * timestamp = TimeStamp;
Or create a global method:
或者创建一个全局方法:
- (NSString *) timeStamp {
return [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] * 1000];
}
Have a Note:The 1000 is to convert the timestamp to milliseconds. You can remove this if you prefer your timeInterval in seconds.
有一个注意:1000 是将时间戳转换为毫秒。如果您更喜欢以秒为单位的 timeInterval,则可以删除它。