-2

Important update: Of you hover over a NSDATE with your mouse the degugger will convert the NSDate to your local timezone you have set on you Mac, but if you do a NSLOG you will notice that the NSDate is using the timezone that you assigned to the its respective formatter.

If you want to see in the xcode debugger what the NSDate is for the timezone you are working with go to your date/time settings for you Mac OS and change the Timezone to the one you are testing.

I require a NSDate to be created from the date I pass in, but currently it is set to the the day before I pass in:

 NSString *dateStr = @"2015-08-09";

NSDateFormatter *myformatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [formatter dateFromString:dateStr];

The code above returns: an NSDate set to '2015-08-08 12:00:00 +0000'

I need an NSDate object set to the datStr I pass in.

ConfusedDeer
  • 3,335
  • 8
  • 44
  • 72
  • 2
    What time zone are you in? – Fogmeister Aug 09 '15 at 17:26
  • If you are going to down-vote please leave a reason why and I will fix or delete the question. – ConfusedDeer Aug 09 '15 at 17:28
  • @Fogmeister Is it possible to get a NSDate irrespective of the timezone? – ConfusedDeer Aug 09 '15 at 17:29
  • NSDate doesn't know anything about time zone. That's why I was asking. NSDate is simply a point in time. It is your computer that converts that point in time to a year, month, date, hour, minute, etc... when you ask it to print the date out using `NSLog`. – Fogmeister Aug 09 '15 at 17:30

1 Answers1

0

This perhaps, from this:

Convert NSDate to NSString with NSDateFormatter with TimeZone without GMT Time Modifier

leave the 'z' lowercase for named timezone, i.e. PST and uppercase 'Z' for -0800

NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"MMMM dd, yyyy (EEEE) HH:mm:ss z Z"];
NSDate *now = [NSDate date];
NSString *nsstr = [format stringFromDate:now];

Also, you date should be more robust, like if you want to pass in the current day that your are passing in and it's one day off, then just add a day. The problem, it seems is that the date is returning the correct date which is the end of the last day, add 1 second or a millisecond and it'll probably be corrected, or just hack attack this and add 1 day to the days you are passing in. Be smart! Sometimes you just have to add 1 day to fix stuff and move on.

Community
  • 1
  • 1
Larry Pickles
  • 4,615
  • 2
  • 19
  • 36
  • If the question is answered by an answer to another question, it is a duplicate and should be marked as such. – nhgrif Aug 09 '15 at 17:53
  • surely, but I don't know how this stack stuff works, I've been here for 2 days and I've already answers about a billion questions that you guys couldn't, so feel free to mark it as a duplicate IDGAF. Also, this is clearly a different quesiton, did you read the question? If you didn't I suggest you read it, it's not that many lines and should be easy to digest. – Larry Pickles Aug 09 '15 at 17:54
  • @daddywarbucks the thought of adding one day seems like a hack solution and I was trying to avoid that, but that did work. – ConfusedDeer Aug 09 '15 at 18:06
  • @ConfusedDeer, I know, I know, but that's just how code works sometimes, I hate doing stuff like this myself, and I get it, but sometimes, it's not worth the trouble, and perhaps you'll have some inspiration after a few hours and think "Ah hah!, I figured it out" – Larry Pickles Aug 09 '15 at 18:07
  • The format string in your answer doesn't match the string that OP has; there is also a much better way to solve this problem than a "hack attack" so it will "probably be corrected". See the duplicate I've provided. – jscs Aug 09 '15 at 18:46
  • My answer is still SOOOOO MUCH better, just add one to stuff and you are done, that's all you do, just add one – Larry Pickles Aug 09 '15 at 19:45
  • @daddywarbucks it turns out all I needed to do was set the timezone for the formatter and then I found out how the NSDate and xcode debugger work in conjunction with the timezone you have in your Mac OS. If you don't know that the debugger will auto-convert the NSDate time to your local timezone this will cause you the programmer to think they are doing something wrong, which is what happened to me, even after I solved the issue. – ConfusedDeer Aug 09 '15 at 21:12
  • yeah, i assumed it wasn't too hard to do, thanks ConfusedDeer, that helps, adding 1 to stuff randomly is a bad idea unless you have to release your app by tomorrow and you need to code up 5 views still, – Larry Pickles Aug 09 '15 at 21:16