0
A
回答
2
我借用了這段代碼,所以我不是作者。這裏有雲:
@implementation NSDate (Ticks)
- (long long) ticks
{
double tickFactor = 10000000;
long long tickValue = (long long)floor([self timeIntervalSince1970] * tickFactor) + 621355968000000000LL;
return tickValue;
}
+ (NSDate*) dateWithTicks:(long long)ticks
{
double tickFactor = 10000000;
double seconds = (ticks - 621355968000000000LL)/tickFactor;
return [NSDate dateWithTimeIntervalSince1970:seconds];
}
@end
1
的OP標記他與斯威夫特的問題,所以這裏是一個備選答案,但它基本上是一樣的尼古拉已經發布。但是,此版本添加了將DateTime.MinValue和DateTime.MaxValue映射到Date.distantPast和Date.distantFuture的支持。
private static let CTicksAt1970 : Int64 = 621_355_968_000_000_000
private static let CTicksPerSecond : Double = 10_000_000
private static let CTicksMinValue : Int64 = 0
private static let CTicksMaxValue : Int64 = 3_155_378_975_999_999_999
// Method to create a Swift Date struct to reflect the instant in time specified by a "ticks"
// value, as used in .Net DateTime structs.
internal static func swiftDateFromDotNetTicks(_ dotNetTicks : Int64) -> Date {
if dotNetTicks == CTicksMinValue {
return Date.distantPast
}
if dotNetTicks == CTicksMaxValue {
return Date.distantFuture
}
let dateSeconds = Double(dotNetTicks - CTicksAt1970)/CTicksPerSecond
return Date(timeIntervalSince1970: dateSeconds)
}
// Method to "convert" a Swift Date struct to the corresponding "ticks" value, as used in .Net
// DateTime structs.
internal static func dotNetTicksFromSwiftDate(_ swiftDate : Date) -> Int64 {
if swiftDate == Date.distantPast {
return CTicksMinValue
}
if swiftDate == Date.distantFuture {
return CTicksMaxValue
}
let dateSeconds = Double(swiftDate.timeIntervalSince1970)
let ticksSince1970 = Int64(round(dateSeconds * CTicksPerSecond))
return CTicksAt1970 + ticksSince1970
}
相關問題
- 1. SWIFT將NSTimeInteval轉換爲NSDate
- 2. 如何將MYSQL DateTime轉換爲NSDate - Swift
- 3. 將Objective-C轉換爲Swift
- 4. 將NSDate的描述轉換爲NSDate?
- 5. 將DateTime轉換爲.NET中的ticks?
- 6. Swift 2 - 將objective-C#define轉換爲swift
- 7. 將NSString轉換爲NSDate
- 8. 將NSDateComponents轉換爲NSDate
- 9. 將NSString轉換爲NSDate
- 10. 將NSString轉換爲NSDate
- 11. Swift將特定格式的字符串轉換爲NSDate
- 12. SWIFT:將字符串從XLM轉換爲NSDate
- 13. 將NSDate轉換爲SWIFT中特定時區的字符串
- 14. 如何將字符串轉換爲Swift中的NSDate?
- 15. 試圖將Swift中的Firebase時間戳轉換爲NSDate
- 16. 如何將「2015年9月6日」轉換爲Swift中的NSDate()?
- 17. 在iOS Swift中將NSDate轉換爲字符串
- 18. 將Objective-C中的rangeOfString轉換爲Swift
- 19. 將objective-c方法轉換爲swift [updateSearchResultsForSearchController]
- 20. 將Objective-C塊轉換爲Swift
- 21. 將PinchRecognizer Objective-C代碼轉換爲Swift
- 22. 將Objective-C語法轉換爲Swift 3
- 23. 將Objective-C方法轉換爲Swift
- 24. 將C#JSON POST轉換爲SWIFT 3
- 25. 將Objective-C代碼轉換爲Swift
- 26. ZXingObjc - 將對象c轉換爲swift
- 27. 將NSRangePointer從Objective-C轉換爲Swift
- 28. 從swift轉換爲objective-c?
- 29. 如何將UTC日期轉換爲NSDate?
- 30. 將NSDate轉換爲JSON日期?
'timeIntervalSinceReferenceDate'返回2001年1月1日的日期對象和UTC 00:00:00之間的時間間隔(只讀) – zcui93
你的問題是_really_ vauge不清。你可以請**更多**具體? [mcve]會很好.. –