Tuesday 31 January 2012

Convert_stringToDate,DateToString...


-(NSDate *)convertStringToDate:(NSString *) date {
        NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];

//This bellow 3 lines for system date issue
    NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    assert(enUSPOSIXLocale != nil);
    
    [formatter setLocale:enUSPOSIXLocale];

    [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

        NSDate *nowDate = [[[NSDate alloc] init] autorelease];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];// set format here which format in string date
        date = [date stringByReplacingOccurrencesOfString:@"+0000" withString:@""];
        nowDate = [formatter dateFromString:date];
        // NSLog(@"date============================>>>>>>>>>>>>>>> : %@", nowDate);
        return nowDate;
}

    NSString *str = @"Tue, 24 May 2011 15:42:01 +0000";
    
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];

//This bellow 3 lines for system date issue
    NSLocale *enUSPOSIXLocale = [[NSLocale allocinitWithLocaleIdentifier:@"en_US_POSIX"];
    assert(enUSPOSIXLocale != nil);
    
    [formatter setLocale:enUSPOSIXLocale];

    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    [dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss z"];

    NSDate *date = [dateFormatter dateFromString: str];
    
    dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];
    
    NSString *convertedString = [dateFormatter stringFromDate:date];
    NSLog(@"Converted String : %@",convertedString);




    NSLog(@"Date String Arrived =====>>>%@",strDate);
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];

//This bellow 3 lines for system date issue
    NSLocale *enUSPOSIXLocale = [[NSLocale allocinitWithLocaleIdentifier:@"en_US_POSIX"];
    assert(enUSPOSIXLocale != nil);
    
    [dateFormatter setLocale:enUSPOSIXLocale];


    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    [dateFormatter setDateFormat:@"dd-MM-yyyy hh:mm:ss a"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    NSLog(dateString); 


No comments:

Post a Comment