How To Become A Programmer Without A Degree & Experience

[ iOS ] How to get date from NSDate

iOS (Objective-C) Tutorial – NSDate –

iOS (Objective-C) Tutorial  - NSDate -




Today, I’ll introduce how to use NSDate by creating a simple app which displays year, month, date and day.

I set font and colors as follows. (You can check them in the video.)



2016Avenir Heavy (22)titleLabel
JuneAvenir Heavy (24)monthLabel
19Avenir Heavy (46)dateLabel
SunAvenir Heavy (18)dayLabel





Hope you enjoy coding 🙂


Sample Code

//  ViewController.h
//  NSDate
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *yearLabel;
@property (weak, nonatomic) IBOutlet UILabel *monthLabel;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UILabel *dayLabel;
//  ViewController.m
//  NSDate
#import "ViewController.h"
@interface ViewController ()
@implementation ViewController
@synthesize yearLabel,monthLabel,dateLabel,dayLabel;
- (void)viewDidLoad {
    [super viewDidLoad];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // Set your locale identifier. en_US, en_GB, fr_FR, de_DE, ja_JP etc...
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [formatter setLocale:locale];
    [formatter setDateFormat:@"y,MMMM,d,E"];
    // Date to string
    NSDate *now = [NSDate date];
    NSString *stringDate = [formatter stringFromDate:now];
    // Split stringDate by a comma.
    NSArray *values = [stringDate componentsSeparatedByString:@","];
    yearLabel.text = [values objectAtIndex:0];
    monthLabel.text = [values objectAtIndex:1];
    dateLabel.text = [values objectAtIndex:2];
    dayLabel.text = [values objectAtIndex:3];