3

I need a way to get the current date converted into epoch time e.g: 12343215325

I've tried so many things, but since im new i might be doing stupid stuff.

Any help is appreciated.

Thank you.

user3396301
  • 101
  • 1
  • 3
  • 8
  • possible duplicate of [Convert a NSDate to milliseconds epoch time](http://stackoverflow.com/questions/9321055/convert-a-nsdate-to-milliseconds-epoch-time) – jscs Apr 06 '14 at 01:45
  • 1
    Where is the difference to your previous question http://stackoverflow.com/questions/22885867/display-current-time-in-epoch ? It would be better to *clarify* your question (in particular because several people tried to answer it) instead of posting a new one. – Martin R Apr 06 '14 at 04:48

2 Answers2

18

This can be done with the NSDate class:

NSDate *now = [NSDate date];
NSTimeInterval nowEpochSeconds = [now timeIntervalSince1970];

Note that timeIntervalSince1970 is an instance method that returns an NSTimeInterval. However, if we look at the definition of NSTimeInterval:

typedef double NSTimeInterval;

So we simply have a double holding current seconds since 00:00:00 on 1 Jan 1970, so output format is the same as with a double format. This value is precise to sub-milliseconds so we can get milliseconds-epoch-time by multiplying by 1000.

Praveen-K
  • 3,401
  • 1
  • 23
  • 31
qqq
  • 1,360
  • 1
  • 9
  • 24
0

in c declare a tm structure struct tm yourtm; fill in the tm structure with the current local date and time. then call mktime ( &yourtm ) and it will return the number of seconds since Jan 1, 1970 in GMT.