Objective C Tutorial - Objective C NSDate






Create Today's Date

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
        
        NSDate *todaysDate = [NSDate date];
        
        NSLog(@"Today's date is %@", todaysDate);
    return 0;
}

The code above generates the following result.





Create Custom Dates

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

        NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
        dateComponents.year = 2007;
        dateComponents.month = 6;
        dateComponents.day = 29;
        dateComponents.hour = 12;
        dateComponents.minute = 01;
        dateComponents.second = 31;
        dateComponents.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"PDT"];
        
        NSDate *iPhoneReleaseDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
        
        NSLog(@"The original iPhone went on sale: %@", iPhoneReleaseDate);
        
    return 0;
}

The code above generates the following result.





Comparing Dates

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

        NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
        dateComponents.year = 2007;
        dateComponents.month = 6;
        dateComponents.day = 29;
        dateComponents.hour = 12;
        dateComponents.minute = 01;
        dateComponents.second = 31;
        dateComponents.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"PDT"];
        
        NSDate *iPhoneReleaseDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
        
        NSLog(@"The original iPhone went on sale: %@", iPhoneReleaseDate);
        
        NSDate *todaysDate = [NSDate date];
        
        NSLog(@"Today's date is: %@", todaysDate);
        
        if([todaysDate isEqualToDate:iPhoneReleaseDate])
            NSLog(@"The iPhone was released today!");
        else
            NSLog(@"The iPhone was released on some other date");
        
        NSDate *earlierDateIs = [todaysDate earlierDate:iPhoneReleaseDate];
        
        NSLog(@"The earlier date is: %@", earlierDateIs);
        
        NSDate *laterDateIs = [todaysDate laterDate:iPhoneReleaseDate];
        
        NSLog(@"The later date is: %@", laterDateIs);
        
        NSTimeInterval timeBetweenDates = [todaysDate timeIntervalSinceDate:iPhoneReleaseDate];
        
        NSLog(@"The iPhone was released %f seconds ago", timeBetweenDates);
        
        NSCalendar *systemCalendar = [NSCalendar currentCalendar];
        
        unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit| NSDayCalendarUnit;

        NSDateComponents *dateComparisonComponents = [systemCalendar components:unitFlags
                                                             fromDate:iPhoneReleaseDate
                                                               toDate:todaysDate
                                                              options:NSWrapCalendarComponents];
        
        NSLog(@"The iPhone was released %ld years, %ld months and %ld days ago",
              dateComparisonComponents.year,
              dateComparisonComponents.month,
              dateComparisonComponents.day
              );
        
    return 0;
}

The code above generates the following result.

Create Dates From Strings

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

       
        NSString *dateString = @"02/14/2012";
        
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        
        df.dateFormat = @"MM/dd/yyyy";
        
        NSDate *valentinesDay = [df dateFromString:dateString];
        
        NSLog(@"Valentine's Day = %@", valentinesDay);

        
    
    return 0;
}

Format Dates

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

        
        NSString *dateString = @"02/14/2012";
        
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        
        df.dateFormat = @"MM/dd/yyyy";
        
        NSDate *valentinesDay = [df dateFromString:dateString];
        
        NSLog(@"Unformatted Valentine's Day = %@", valentinesDay);
        
        NSLog(@"Formatted Valentine's Day = %@", [df stringFromDate:valentinesDay]);
        
        df.dateFormat = @"EEEE, MMMM d";
        
        NSLog(@"Another Formatted Valentine's Day = %@", [df stringFromDate:valentinesDay]);
        
    return 0;
}

Adding and Subtracting Dates


#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
        NSString *dateString = @"02/14/2012";
        
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        
        df.dateFormat = @"MM/dd/yyyy";
        
        NSDate *valentinesDay = [df dateFromString:dateString];
        
        NSLog(@"Valentine's Day = %@", valentinesDay);
        
        NSDateComponents *weekBeforeDateComponents = [[NSDateComponents alloc] init];
        
        weekBeforeDateComponents.week = -1;
        
        NSDate *vDayShoppingDay = [[NSCalendar currentCalendar] dateByAddingComponents:weekBeforeDateComponents
                                                                                toDate:valentinesDay
                                                                               options:0];
        NSLog(@"Shop for Valentine's Day by %@", vDayShoppingDay);
        
    return 0;
}