Skip to content

Instantly share code, notes, and snippets.

@codingwithsara
Created November 8, 2018 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codingwithsara/de526de6b1e6768fe0d126e1b540dfaf to your computer and use it in GitHub Desktop.
Save codingwithsara/de526de6b1e6768fe0d126e1b540dfaf to your computer and use it in GitHub Desktop.
[ iOS ] How to get date from NSDate
//
// ViewController.m
// NSDate
//
#import "ViewController.h"
@interface ViewController ()
@end
@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];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment