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]]];

    //OR FOR BESPOKE FORMATTING
    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

Github

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
			//Past
		else
			//Future

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;

Comments

  1. Abhijeet Gite

    The GITHUB link is trashed. Can you provide an example to handle dates for To_date > start_date

  2. 徐小威

    NSDate * aDate = [NSDate dateWithString:@”2010-12-08 16:37:00 +0200″]
    I don’t think this can work on iOS now…