Ad space (banner)
๐ŸŽObjective-C Lessons
Lesson 36 / 68

Assembling and displaying a date (NSDateComponents)

This lesson covers the basics of date handling with NSDateComponents in Objective-C, with the aim of assembling a date and finding its day of the week.

NSDateComponents lets you assemble and handle a specific date. Getting the day of the week and changing the display format are both straightforward in combination with NSCalendar.

The sample code sets the year, month, and day properties of an NSDateComponents and assembles the date through NSCalendar. Note that getting the weekday uses a separate calendar method.

A common early stumble is how many date-related classes there are. NSDate, NSDateComponents, NSCalendar, NSDateFormatter — you combine several depending on the job, which is confusing at first.

In professional work, dates are needed in almost every application — showing when something was posted, calculating a deadline.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Objective-C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)