NSDateFormatter and yyyy-MM-dd

后端 未结 2 1646
慢半拍i
慢半拍i 2020-12-02 00:15

I\'m trying to take a NSString date in the format \"2/22/11\" and convert it to this format: 2011-02-22

This is my code:

NSDate *dateTemp = [[NSDate          


        
2条回答
  •  [愿得一人]
    2020-12-02 00:42

    This should work fine. I have set Date style. You can change NSDateFormatterShortStyle to something else. Also check your newInvoice.date format.

    NSDate *dateTemp = [[NSDate alloc] init];
    
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    
    [dateFormat setDateStyle: NSDateFormatterShortStyle];
    
    dateTemp = [dateFormat dateFromString: newInvoice.date];
    
    [dateFormat setDateFormat:@"yyyy-MM-dd"];
    
    newInvoice.date = [dateFormat stringFromDate:dateTemp];
    

提交回复
热议问题