-
-
Save codingwithsara/de526de6b1e6768fe0d126e1b540dfaf to your computer and use it in GitHub Desktop.
[ iOS ] How to get date from NSDate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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