以下应该给我一个9:30
的NSDate对象:
NSString *dateString = @"09-30";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh-mm"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
如何以相同的格式获取当前时间?
答案 0 :(得分:15)
NSDate *currentTime = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh-mm"];
NSString *resultString = [dateFormatter stringFromDate: currentTime];
答案 1 :(得分:2)
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh-mm"];
NSDate *currentDate = [NSDate date];
NSString *currentDateString = [dateFormatter stringFromDate:currentDate];
答案 2 :(得分:1)
NSDate alloc init将使用当前时间创建日期。然后,您可以使用NSDateFormatter stringFromDate作为格式并显示该日期。
此:
NSDate *date = [[NSDate alloc] init];
NSLog(@"%@", date);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh-mm"];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(@"%@", dateString);
输出:
2011-12-05 07:20:02.994 Craplet[1706:707] 2011-12-05 12:20:02 +0000
2011-12-05 07:20:02.995 Craplet[1706:707] 07-20
答案 3 :(得分:1)
NSDate *today = [[NSDate alloc] init];
NSLog(@"today is :%@", today);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh-mm:ss"];
NSString *timeString = [dateFormatter stringFromDate:today];
NSLog(@"%@", timeString);
答案 4 :(得分:0)
NSDate *currentDate =[NSDate date];
//Your formatter goes here.
NSString *currentTime = [dateFormatter stringFromDate:currentDate];