Tag Archives: NSDate

In this series I’ll show you how to create a simple Day Counter on iOS, using Objective-C and Xcode 9. The idea is to set a date in a settings screen, and then see how many days have elapsed on the main screen right after launching the app.

This is a 3-Part Mini-Series:

Part 1 is all about building the interface in Interface Builder

Part 2 is about coding the NSDate subtraction methods, using NSCalendar and loading/saving data using NSUserDefaults

Part 3 will introduce Key/Value Observing to update the first view controller as soon as the date is changed in the settings and deals with how to normalise an NSDate object.

Share this:

In this series I’ll show you how to create a simple Day Counter on iOS, using Objective-C and Xcode 9. The idea is to set a date in a settings screen, and then see how many days have elapsed on the main screen right after launching the app.

This is a 3-Part Mini-Series:

Part 1 is all about building the interface in Interface Builder

Part 2 is about coding the NSDate subtraction methods, using NSCalendar and loading/saving data using NSUserDefaults

Part 3 will introduce Key/Value Observing to update the first view controller as soon as the date is changed in the settings and deals with how to normalise an NSDate object.

Share this:

In this series I’ll show you how to create a simple Day Counter on iOS, using Objective-C and Xcode 9. The idea is to set a date in a settings screen, and then see how many days have elapsed on the main screen right after launching the app.

This is a 3-Part Mini-Series:

Part 1 is all about building the interface in Interface Builder

Part 2 is about coding the NSDate subtraction methods, using NSCalendar and loading/saving data using NSUserDefaults

Part 3 will introduce Key/Value Observing to update the first view controller as soon as the date is changed in the settings and deals with how to normalise an NSDate object.

Share this:

Here’s how we can do this, with the help of our old friend the NSDateFormatter:

Objective-C

1

2

3

4

5

6

7

8

9

10

11

12

// here we have a date

NSString*dateString=@"11/01/1989";

// convert it into an NSDate object

NSDateFormatter*formatter=[[NSDateFormatteralloc]init];

[formatter setDateFormat:@"dd/MM/yyyy"];

NSDate*theDate=[formatter dateFromString:dateString];

// so what is that?

NSLog(@"Your Date Object is %@",theDate);

// Your Date Object is 1989-01-11 05:00:00 +0000

Note the lower case yyyy. In many cases, YYYY and yyyy will yield the same result, but there is a difference:

Objective-C

1

2

3

[formatter setDateFormat:@"dd/MM/yyyy"];

// Your Date Object is 1988-12-25 05:00:00 +0000

As Apple put it:

It uses yyyy to specify the year component. A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.