Current Unix Timestamp SECONDS SINCE JAN 01 1970

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

How to convert Unix timestamp to Datetime in Objective-C

🎈 🎈 🎈
+1

            #import <Foundation/Foundation.h>

            int main(int argc, const char * argv[]) {
                @autoreleasepool {
                    // Convert Unix timestamp to Date
                    NSTimeInterval unixTimestamp = 1730636448; // Unix timestamp
                    NSDate *date = [NSDate dateWithTimeIntervalSince1970:unixTimestamp];
            
                    // Create a date formatter
                    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            
                    // Convert the NSDate to a string
                    NSString *formattedDate = [dateFormatter stringFromDate:date];
                    NSLog(@"Converted Date: %@", formattedDate);
                }
                return 0;
            }            
        

Output:
Converted Date: 2024-11-03 12:20:48
Example only. There may be multiple ways to perform this operation.

Code Explanation

To convert a Unix timestamp to date in Objective-C, the Unix timestamp 1730636448 is passed to the dateWithTimeIntervalSince1970 method, which creates an NSDate object.
The NSDateFormatter class is then used to format the NSDate object into a string in the format yyyy-MM-dd HH:mm:ss.
The result, 2024-11-03 12:20:48, 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