Shows seconds, minutes and hours that I have been alive.
[code]#import <Foundation/Foundation.h>
// Function Prototype
NSString *prettyDateAndTime (NSDate *datetime);
int main(int argc, const char * argv[]) {
@autoreleasepool {
// Date and Time right now
NSDate *now = [[NSDate alloc]init];
NSDateComponents *comps = [[NSDateComponents alloc]init];
[comps setYear:1978];
[comps setMonth:7];
[comps setDay:21];
[comps setHour:07];
[comps setMinute:00];
[comps setSecond:00];
NSCalendar *g = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// Create an instance of NSDate initialize with values from comps instance
NSDate *dateOfBirth = [g dateFromComponents:comps];
double secondsSinceEarlierDate = [now timeIntervalSinceDate:dateOfBirth];
// Display my date of birth in the local time zone rather than UTC
NSLog(@"Born on %@", prettyDateAndTime(dateOfBirth));
// Shows how long I been alive for seconds, minutes and hours.
NSLog(@"Been alive for %f seconds, %f minutes, %f hours",
secondsSinceEarlierDate, secondsSinceEarlierDate / 60,
(secondsSinceEarlierDate / 60) / 60);
}
return 0;
}
/*************************************************************************************/
// A function that displays the date and time in local time when using NSLog rather than UTC
NSString *prettyDateAndTime (NSDate *datetime)
{
NSDateFormatter *prettyDateFormat = [[NSDateFormatter alloc] init];
[prettyDateFormat setTimeStyle:NSDateFormatterMediumStyle];
[prettyDateFormat setDateStyle:NSDateFormatterMediumStyle];
[prettyDateFormat setLocale:[NSLocale currentLocale]];
return [prettyDateFormat stringFromDate];
}[/code]