Converting string MM/DD/YYYY to NSDate

后端 未结 4 1390
傲寒
傲寒 2020-12-10 15:02

In my app i\'m using Facebook Graph API and when I\'m fetching user details i get the user\'s birthday as a string with format MM/DD/YYYY.

My question is, how can

相关标签:
4条回答
  • 2020-12-10 15:47

    Add [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]

    0 讨论(0)
  • 2020-12-10 15:51

    You are doing everything fine. Your problem is only the format string.

    • Capital M is month.
    • Small d is day.
    • Small y is year.

    Just use these symbols consistently and you should be fine.

    0 讨论(0)
  • 2020-12-10 15:54

    Using NSDateFormatter you can convert directly from a NSString to a NSDate. Here's an example:

    NSDateFormatter* myFormatter = [[NSDateFormatter alloc] init];
    [myFormatter setDateFormat:@"MM/dd/yyyy"];
    NSDate* myDate = [myFormatter dateFromString:@"8/26/2012"];
    NSLog(@"%@", myDate);
    

    See Unicode.org for details on date formatting patterns.

    0 讨论(0)
  • 2020-12-10 15:57

    For exactly such cases NSDateFormatter exist.

    0 讨论(0)
提交回复
热议问题