Friday, May 10, 2013

NSDateFormatter setLocale example ios


setLocale:

Sets the locale for the receiver.
- (void)setLocale:(NSLocale *)locale
Parameters
locale
The locale for the receiver.


Example of [NSDateFormatter setLocale]

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *date = [NSDate date];
NSString *dateString = [dateFormatter stringFromDate:date];

Example of [NSDateFormatter setLocale]
-(NSString *) stringFromDate:(NSDate *) date{

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];

[dateFormatter setLocale:[NSLocale currentLocale]];

NSString *dateString = [dateFormatter stringFromDate:date];

[dateFormatter release];

return dateString;
}

Example of [NSDateFormatter setLocale]
-(NSDate *) dateFromString:(NSString *) dateInString{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];

[dateFormatter setLocale:[NSLocale currentLocale]];

NSDate *dateFromString = [dateFormatter dateFromString:dateInString];

[dateFormatter release];

return dateFromString;
}