Thursday, June 9, 2011

How to use NSDateFormatter in Objective-C ?


       NSDate *date = [NSDate date];
    NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
    [_formatter setDateFormat:@"dd-MM-yyyy"];
    NSString *_date=[_formatter stringFromDate:date];

       NSDate *date = [NSDate date];
    NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
    [_formatter setDateFormat:@"HH.mm.ss"];
    NSString *_time=[_formatter stringFromDate:date];



Given above 2  examples above display how to get current Date in format specified by NSDateFormatter  f and how to get current Time in format specified. You will get the output looking something like 13-05-2011 for the first piece of code(Note that date will be the today's date) and second code is giving out the time as 09.21.33 (hour, mins then followed by seconds).

No comments:

Post a Comment