Powered By Blogger

Saturday, November 24, 2012

how to use NSDateFormatter for different format?

 Hi,
Using the below category for NSDate, you can use below functions to get various date format string or date.

 #import <Foundation/Foundation.h>

@interface NSDate(TCUtils)

- (NSDate *)TC_dateByAddingCalendarUnits:(NSCalendarUnit)calendarUnit amount:(NSInteger)amount;
- (NSString*)TC_dateindisplayformat:(NSString*) format;
- (NSString*)TC_datewithTformat:(NSString*)dt format:(NSString*) format;
- (NSString*)TC_dateformat:(NSString*)dt format:(NSString*) format;
- (NSDate*)TC_stringformat:(NSString*)dt format:(NSString*) format;
- (NSString*)TC_date24format:(NSString*)dt format:(NSString*) format;
- (NSDate*)TC_datewithTformat:(NSString*)dt;
- (BOOL)isSameDay:(NSDate*)anotherDate;
- (NSDate *) todayDateWithoutTime;
@end



#import "NSDate+TCUtils.h"

@implementation NSDate (TCUtils)


- (NSDate *)TC_dateByAddingCalendarUnits:(NSCalendarUnit)calendarUnit amount:(NSInteger)amount {
    NSDateComponents *components = [[NSDateComponents alloc] init];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *newDate;
   
    switch (calendarUnit) {
        case NSSecondCalendarUnit:
            [components setSecond:amount];
            break;
        case NSMinuteCalendarUnit:
            [components setMinute:amount];
            break;
        case NSHourCalendarUnit:
            [components setHour:amount];
            break;
        case NSDayCalendarUnit:
            [components setDay:amount];
            break;
        case NSWeekCalendarUnit:
            [components setWeek:amount];
            break;
        case NSMonthCalendarUnit:
            [components setMonth:amount];
            break;
        case NSYearCalendarUnit:
            [components setYear:amount];
            break;
        default:
            ////NSLog(@"addCalendar does not support that calendarUnit!");
            break;
    }
   
    newDate = [gregorian dateByAddingComponents:components toDate:self options:0];
    [components release];
    [gregorian release];
    return newDate;
}

- (NSString*)TC_dateindisplayformat:(NSString*) format
{
    NSDateFormatter* df=[[[NSDateFormatter alloc] init] autorelease];
    [df setTimeZone:[NSTimeZone defaultTimeZone]];
    NSLocale *locale = [[[NSLocale alloc]
                         initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]; 
    [df setLocale:locale];
    [df setDateFormat:format];
    return [df stringFromDate:self];   
}

- (NSString*)TC_datewithTformat:(NSString*)dt format:(NSString*) format
{
    NSDateFormatter* df=[[[NSDateFormatter alloc] init] autorelease];
    [df setTimeZone:[NSTimeZone defaultTimeZone]];
    NSLocale *locale = [[[NSLocale alloc]
                         initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]; 
    [df setLocale:locale];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSArray* a=[dt componentsSeparatedByString:@"T"];
    NSDate* tdt;
    if([a count]>0){
        tdt=[df dateFromString:[a objectAtIndex:0]];
    }
    else{
        tdt=[NSDate date];
    }
    [df setDateFormat:format];
   
    return [df stringFromDate:tdt];
}

- (NSString*)TC_dateformat:(NSString*)dt format:(NSString*) format
{
    NSDateFormatter* df=[[[NSDateFormatter alloc] init] autorelease];
    [df setTimeZone:[NSTimeZone defaultTimeZone]];
    NSLocale *locale = [[[NSLocale alloc]
                         initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]; 
    [df setLocale:locale];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSDate* tdt=[df dateFromString:dt];
    [df setDateFormat:format];
   
    return [df stringFromDate:tdt];
}

- (NSDate*)TC_stringformat:(NSString*)dt format:(NSString*) format
{
    NSDateFormatter* df=[[[NSDateFormatter alloc] init] autorelease];
    [df setTimeZone:[NSTimeZone defaultTimeZone]];
    NSLocale *locale = [[[NSLocale alloc]
                         initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]; 
    [df setLocale:locale];
    [df setDateFormat:format];

    return [df dateFromString:dt];
}

- (NSDate*)TC_datewithTformat:(NSString*)dt
{
    NSDateFormatter* df=[[[NSDateFormatter alloc] init] autorelease];
    [df setTimeZone:[NSTimeZone defaultTimeZone]];
    NSLocale *locale = [[[NSLocale alloc]
                         initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
    [df setLocale:locale];
    dt=[dt stringByReplacingOccurrencesOfString:@"T" withString:@" "];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [df dateFromString:dt];
}

- (NSString*)TC_date24format:(NSString*)dt format:(NSString*) format
{
    NSDateFormatter* df=[[[NSDateFormatter alloc] init] autorelease];
    [df setTimeZone:[NSTimeZone defaultTimeZone]];
    NSLocale *locale = [[[NSLocale alloc]
                         initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]; 
    [df setLocale:locale];
    [df setDateFormat:@"yyyy-MM-dd hh:mm a"];
    NSDate* tdt=[df dateFromString:dt];
    [df setDateFormat:format];
   
    return [df stringFromDate:tdt];
}

- (NSDate *) todayDateWithoutTime
{
    NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
      [df setTimeZone:[NSTimeZone  defaultTimeZone]];
    NSLocale *locale = [[[NSLocale alloc]
                         initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
    [df setLocale:locale];
    [df setDateFormat:@"yyyy-MM-dd"];

    NSString *todayDateS = [df stringFromDate:[NSDate date]];
    NSDate *todayDate = [df dateFromString:todayDateS];
    return todayDate;
}



- (BOOL)isSameDay:(NSDate*)anotherDate{
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* components1 = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:self];
    NSDateComponents* components2 = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:anotherDate];
    return ([components1 year] == [components2 year] && [components1 month] == [components2 month] && [components1 day] == [components2 day]);
}

@end

No comments:

Post a Comment