NSDate time converting from 12hr to 24 hr format

后端 未结 7 529
抹茶落季
抹茶落季 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];
    
    }
    
    0 讨论(0)
  • 2020-12-19 22:41

    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.

    0 讨论(0)
  • 2020-12-19 22:52

    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)
    
    0 讨论(0)
  • 2020-12-19 22:55
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"HH:mm";
    NSDate *date = [dateFormatter dateFromString:@"15:15"];
    
    dateFormatter.dateFormat = @"hh:mm a";
    NSString *pmamDateString = [dateFormatter stringFromDate:date];
    
    0 讨论(0)
  • 2020-12-19 22:55
    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];
      
    0 讨论(0)
  • 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];
    
    0 讨论(0)
提交回复
热议问题