NSDate time converting from 12hr to 24 hr format

后端 未结 7 564
抹茶落季
抹茶落季 2020-12-19 22:22

hi i have problem while converting Date Formats... the date time is in the Format 12 hour Format like 12/9/2010 4:00:00 PM (Month/date/year Hour:Min:sec P

7条回答
  •  悲&欢浪女
    2020-12-19 23:02

    You need to use a NSDateFormatter. I encourage you to read the class reference as linked by Kay.

    Here's how you'd do it. I'm assuming the date is originating as a NSString.

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM-dd-yyyy HH:mm:ss"];
    NSDate *myDate = [df dateFromString:yourNSString]; 
    NSString *myNewDate = [formatter stringFromDate:myDate];
    [formatter release];
    [myDate release];
    

提交回复
热议问题