How to convert date string to string (yyyy-MM-dd). Doing this am I getting null values?

I have data like customerFromDate "Apr 01 2010" and customerToDate Apr 30 2010 "which is a string.

I want to convert this format to the string "yyyy-MM-dd", but in doing so I got null values. Please have a look at the following code I've tried.

printf("\n customerFromDate %s",[customerStatementObj.customerFromDate UTF8String]);
printf("\n customerToDate %s",[customerStatementObj.customerToDate UTF8String]);
/*
 prints as the following 
 customerFromDate 01 Apr 2010
 customerToDate 30 Apr 2010
 */

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *fromDate=[[NSDate alloc]init];
fromDate = [dateFormatter dateFromString:customerStatementObj.customerFromDate];
printf("\n fromDate: %s",[fromDate.description UTF8String]);
NSString *fromDateString=[dateFormatter stringFromDate:fromDate];
printf("\n fromDateString: %s",[fromDateString UTF8String]);    
[dateFormatter release];

NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc]init];
[dateFormatter1 setDateFormat:@"yyyy-MM-dd"];
NSDate *toDate=[[NSDate alloc]init];
toDate = [dateFormatter1 dateFromString:customerStatementObj.customerToDate];
printf("\n toDate: %s",[toDate.description UTF8String]);
NSString *toDateString=[dateFormatter1 stringFromDate:toDate];
printf("\n toDateString: %s",[toDateString UTF8String]);            
[dateFormatter1 release];

      

Thank you Madan Mohan.

+2


a source to share


1 answer


A few notes:

You need two different NSDateFormatters. One that defines the format of the input date and one that defines the format of the output date.

    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:@"dd MMM yyyy"];
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:@"yyyy-MM-dd"];

      



You can reuse these formats for your fromDate

and toDate

.

Second, it dateFromString:

returns a dedicated, auto-implemented NSDate object. You are missing out on the ones that you manually highlight.

#import <Cocoa/Cocoa.h>

int main (int argc, char const *argv[])
{
    NSAutoreleasePool *pool = [NSAutoreleasePool new];

    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:@"dd MMM yyyy"];
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:@"yyyy-MM-dd"];

    NSDate *fromDate = [inputFormatter dateFromString:@"01 Apr 2010"];
    NSDate *toDate = [inputFormatter dateFromString:@"30 Apr 2010"];

    printf("\n fromDate: %s",[fromDate.description UTF8String]);
    NSString *fromDateString=[outputFormatter stringFromDate:fromDate];
    printf("\n fromDateString: %s",[fromDateString UTF8String]);

    printf("\n toDate: %s",[toDate.description UTF8String]);
    NSString *toDateString=[outputFormatter stringFromDate:toDate];
    printf("\n toDateString: %s",[toDateString UTF8String]);

    [inputFormatter release];
    [outputFormatter release];

    [pool drain];
    return 0;
}

      

+4


a source







All Articles