Date Formate & Get Date


Date Formatter
NSDate *date = [NSDate date];
NSLog(@"date: %@",date);

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];

//Convert Date to String
NSString *strDate = [dateFormatter stringFromDate:date];
NSLog(@"strDate: %@",strDate);

//Convert String to Date
NSDate *ConvertDate = [dateFormatter dateFromString:strDate];
NSLog(@"ConvertDate: %@",ConvertDate);


Only Get EndDate
NSDate *StartDate = [NSDate date];

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:15];

NSDate *EndDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:StartDate options:0];

NSLog(@"StartDate: %@",StartDate);
NSLog(@"EndDate: %@",EndDate);


List of All Dates between Two Date
NSMutableArray *result = [NSMutableArray array];
NSDate *StartDate = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];

NSDateComponents *comps = [cal components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)fromDate:StartDate];

NSDate *date = [cal dateFromComponents:comps];
NSLog(@"date: %@",date);

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];

for (int i = 0 ; i < 15; i ++)
{
    NSString *strDate = [dateFormatter stringFromDate:date];    
    [result addObject:strDate];
    [comps setDay:(comps.day + 1)];
    date = [cal dateFromComponents:comps];
}
NSLog(@"Result: %@",result);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s