NSString to NSDate for yyyy-MM-dd'T'HH:mm:ss.SSS+05:30 format

后端 未结 5 1070
一向
一向 2021-01-01 21:52

My date string that I am getting is 2014-01-08T21:21:22.737+05:30 of this format. How do i confer it to NSDate?

I tried:

       NSString *currentDate         


        
相关标签:
5条回答
  • 2021-01-01 22:36

    Your date format contains a time zone as a literal: yyyy-MM-dd'T'HH:mm:ss.SSS+05:30 the +05:30 it the time zone definition. You should parse it with Z.

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    NSString *currentDateString = @"2014-01-08T21:21:22.737+05:30";
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
    NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
    
    NSLog(@"CurrentDate:%@", currentDate);
    
    0 讨论(0)
  • 2021-01-01 22:38
    NSString *p=@"2014-01-08T21:21:22.737+05:30";
    
    
    NSDateFormatter *dateformat=[[NSDateFormatter alloc]init];
    
    [dateformat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
    
    NSDate *datefor=[dateformat dateFromString:p];
    
    [dateformat setDateFormat:@"MM-dd-yyyy hh:mm a"];
    
    NSString *dateStr=[dateformat stringFromDate:datefor];
    
    
    NSDate *datetype=[dateformat dateFromString:dateStr];
    
    0 讨论(0)
  • 2021-01-01 22:39

    I think you need to initialize the dateFormatter like this below in your code. Because if do not initialze always you will get the null value:-

    dateFormatter=[[NSDateFormatter alloc]init];
    
    0 讨论(0)
  • 2021-01-01 22:45

    Set your dateFormat as

    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
    

    And Yes, Now NSLog is Printing a specific Date.!!!

    Enjoy Coding

    0 讨论(0)
  • 2021-01-01 22:59

    Use this code it is working fine your dateFormatter was wrong .

    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
    
    NSString *currentDateString = @"2014-01-08T21:21:22.737+05:30";
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];        
    NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
    
    NSLog(@"CurrentDate:%@", currentDate);
    

    Happy coding!!!!!!!!!!.

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