String to a Format yyyy-MM-dd HH:mm:ss Iphone

前端 未结 2 1051
孤街浪徒
孤街浪徒 2021-01-14 18:35

I have an nsstring ,see below

NSString *Mydate=@\"9/8/2011\"; in month/day/year format.

I want this string to be in the format yyyy-M

相关标签:
2条回答
  • 2021-01-14 18:53

    Try below code with valid input string that will help you.

        NSString *dateStr = @"9/8/2011 11:10:9";
        NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
        [dtF setDateFormat:@"d/M/yyyy hh:mm:s"];
        NSDate *d = [dtF dateFromString:dateStr];
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:s"];
        NSString *st = [dateFormat stringFromDate:d];   
        NSLog(@"%@",st);
        [dtF release];
        [dateFormat release];
    
    0 讨论(0)
  • 2021-01-14 19:05

    You may use NSDateFormatter

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