String to a Format yyyy-MM-dd HH:mm:ss Iphone
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-MM-dd HH:mm:ss
.
for eg: 2011-09-08 15:51:57
so that i need to show the string in a label in the later format.
Thanks all.
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];
You may use NSDateFormatter
精彩评论