Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Convert Datetime to Unix Timestamp in Objective-C

How to convert Datetime to Unix timestamp in Objective-C

🎈 🎈 🎈
+1

    #import <Foundation/Foundation.h>

    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            // Create a date formatter
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
            // Convert Date to Unix timestamp
            NSString *dateString = @"1975-02-11 15:56:58";
            NSDate *date = [dateFormatter dateFromString:dateString];
    
            // Get the Unix timestamp
            NSTimeInterval unixTimestamp = [date timeIntervalSince1970];
            NSLog(@"Unix Timestamp: %.0f", unixTimestamp);
        }
        return 0;
    }            
        

Output:
Unix Timestamp: 161366218
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this example, the NSDateFormatter is used to convert a date string ('1975-02-11 15:56:58') into an NSDate object. The timeIntervalSince1970 method is then called on the NSDate object to return the number of seconds since January 1, 1970 (Unix epoch).
The result is the Unix timestamp '161366218', which is printed using NSLog.



Other useful Objective-C date functions

Function Description Example
NSDate date Returns the current date and time as an NSDate object. [NSDate date] returns 2024-08-28 12:34:56
NSDate dateWithTimeIntervalSince1970() Creates an NSDate object from a Unix timestamp (seconds since 1970). [NSDate dateWithTimeIntervalSince1970:1589530245] returns 2020-05-15 08:10:45
NSDateFormatter dateFromString() Converts a date string into an NSDate object based on a specified format. [dateFormatter dateFromString:@"2020-05-15 08:10:45"] returns an NSDate object.
NSDateFormatter stringFromDate() Converts an NSDate object to a formatted date string. [dateFormatter stringFromDate:date] returns 2020-05-15 08:10:45
timeIntervalSince1970() Returns the Unix timestamp for an NSDate object. [date timeIntervalSince1970] returns 1589530245
NSDateFormatter setDateFormat() Sets the format for date parsing and formatting. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]
NSDateFormatter setTimeZone() Sets the time zone for the date formatter. [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]
NSCalendar currentCalendar() Returns the current calendar used by the system. [NSCalendar currentCalendar]
NSDateComponents year, month, day, hour, minute, second Accesses the individual components of a date (year, month, day, hour, etc.). components.year returns the year component of a date.
Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder