我希望在设置时间时获得本地通知所以在timepickker中我得到的时间格式如07:14 Am / Pm,我需要转换为19:14,以便我可以设置像
这样的值我想将07转换为19如何将值放在datecomponet中。
set prefix to text 1 thru 3 of fileName
答案 0 :(得分:1)
尝试在这样的游乐场做:
Swift Elample:
let stringDate = "16:00:00"
let dateFormatter1 = DateFormatter()
dateFormatter1.dateFormat = "HH:mm:ss"
let date1 = dateFormatter1.date(from: stringDate)
print(date1!)
let dateFormatter2 = DateFormatter()
dateFormatter2.dateFormat = "hh:mm a"
print(dateFormatter2.string(from: date1!))
print(dateFormatter1.string(from: date1!))
Objective-C Elample:(粘贴到任何viewDidLoad
)
NSString *stringDate = @"16:00:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss"];
NSDate *date1 = [dateFormatter dateFromString:stringDate];
NSLog(@"%@", date1);
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:@"hh:mm a"];
NSLog(@"%@", [dateFormatter2 stringFromDate:date1]);
NSLog(@"%@", [dateFormatter stringFromDate:date1]);
<强>结果:强>
注意:强> 如果你想得到: