NSDate objects are immutable.

	//Define a date variable
	NSDate *dateCreated;

	//Initialise it:
	dateCreated = [[NSDate alloc] init];

Getting Date Values

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"yyyy"];
    int year = [[dateFormatter stringFromDate:[NSDate date]] intValue];

    [dateFormatter setDateFormat:@"MM"];
    int month = [[dateFormatter stringFromDate:[NSDate date]] intValue];

    [dateFormatter setDateFormat:@"dd"];
    int day = [[dateFormatter stringFromDate:[NSDate date]] intValue];

    [dateFormatter setDateFormat:@"HH"];
    int hour = [[dateFormatter stringFromDate:[NSDate date]] intValue];

    [dateFormatter setDateFormat:@"mm"];
    int minute = [[dateFormatter stringFromDate:[NSDate date]] intValue];

    [dateFormatter setDateFormat:@"ss"];
    int second = [[dateFormatter stringFromDate:[NSDate date]] intValue];

    [dateFormatter release];

Getting String With Custom DateTime Format

	NSString *MyString;
	NSDate *now = [NSDate date];
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:@"yyyy-MM-dd-HH-mm-ss"];
	MyString = [dateFormatter stringFromDate:now];
	[dateFormatter release];

Convert To Universal Time Format String

Converting a DateTime to this format allows you to avoid local date time format issues when passing DateTime values between systems.

	NSString *MyString;
	NSDate *now = [NSDate date];
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
	MyString = [dateFormatter stringFromDate:now];
	[dateFormatter release];

Displaying Dates

Display The Time On A Label

	NSDate *now = [NSDate date];
	NSDateFormatter *formatter = nil;
	formatter = [[NSDateFormatter alloc] init];
	[formatter setTimeStyle:NSDateFormatterShortStyle];
	[timeLabel setText:[formatter stringFromDate:now]];
	[formatter release];
Formatting The Display Of A Date Variable

    NSDate *myDateVariable;

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];	//Not shown
    [someDateLabel setText:[dateFormatter stringFromDate:[myDateVariable]]];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"'Due' EEEE d MMM yyyy, h:mm a"];
    [someDateLabel setText:[dateFormatter stringFromDate:[myDateVariable]]];

    //Time using users 24/12 hour preference:
    [dateFormatter setDateFormat:@"HH:mm"];

    //Time using 12 hour AM/PM format:
    [dateFormatter setDateFormat:@"h:mm a"];

    //Day and date using abbreviated words:
    [dateFormatter setDateFormat:@"' ('EEE d MMM yyyy')'"];

    //Day and date using full words:
    [dateFormatter setDateFormat:@"' ('EEEE d MMMM yyyy')'"];

Manipulating Dates

Good resources


Adding seconds to the current date example

	MyTargetDateObject = [NSDate dateWithTimeIntervalSinceNow:60];
Adding 3 months and 4 days to the current date example

	NSDate *now = [NSDate date];
	NSDateComponents *dc = [[NSDateComponents alloc] init];
	[dc setMonth:3];
	[dc setDay:4];
	MyTargetDateObject = [[NSCalendar currentCalendar] dateByAddingComponents:dc toDate:now options:0];
	[dc release];
Compare Date

		NSTimeInterval dateTime = [ReminderDate.date timeIntervalSinceNow];    //Gets difference in seconds
		if (dateTime <= 0)    //Difference in seconds

Dates From Strings

NSDate * aDate = [NSDate dateWithString:@"2010-12-08 16:37:00 +0200"];

Break Date Time Into Components

            //Get number of days
            NSDate *now = [NSDate date];
            NSCalendar *sysCalendar = [NSCalendar currentCalendar];
            unsigned int unitFlags = NSDayCalendarUnit;     //Other usage:  =(NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit);
            NSDateComponents *breakdownInfo = [sysCalendar components:unitFlags fromDate:now  toDate:SomeOtherDate options:0];
            int days = [breakdownInfo day]; //Other usage: [breakdownInfo minute] [breakdownInfo hour] [breakdownInfo day] [breakdownInfo month];

Difference In Days Between 2 Dates

You need to compare NSDate objects with time part equals to 00:00:00. As NSDate objects are immutable do a conversion NSDate -> NSString -> NSDate and remove the time element

    //GET # OF DAYS
    NSDateFormatter *df = [NSDateFormatter new];
    [df setDateFormat:@"dd MM yyyy"];       //Remove the time part
    NSString *TodayString = [df stringFromDate:[NSDate date]];
    NSString *TargetDateString = [df stringFromDate:SomeDate];
    NSTimeInterval time = [[df dateFromString:TargetDateString] timeIntervalSinceDate:[df dateFromString:TodayString]];
    [df release];
    int days = time / 60 / 60/ 24;

We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.

Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>