Question

Create timestamp of now as NSNumber in objective-c

How can I get timestamp as NSNumber? I only need something like this: 1232885280

 45  47533  45
1 Jan 1970

Solution

 128
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
// NSTimeInterval is defined as double
NSNumber *timeStampObj = [NSNumber numberWithDouble: timeStamp];
2010-08-10

Solution

 1

Time Intervals Since 1970

NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];

Time Intervals Since Now

NSTimeInterval timeStamp = [[NSDate date] timeIntervalSinceNow];

Time Intervals Since Date

NSDateComponents* comps = [[NSDateComponents alloc]init];
comps.year = 2014;
comps.month = 3;
comps.day = 31;
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDate* date = [calendar dateFromComponents:comps];

NSTimeInterval timeStamp = [[NSDate date] timeIntervalSinceDate:date];

Time Intervals Since Reference Date

The Intervals between the date object and 00:00:00 UTC on 1 january 2001.

NSTimeInterval timeStamp = [[NSDate date] timeIntervalSinceReferenceDate];
2020-12-09