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?
Can also use
for timestamp in seconds.
To get timestamp from NSDate Swift 3
To use
It's convenient to define a macro for get current timestamp
Then you can use
let timestamp = Constant.Time.now()
Here's what I use:
(times 1000 for milliseconds, otherwise, take that out)
If You're using it all the time, it might be nice to declare a macro
Then Call it like this:
Or as a method:
As TimeInterval
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:
Then, you can call it
Again, the
*1000
is for miliseconds, if you'd prefer, you can remove that. If you want to keep it as anNSTimeInterval
Declare these outside of the context of any class and they'll be accessible anywhere.
If you'd like to call this method directly on an NSDate object and get the timestamp as a string in milliseconds without any decimal places, define this method as a category:
Swift:
I have a UILabel which shows TimeStamp over a Camera Preview.
I am setting up a setting Button to trigger a alertView.
}