What is the Objective-c equivalent of java timestamp?

I did not find the answer to this question anywhere, but this seems like a typical problem: I have Objective-C "NSDate timestamp" that looks like this: "2010-07-14 16:30:41 +0200". The java timestamp is just a long integer (ex: "976712400000").

So my question is: what is the Objective-C equivalent of java timestamp?

Thanks in advance for your help.

+4
source share
2 answers

You can convert the format that NSDAte gives you unix time by subtracting the original date of unix time, which is January 1, 1970. NSTimeInterval is just the difference between two dates, and you can get it in a few seconds:

NSDate * past = [NSDate date]; NSTimeInterval oldTime = [past timeIntervalSinceDate:[NSDate dateWithNaturalLanguageString:@"01/01/1970"]]; NSString * unixTime = [[NSString alloc] initWithFormat:@"%0.0f", oldTime]; 
+4
source

Although @lordsandwich's answer is correct, you can also directly use the NSDate timeIntervalSince1970 method instead of creating the 1970 NSDate .

This will work as follows:

 NSDate *past = [NSDate date]; NSTimeInterval oldTime = [past timeIntervalSince1970]; NSString *unixTime = [[NSString alloc] initWithFormat:@"%0.0f", oldTime]; 

As with using this, you do not unnecessarily add a new object to the autorun pool, I think that it is actually better to use this method.

+17
source

Source: https://habr.com/ru/post/1315792/


All Articles