Steaphann
Steaphann

Reputation: 2777

NSDate is not transformed correctly

I am trying to transform a datestring into another format. I am doing it like this.

NSLog(@"datestring is NOW %@",_dayObject.p_date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-DD"];
NSDate *date = [dateFormat dateFromString:_dayObject.p_date];
NSLog(@"date transformed %@",date);
[dateFormat setDateFormat:@"EEEE, dd/MM/YYYY"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSString *dateStr = [dateFormat stringFromDate:date];
NSLog(@"datestring is AFTER %@",dateStr);

But this is what I get from my NSLOGs

2013-04-18 08:43:36.181 mosaqua2[9629:907] datestring is NOW 2013-05-04
2013-04-18 08:43:36.184 mosaqua2[9629:907] date transformed 2013-01-03 23:00:00 +0000
2013-04-18 08:43:36.184 mosaqua2[9629:907] datestring is AFTER donderdag, 03/01/2013

What I want it to be is

Saterday, 04/05/2013

Can anybody help me ?

Upvotes: 1

Views: 194

Answers (6)

Rushi
Rushi

Reputation: 4500

Change this : "YYYY"

To : "yyyy"

Reason for the error,

It's a common mistake to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.

Upvotes: 0

βhargavḯ
βhargavḯ

Reputation: 9836

You are doing all right except setTimeZone , comment that you will get your output.

NSLog(@"datestring is NOW %@",_dayObject.p_date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-DD"];
NSDate *date = [dateFormat dateFromString:_dayObject.p_date];
NSLog(@"date transformed %@",date);
[dateFormat setDateFormat:@"EEEE, dd/MM/YYYY"];
//[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; //NOT NEEDED
NSString *dateStr = [dateFormat stringFromDate:date];
NSLog(@"datestring is AFTER %@",dateStr);

Upvotes: 0

MilanPanchal
MilanPanchal

Reputation: 2953

Check this.....

NSLog(@"datestring is NOW ==>> %@", _dayObject.p_date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];

NSDate *date = [dateFormat dateFromString: _dayObject.p_date];
NSLog(@"date transformed ==>> %@",date);
[dateFormat setDateFormat:@"EEEE, dd/MM/yyyy"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSString *dateStr = [dateFormat stringFromDate:date];
NSLog(@"datestring is AFTER ==>> %@",dateStr);
  • OUTPUT:~

    datestring is NOW ==>> 2013-05-04

    date transformed ==>> 2013-05-04 00:00:00 +0000

    datestring is AFTER ==>> Saturday, 04/05/2013

Upvotes: 1

ios_av
ios_av

Reputation: 324

Please try ..this it will work to get the week name

  NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"eeee"];
 NSCalendar * cal = [NSCalendar currentCalendar];
NSUInteger numWeekdays = [cal maximumRangeOfUnit:NSWeekdayCalendarUnit].length;
 NSDateComponents * comp = [[NSDateComponents alloc] init];
for( NSUInteger day = 1; day <= numWeekdays; day++ ){
[comp setWeekday:day];
[comp setWeek:0];
NSDate * date = [cal dateFromComponents:comp];

NSString * dayName = [dateFormat stringFromDate:date];

NSLog(@"%@", dayName);
}

Upvotes: 0

iphonic
iphonic

Reputation: 12719

Check the character case of parameters

NSLog(@"datestring is NOW %@",_dayObject.p_date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; //MODIFIED
NSDate *date = [dateFormat dateFromString:_dayObject.p_date];
NSLog(@"date transformed %@",date);
[dateFormat setDateFormat:@"EEEE, dd/MM/yyyy"]; //MODIFIED
NSString *dateStr = [dateFormat stringFromDate:date];
NSLog(@"datestring is AFTER %@",dateStr);

Upvotes: 0

Paras Joshi
Paras Joshi

Reputation: 20541

Use my this bellow custom method for convert Date Format ...

-(NSString *)changeDateFormat:(NSString*)stringDate dateFormat:(NSString*)dateFormat getwithFormat:(NSString *)getwithFormat{


    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:dateFormat];

    NSDate *date = [dateFormatter dateFromString:stringDate];

    dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:getwithFormat];

    NSString *convertedString = [dateFormatter stringFromDate:date];
    NSLog(@"Converted String : %@",convertedString);
    return convertedString;
}

and use it like bellow...

NSString *strSDate = [self changeDateFormat:_dayObject.p_date dateFormat:@"yyyy-MM-dd" getwithFormat:@"EEEE, dd/MM/yyyy"];
NSLog(@"datestring is AFTER %@",strSDate);

See My Blog with this post...

Upvotes: 2

Related Questions