-3

I need to convert 2013/5/23 to day. here the problem is month is having as 5.

2013/5/23 => Thursday

code that I am using:

        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"yyyy/MM/dd"];
        
        NSDate *date = [dateFormat dateFromString:@"2013/5/23"];
        NSLog(@"Date :%@",date);
        
        [dateFormat setDateFormat:@"EEEE"];
        NSLog(@"DAY %@",[dateFormat stringFromDate:date]);

and the output is

Date :2013-05-22 18:30:00 +0000

DAY Thursday

Actually day is printed correctly according to my input date. But printed date is wrong. it should be 2013-05-23

Community
  • 1
  • 1
Rocky
  • 19
  • 3
  • possible duplicate of [Converting NSString to NSDate (and back again)](http://stackoverflow.com/questions/3917250/converting-nsstring-to-nsdate-and-back-again) – gsach Jul 23 '13 at 08:40
  • read Apple's documentation for NSDateFormatter and look up examples of its usage. – esh Jul 23 '13 at 08:41
  • i have check that answer. but my problem is month is having as 5. not 05. – Rocky Jul 23 '13 at 08:44
  • Your month is being parsed correctly; the issue is with formatting the date back to a day using `EEEE`. I believe your code should work. Please clarify what you mean by "but when i print the date it's giving as"; is that the final `NSLog()` statement? – trojanfoe Jul 23 '13 at 08:45
  • "when i print the date it's giving as" ?? please describe you this line? – Suryakant Sharma Jul 23 '13 at 08:53
  • 1
    Agree with @Rajneesh071 what do you want exactly? Please clear. What is the problem if you print date ? – Niru Mukund Shah Jul 23 '13 at 08:56
  • He want's `2013/5/23` converted to `Thursday`. – trojanfoe Jul 23 '13 at 08:56
  • i have update the question. i want to convert 2013/5/23 to Thursday. but when i print the date it's giving as 2013-05-22 18:30:00 +0000 – Rocky Jul 23 '13 at 09:00
  • @Rocky Once again then: **WHAT DO YOU MEAN BY "WHEN I PRINT THE DATE"??? IS THAT THE FINAL NSLOG() STATEMENT OR ARE YOU PRINTING THE NSDATE OBJECT**. – trojanfoe Jul 23 '13 at 09:02
  • check now buddy.. it will print your date... – Rajneesh071 Jul 23 '13 at 09:04
  • when im printing the nsdate obj it's giving the wrong date – Rocky Jul 23 '13 at 09:04
  • @Rocky and what would you expect the `NSDate` object to contain? – trojanfoe Jul 23 '13 at 09:05
  • @trojanfoe While I applaud your attempts to get to the bottom of this; the date is actually correct, except that the user hasn't accounted for the timezone. Local midnight for him is 6:30pm GMT, which is what the log is showing. – Abizern Jul 23 '13 at 09:09
  • 1
    @Abizern I think the OP is confused that calling `[NSDate description]` (by way of `NSLog(@"%@");`) is **not** the same as using `NSDateFormatter` to get the date in the format he wants. He doesn't mention anything about timezones in his question and says that all he wants is the day-of-week for the specified input date. I think the question is completely bogus. – trojanfoe Jul 23 '13 at 09:24

2 Answers2

0

you can set timezone like

[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
Yas
  • 1,064
  • 9
  • 13
-2
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
  • can any one explain reason of down voting? i want to improve answer if you have proper solution, and if don't have then no need to down vote.. – Rajneesh071 Jul 23 '13 at 09:35