提问人:VyTcdc 提问时间:12/16/2017 最后编辑:VyTcdc 更新时间:5/30/2018 访问量:56
我得到了像 07:14 Am/Pm 这样的时间格式,我需要在 NSDateComponent 中转换 19:14
I am getting the time format like 07:14 Am/Pm that I required to convert like 19:14 in NSDateComponent
问:
我想在设置时间时获得 localnotification,因此在 timepickker 中,我获得了像 07:14 Am/Pm 这样的时间格式,我需要像 19:14 一样进行转换,以便我可以设置像
我想将 07 转换为 19 如何将值放入 datecomponet..
//getting time format 07:14 Am/Pm
NSString * time1 = Timepicker.text;
NSArray *array = [time1 componentsSeparatedByString:@":"];
[datecomponet setHour:19];
[datecomponet setMinute:38];
[dateformater setDateFormat:@"hh:mm a"];
self.Timepicker.text= [NSString stringWithFormat:@"%@",[dateformater stringFromDate:datepicker.date]];
/// This is local notification code
NSCalendar * gregcalender = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[gregcalender setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"IST"]];
NSDateComponents *datecomponet = [gregcalender components:NSCalendarUnitYear|NSCalendarUnitMonth | NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate:date];
// [datecomponet setYear:2017];
// [datecomponet setMonth:12];
// [datecomponet setDay:16];
// [datecomponet setHour:18];
// [datecomponet setMinute:38];
NSInteger year = [datecomponet year];
NSInteger month = [datecomponet month];
NSInteger day = [datecomponet day];
NSString * time1 = Timepicker.text;
// NSInteger hour = [datecomponet hour];
// NSInteger min = [datecomponet minute];
//NSString *testString= @"It's a rainy day";
NSArray *array = [time1 componentsSeparatedByString:@":"];
[datecomponet setHour:18];
[datecomponet setMinute:38];
UIDatePicker * dd = [[UIDatePicker alloc]init];
[dd setDate:[gregcalender dateFromComponents:datecomponet]];
NSLog(@"======>NSNotification method tab<=====");
答:
1赞
biloshkurskyi.ss
12/16/2017
#1
尝试在操场上像这样做:
斯威夫特 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]);
结果:
- 2000-01-01 14:00:00 +0000 / 2000 年 1 月 1 日星期六 16:00:00 (包括我的时区)
- 04:00 下午
- 16:00:00
注意:如果您想获得:
- 上午 12 点,下午 5 点,然后使用“hh mm a”
- 00、17 则使用“HH mm a”
评论
0赞
VyTcdc
12/16/2017
你能在 objecive c 中举个例子吗
0赞
biloshkurskyi.ss
12/16/2017
@VyTcdc当然。请检查
0赞
biloshkurskyi.ss
12/16/2017
@VyTcdc尝试将验证添加到字符串日期:if (stringDate.length > 0) { //time formating } else { NSLog(@"stringDate is nil. Need to check picker data."); }
评论
TimePicker
datecomponet