0

Say for instance, I have a date: 2014-11-24 12:24:25 stored in an NSString variable.

I'm trying to convert that to an NSDate object so that I can calculate a future date:

NSDate *futureTimeStamp;

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

NSDate *oldDate = [dateFormatter dateFromString:timestamp];

It keeps telling me that the old date time is: 2014-11-24 06:46:06 +0000 which isn't correct.

Any ideas why it's not converting it correctly?

Update #1:

timestamp is a string. Stored in that variable is: 2014-11-24 12:58:40

When I print out oldDate, it shows this in the 2014-11-24 06:58:40 +0000

Sandy D.
  • 3,166
  • 1
  • 20
  • 31
  • Seems like you need to set the date formatter's locale explicitly? – Max Chuquimia Nov 24 '14 at 18:58
  • Post the missing code, for example what exact code tells you what the "old time" is. I bet you are somewhere in India. Maybe the fact that I'm guessing this gives you a hint what you are doing wrong. – gnasher729 Nov 24 '14 at 18:59
  • @gnasher729 I'm not in India. lol. I'm in the United States. – Sandy D. Nov 24 '14 at 19:02
  • @Jugale I tried adding this: [dateFormatter setLocale:[NSLocale currentLocale]]; and it didn't change anything. Same results. – Sandy D. Nov 24 '14 at 19:02
  • Does this work? [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss a"]; – mikemike396 Nov 24 '14 at 19:06
  • @Pyraego.com Nope. It logs null for the old date using that. =( – Sandy D. Nov 24 '14 at 19:07
  • 1
    NSDate always dumps in UTC. – Hot Licks Nov 24 '14 at 19:08
  • The date conversion is correct. 7 in the morning in London, as the `NSLog()` prints it, is the same time as 1 in the afternoon in Dhaka, where you are. – jscs Nov 24 '14 at 19:14
  • @JoshCaswell I'm in the U.S. This date conversion isn't correct. It should be the exact same date value that is stored in the NSString -- I just need it to be an NSDate object. – Sandy D. Nov 24 '14 at 19:20
  • 2
    Wherever you are, your device thinks it's in GMT+6. I don't know why, but the problem is the same. The code is functioning as it should. – jscs Nov 24 '14 at 19:22
  • NSString *dateStr=[self getUTCFormateDate:[NSDate date]]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss "]; NSDate *dateTodate = [dateFormat dateFromString:dateStr]; NSDate* myEvent = [dateFormatter dateFromString:dateString]; NSTimeInterval interval = [dateTodate timeIntervalSinceDate:myEvent]; – Nikita Khandelwal Nov 25 '14 at 09:36
  • Do note that with date format "yyyy-MM-dd hh:mm:ss" you will not be able to parse any times after 12:59:59. – Hot Licks Nov 25 '14 at 19:13
  • @HotLicks What do you mean -- time after 12:59:59?? – Sandy D. Nov 26 '14 at 14:36
  • Like either 13:00:00 or 1:00:00 pm – Hot Licks Nov 26 '14 at 17:05
  • @HotLicks I end up taking the string and convert it back to an NSDate before I do any comparisons... so I haven't ran into any issues with parsing because I'm comparing two NSDates... so I don't think I will run into that problem. – Sandy D. Nov 26 '14 at 17:33
  • So what happens if you receive a date string with 13:00:00 or 1:00:00 pm in it? Have you tried it? – Hot Licks Nov 26 '14 at 17:40
  • @HotLicks The string is converted to an NSDate object - which prints out 2014-11-25 01:00:00 +0000 for input string 2014-11-25 01:00:00 The 13:00:00 doesn't print out correctly.... I'm curious though if I'll ever get that... because for 12:00 A.M -- it's been 00:00:00. What are you suggesting should be the format for the datetime stamp? – Sandy D. Nov 26 '14 at 17:47
  • So you never have a time of 1 PM? Always times in the morning? – Hot Licks Nov 26 '14 at 17:53
  • @HotLicks That's not what I'm saying. I'm saying it always prints out to the console as the same value as in the morning time. Maybe the reason it does this is because of how I'm storing it in the NSString. What should this yyyy-MM-dd hh:mm:ss so that it keeps AM vs PM? – Sandy D. Nov 26 '14 at 17:56
  • 1
    Bookmark this: http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns – Hot Licks Nov 26 '14 at 17:57
  • @HotLicks I'm beyond confused at this point. If I add 'z' to the end of my date format -- then I get this 1970-01-01 00:00:00 +0000 for the current timestamp... which makes no sense to me. – Sandy D. Nov 26 '14 at 18:11
  • 1
    Look at what "hh" means. – Hot Licks Nov 26 '14 at 18:25
  • @HotLicks OMG! Thank you so very much!!!! h -> Hour [1-12]. H -> Hour [0-23]. Changed format to: `yyyy-MM-dd HH:mm:ss` – Sandy D. Nov 26 '14 at 18:37
  • 1
    And there is a [local "feature"](http://stackoverflow.com/questions/6613110/what-is-the-best-way-to-deal-with-the-nsdateformatter-locale-feature) you should probably be aware of, when you start testing on real hardware. – Hot Licks Nov 26 '14 at 18:52
  • @HotLicks I'm actually testing on an iPhone 6 device. I had this set in my code: NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; I"m guessing that is what you are referring to? So, I shouldn't run into any problems, right? – Sandy D. Nov 26 '14 at 19:00
  • 1
    If you have that (in the right place) you should be OK. – Hot Licks Nov 26 '14 at 19:03
  • @HotLicks I have it right after I create my dateFormatter. I haven't run into any problems testing on my device so far. I really appreciate your help! Thank you so much for pointing out the hour issue. – Sandy D. Nov 26 '14 at 19:04

2 Answers2

-1

You can make time shifting like:

NSDate *date = [oldDate dateWithTimeIntervalSinceReferenceDate:milliseconds];
ios
  • 15
  • 3
  • I'm new to iOS development... so I'm confused how this helps me.. since oldDate is the issue... the oldDate isn't converting from a string to the correct date. – Sandy D. Nov 24 '14 at 19:05
  • you can calc the deference between the correct time and the converted date. then you can shift the converted time to correct one. check this https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html – ios Nov 24 '14 at 19:07
  • 1
    The date is correct and does not need to be shifted manually. – jscs Nov 24 '14 at 19:16
  • 2
    The above solves no problem -- just introduces more. – Hot Licks Nov 25 '14 at 19:11
-1

Try converting to the local date from UTC

  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
  NSDate *date = [dateFormatter dateFromString:timestamp];
  NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
  NSTimeZone *previousTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
  NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:date];
  NSInteger currentOffset = [previousTimeZone secondsFromGMTForDate:date];
  NSTimeInterval gmtInterval = currentGMTOffset - currentOffset;
  NSDate *localDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:date];
mikemike396
  • 2,435
  • 1
  • 26
  • 41
  • 1
    This definitely helped me out! Thank you! – Sandy D. Nov 26 '14 at 14:36
  • But your date format is wrong. – Hot Licks Nov 26 '14 at 17:06
  • And it's bad practice to have an NSDate that is referenced to a timezone other than UTC. If you do it, comment it up the wazoo. – Hot Licks Nov 26 '14 at 17:07
  • @HotLicks The date format matches what the original question was and adding the comments is your opinion! Why the down vote when the answer is correct... – mikemike396 Nov 26 '14 at 17:11
  • @HotLicks What should the date format be then?? – mikemike396 Nov 26 '14 at 17:26
  • Bookmark this: http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns – Hot Licks Nov 26 '14 at 17:30
  • And, as I said, never have an NSDate containing other than UTC, and if you break that rule comment the bejeezus out of it. – Hot Licks Nov 26 '14 at 17:32
  • @HotLicks I'm confused. I'm comparing two NSDates. I haven't ran into any issues yet... This answer helped me because it showed me how to determine the offset and bring it back to local time -- which is what I needed -- because I needed to compare both NSDates in the same time conversion (local dates). – Sandy D. Nov 26 '14 at 17:36
  • @SandyD. - I know you're confused, and getting more confused by the minute. (You will run into all sorts of "issues" shortly.) – Hot Licks Nov 26 '14 at 17:42
  • @HotLicks He is confused by your statement which is your opinion. He stated the answer solved his issue. – mikemike396 Nov 26 '14 at 17:44