swift中PHP`time()`的等价函数是什么?

时间:2016-01-26 02:09:35

标签: swift time

swift中PHP using namespace System; using namespace System::Reflection; Assembly^ MyResolveEventHandler(Object^ sender, ResolveEventArgs^ args) { if (args->Name == "CodeToCall, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null") return Assembly::LoadFrom("C:\\Program Files (x86)\\Sigma\\CodeToCall.dll"); else return nullptr; } void UseClass1() { auto obj = gcnew CodeToCall::Class1; obj->Some(); } int main(array<System::String ^> ^args) { AppDomain::CurrentDomain->AssemblyResolve += gcnew ResolveEventHandler(MyResolveEventHandler); // You can't directly use Class1 in the same method/function // that you use to subscribe to AssemblyResolve. UseClass1(); Console::ReadKey(); return 0; } 的等效功能是什么?

我使用的是NSDate,但它不像PHP的time()

time()

2 个答案:

答案 0 :(得分:1)

您可以使用NSDateFormatter实例来自定义NSDate对象的显示,甚至可以构建NSDate实例。

let myDateFomratter = NSDateFormatter()
myDateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss"

myDateFormatter.stringFromDate(NSDate())

这只是一个非常快速的例子,NSDateFormatter是一个很棒的类,具有很大的灵活性。查看相关文档。

编辑,这是类ref: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/

您甚至可以从预定义的日期和时间中进行选择。时间样式以及确保它匹配用户所在的任何语言环境。

如果您需要自1970年以来的秒数@MikeG建议使用:

的NSDate()。timeIntervalSince1970

您可以使用NSDateFormatter从字符串创建日期,然后获取这样的值,甚至使用格式化程序和NSDateComponents构建日期,https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/

以下是如何从没有小数的纪元获取间隔,然后将其用作日期:

let aDate = (NSDate().timeIntervalSince1970 as NSNumber).integerValue

NSDate(timeIntervalSince1970: NSTimeInterval(aDate))

答案 1 :(得分:0)

如果您想获得自1970年以来经过的秒数,您可以致电:

var secondsSince1970 = NSDate().timeIntervalSince1970 

如果您希望它返回一个整数,您可以使用:

var secondsSince1970 = Int(NSDate().timeIntervalSince1970) 

@ thefredelment的答案更完整。