NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:mm a"];
NSString *hourMinuteSecond = [dateFormatter stringFromDate:date];
hour1Label.text = [hourMinuteSecond substringWithRange:NSMakeRange(0, 1)];
hour2Label.text = [hourMinuteSecond substringWithRange:NSMakeRange(1, 1)];
minute1Label.text = [hourMinuteSecond substringWithRange:NSMakeRange(3, 1)];
minute2Label.text = [hourMinuteSecond substringWithRange:NSMakeRange(4, 1)];
hourMinuteSecond 的内容是:06:40
那么substringWithRange:NSMakeRange(0, 1)的意思就是从hourMinuteSecond这个字符串里取得第0位到第1位的内容,即0;
同理,NSMakeRange(3, 1)的内容就是hourMinuteSecond的字符串里第3位,取值长度为1位,即4……
感谢:
http://blog.sina.com.cn/s/blog_6f72ff900102v3br.html
网友评论