以时间戳格式获取当前NSDate [英] Get current NSDate in timestamp format
问题描述
我有一个基本的方法,获取当前时间并将其设置为字符串。然而,我怎么能得到它格式化当前日期&时间在UNIX自1970年以来的时间戳格式?
这是我的代码:
NSDate * currentTime = [NSDate date];
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@hh-mm];
NSString * resultString = [dateFormatter stringFromDate:currentTime];
是否可以使用 NSDateFormatter
'resultString'into a timestamp?
这里是我使用的:
NSString * timestamp = [NSString stringWithFormat:@%f,[[NSDate date] timeIntervalSince1970] * 1000];
(以毫秒为单位乘以1000,否则取出)
如果你一直使用它,那么可以声明一个宏。
#define TimeStamp [NSString stringWithFormat:@%f,[[NSDate date] timeIntervalSince1970] * 1000]
然后调用它像这样:
NSString * timestamp = TimeStamp;
或作为方法:
- (NSString *)timeStamp {
return [NSString stringWithFormat:@%f,[[NSDate date] timeIntervalSince1970] * 1000];
}
As TimeInterval
- (NSTimeInterval)timeStamp {
return [[NSDate date] timeIntervalSince1970] * 1000;
}
注意:
1000是将时间戳转换为毫秒。
如果你想在Swift中使用全局变量,可以使用:
var Timestamp:String {
return \(NSDate()。timeIntervalSince1970 * 1000)
}
可以调用它
println(Timestamp:\(Timestamp))
同样, * 1000
是毫秒,如果您愿意, 。如果你想保持它为 NSTimeInterval
var Timestamp:NSTimeInterval {
return NSDate()。timeIntervalSince1970 * 1000
}
的任何类的上下文,他们将随时随地可以访问。
I have a basic method which gets the current time and sets it in a string. However, how can I get it to format the current date & time in a UNIX since-1970 timestamp format?
Here is my code:
NSDate *currentTime = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh-mm"];
NSString *resultString = [dateFormatter stringFromDate: currentTime];
Is it possible to use NSDateFormatter
to change the 'resultString' into a timestamp?
Here's what I use:
NSString * timestamp = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] * 1000];
(times 1000 for milliseconds, otherwise, take that out)
If You're using it all the time, it might be nice to declare a macro
#define TimeStamp [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] * 1000]
Then Call it like this:
NSString * timestamp = TimeStamp;
Or as a method:
- (NSString *) timeStamp {
return [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] * 1000];
}
As TimeInterval
- (NSTimeInterval) timeStamp {
return [[NSDate date] timeIntervalSince1970] * 1000;
}
NOTE:
The 1000 is to convert the timestamp to milliseconds. You can remove this if you prefer your timeInterval in seconds.
Swift
If you'd like a global variable in Swift, you could use this:
var Timestamp: String {
return "\(NSDate().timeIntervalSince1970 * 1000)"
}
Then, you can call it
println("Timestamp: \(Timestamp)")
Again, the *1000
is for miliseconds, if you'd prefer, you can remove that. If you want to keep it as an NSTimeInterval
var Timestamp: NSTimeInterval {
return NSDate().timeIntervalSince1970 * 1000
}
Declare these outside of the context of any class and they'll be accessible anywhere.
这篇关于以时间戳格式获取当前NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!