Showing posts with label NSDateFormatter. Show all posts
Showing posts with label NSDateFormatter. Show all posts

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).

Convert TimeStamp to NSDate in Objective-C

Here is a code piece which lets you convert the unix type timestamp to Formatted Date


    
    NSString * timeStampString =@"1304245000";
    NSTimeInterval _interval=[timeStampString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
    [_formatter setDateFormat:@"dd.MM.yyyy"];
    NSString *_date=[_formatter stringFromDate:date];


This will print  _date something like 05.03.2011