NSDate time converting from 12hr to 24 hr format

2020-02-12 14:29发布

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 Pm) i need to Convert it into 24 hour format like 12/9/2010 16:00:00
Can any one Help me please.... Thx in advance

7条回答
We Are One
2楼-- · 2020-02-12 14:50

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];

}
查看更多
爷、活的狠高调
3楼-- · 2020-02-12 14:56
  1. In Swift

    var dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "MM/dd/yyyy hh:mm:ss a"
    var date: Date? = dateFormatter.date(from: "12/9/2010 4:00:00 PM")
    dateFormatter.dateFormat = "MM/dd/yyyy HH:mm:ss"
    var convertedString: String? = nil
    if let aDate = date {
        convertedString = dateFormatter.string(from: aDate)
    }
    
  2. In Objective C

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"MM/dd/yyyy hh:mm:ss a";
    NSDate *date = [dateFormatter dateFromString:@"12/9/2010 4:00:00 PM"];
    dateFormatter.dateFormat = @"MM/dd/yyyy HH:mm:ss";
    NSString *convertedString = [dateFormatter stringFromDate:date];
    
查看更多
Deceive 欺骗
4楼-- · 2020-02-12 14:59

Simply do This to get 24 hour time format (In Swift 2)

let formatter = NSDateFormatter()
formatter.dateFormat = "HH:mm a"  //(hh:mm a----> for 12 hour format)
let timeString = formatter.stringFromDate(NSDate())
print(timeString)

UPDATE: Swift 3 version

let formatter = DateFormatter()
formatter.dateFormat = "HH:mm a"  //(hh:mm a----> for 12 hour format)
let timeString = formatter.string(from: Date())
print(timeString)
查看更多
Animai°情兽
5楼-- · 2020-02-12 15:07

Take a look at NSDateFormatter class reference and the data formatting guide. There you will find more especially the method dateFromString and stringFromDate are interesting for you. There is another question dealing with similar problem From string with locale to date on iphone sdk where you can find some sample code when you want to set a special formatting template string.

查看更多
萌系小妹纸
6楼-- · 2020-02-12 15:08
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateformatter.dateFormat = @"hh:mm a";
NSDate *date = [dateformatter dateFromString:departTime];
dateformatter.dateFormat = @"HH:mm";
NSString *time24 = [dateformatter stringFromDate:date];
departTimelbl.text=time24;
查看更多
霸刀☆藐视天下
7楼-- · 2020-02-12 15:12

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];
查看更多
登录 后发表回答