Wrong time from NSDateFormatter

后端 未结 3 1175
南旧
南旧 2021-01-24 14:42

I have a string that I want to parse the time from:

NSString *longdate = @\"Mar 27, 2011 8:38:38 PM\";

I want to parse this date and output jus

相关标签:
3条回答
  • 2021-01-24 14:49

    Found the problem. Had nothing to do with timezones and everything to do with using the wrong formatting codes for the date formatter.

    [inFormat setDateFormat:@"MMM dd, yyyy HH:mm:ss aaa"];
    

    should be:

    [inFormat setDateFormat:@"MMM dd, yyyy h:mm:ss aaa"];
    

    Likewise, outFormat's dateformat should be:

    [outFormat setDateFormat:@"h:mm aaa"];
    

    After this adjustment everything works fine even w/o any TimeZone adjustments.

    0 讨论(0)
  • 2021-01-24 14:59

    You've got your timezones messed up. IIRC, NSDateFormatter (by default) will parse stuff in the UTC timezone (+000), but dates are NSLogged in your current timezone. Or something like that.

    Anyway, check your timezones.

    0 讨论(0)
  • 2021-01-24 15:10

    As Dave said, check your time zones. You can tell the date formatter to use your current time zone as well:

    [outFormat setTimeZone:[NSTimeZone localTimeZone]];
    
    0 讨论(0)
提交回复
热议问题