-2

I am building an application where I need to convert The NSString to NSDate. I have googled and found a lot links of SOF mentioned below -

  1. https://stackoverflow.com/questions/3917250/converting-nsstring-to-nsdate-and- back-again

  2. Convert NSString to NSDate

  3. Converting can NSString to NSDate format in objective C

I had used the answer given there but the problem I still facing is explain below step by step.

  1. I had taken NSString *StringDate = @"01:00:00"; // I have declared time in the NSString variable.

  2. I used the conversion method that mentioned in above link and stored the converted value to the NSDate variable.

  3. When I am NSLog the NSDate variable. I am get "19:30:00" in my logcat. While the expected result should be "01:00:00".

Here is one of the code I am using

NSString *dateString = @"01:00:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:@"hh:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
Community
  • 1
  • 1
  • This issue has been discussed *so* many times here. Please make more of an effort to search before posting. – trojanfoe May 20 '15 at 08:18
  • possible duplicate of [Converting NSString to NSDate (and back again)](http://stackoverflow.com/questions/3917250/converting-nsstring-to-nsdate-and-back-again) – Fogh May 20 '15 at 12:03

3 Answers3

0

This is likely the correct date, but printed in a different timezone. I posted a longer explanation here:

Unexpected value from NSDate

BTW: In

NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];

[[NSDate alloc] init] is completely meaningless, because you assign an instance of NSDate in the very next line.

Community
  • 1
  • 1
Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
  • How to set TimeZone in that So I can get the correct answer. – hitesh matnani May 20 '15 at 08:12
  • Logging is always done in UTC+0. This *is* the correct answer. `NSLog()` is for logging and nothing else. If you want to convert the date into a string according to a specific time zone, use an instance of `NSDateFormatter` and log the (string) result. – Amin Negm-Awad May 20 '15 at 08:14
0
 -(NSString *)geTimeFromString:(NSString *)string
    {
        NSString * dateString = [NSString stringWithFormat: @"%@",string];
        NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"hh:mm:ss"];
        NSDate* myDate = [dateFormatter dateFromString:dateString];
        [dateFormatter setDateFormat:@"hh:mm a"];
        [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
        NSString *stringFromTime = [dateFormatter stringFromDate:myDate];
        return stringFromTime;
    }

Try this

Nikita Khandelwal
  • 1,741
  • 16
  • 25
  • Thanks. can you please explain me what is the meaning of ''en_US_POSIX" in above code ?? – hitesh matnani May 20 '15 at 08:30
  • From the [Docs](https://developer.apple.com/library/ios/qa/qa1480/_index.html) - " when working with fixed-format dates, set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is "en_US_POSIX", a locale that's specifically designed to yield US English results regardless of both user and system preferences. "en_US_POSIX" is also invariant in time (if the US, at some point in the future, changes the way it formats dates, "en_US" will change to reflect the new behaviour, but "en_US_POSIX" will not), and between machines" – Bista May 20 '15 at 08:38
  • my application is crashing – hitesh matnani May 20 '15 at 11:54
0

Try this method to convert NSString to NSDate :-

- (NSDate*) convertStringToDate
{
    NSString* dateStr = @"20/05/2015";
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    NSDate *date = [dateFormat dateFromString:dateStr];
    return date;
}
Neha Gupta
  • 157
  • 4