Date format for 2016-10-20T13:01:47.317

前端 未结 4 1038
别那么骄傲
别那么骄傲 2021-01-26 04:44

This is what I am trying for getting date from the string 2016-10-20T13:01:47.317 but getting nil

NSDateFormatter *dateFormatter1 =         


        
相关标签:
4条回答
  • 2021-01-26 05:14

    it .317 means milliseconds use SSS

    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateFormat:@"yyyy-MM-ddTHH:mm:ss.SSS"]; // or omit the timezone use `yyyy-MM-dd'T'HH:mm:ss.SSS`
     NSDate *date = [dateFormatter1 dateFromString:dateString]; 
    
    0 讨论(0)
  • 2021-01-26 05:14

    Try to use date format as

    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
    NSDate *date = [dateFormatter1 dateFromString:dateString]; 
    
    0 讨论(0)
  • 2021-01-26 05:17

    You need to use this DateFormatter yyyy-MM-dd'T'HH:mm:ss.SSS in your code

    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
    NSDate *date = [dateFormatter1 dateFromString:dateString]; 
    

    Here date is not nil

    0 讨论(0)
  • 2021-01-26 05:23

    Try this for remove millisecond :

    NSString *dateString = @"your string";
    NSDateFormatter* dateFormatter1 = [[NSDateFormatter alloc] init];
    dateFormatter1.dateFormat = @"YYYY-MM-ddTHH:mm:ss.SSS";
    NSDate *yourDate = [dateFormatter1 dateFromString:dateString];
    dateFormatter1.dateFormat = @"YYYY-MM-ddTHH:mm:ss";
    NSLog(@"%@",[dateFormatter1 stringFromDate:yourDate]);
    
    0 讨论(0)
提交回复
热议问题