日期处理类
- 定义
- 时间间隔计算
- 时间比较,返回其一
- 时间格式化
void dateCreate()
{
//date return current time
NSDate *date = [NSDate date];
NSLog(@"%@", date); //+ 10 sec
date = [NSDate dateWithTimeIntervalSinceNow:];
NSLog(@"%@", date); //1970-1-1 00:00:00 + 10 sec
date = [NSDate dateWithTimeIntervalSince1970:];
NSLog(@"%@", date); //Random future time?
date = [NSDate distantFuture];
NSLog(@"%@", date); date = [NSDate distantPast];
NSLog(@"%@", date);
} void dateUse()
{
NSDate *date = [NSDate date];
//NSTimeInterval is double
NSTimeInterval i = [date timeIntervalSince1970];
NSLog(@"%f", i);
} void dateFormat()
{
NSDate *date = [NSDate date];
NSDateFormatter *f = [[NSDateFormatter alloc] init];
f.dateFormat = @"yyyy-MMM-dd HH:mm:ss";
NSString *s = [f stringFromDate:date];
NSLog(@"1:%@", s); NSDate *d2 = [f dateFromString:@"2014-7月-29 22:12:12"];
NSLog(@"2:%@",d2);
}
字符串、日期格式转换:
// 17.将2013年05月05日转换为2013-05-05
NSMutableString *str17 = @"2013年05月05日";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
dateFormatter1.dateFormat = @"yyyy年MM月dd日";
NSDate *date1 = [dateFormatter1 dateFromString:str17];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
dateFormatter2.dateFormat = @"yyyy-MM-dd";
NSString *str17_2 = [dateFormatter2 stringFromDate:date1];
NSLog(@"题目17, 转换后的日期是%@", str17_2);