NSDate time converting from 12hr to 24 hr format

后端 未结 7 567
抹茶落季
抹茶落季 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 22:38

    Use these functions,this will change the date string from 12 to 24 hr formate and 24 to 12hr formate.

    -(NSString *)changeformate_string24hr:(NSString *)date
    {
    
        NSDateFormatter* df = [[NSDateFormatter alloc]init];
    
        [df setTimeZone:[NSTimeZone systemTimeZone]];
    
        [df setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
    
        NSDate* wakeTime = [df dateFromString:date];
    
        [df setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
    
    
        return [df stringFromDate:wakeTime];
    
    }
    
    -(NSString *)changeformate_string12hr:(NSString *)date
    {
    
        NSDateFormatter* df = [[NSDateFormatter alloc]init];
    
        [df setTimeZone:[NSTimeZone systemTimeZone]];
    
        [df setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
        NSDate* wakeTime = [df dateFromString:date];
    
    
        [df setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
    
    
        return [df stringFromDate:wakeTime];
    
    }
    

提交回复
热议问题