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

前端 未结 2 1083
孤街浪徒
孤街浪徒 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];
    

提交回复
热议问题