//时间初始化 NSDate *date = [[NSDate alloc]initWithString:@"2010-01-01 23:59:59 +0900"]; NSString *str = [date description]; NSLog(@"%@",str);
//是否相同日期:
NSDate *date1 = [[NSDate alloc] initWithString:@"2010-01-01 23:59:59 +0900"]; NSDate *date2 = [[NSDate alloc] initWithString:@"2010-02-14 23:59:59 +0900"]; BOOL b = [date1 isEqualToDate: date2]; if (b) { NSLog(@"%@",date1); }else{ NSLog(@"不相等"); }
//取得从现在开始过某秒之后的日期时间: NSDate *d = [NSDate dateWithTimeIntervalSinceNow: 86400*7]; NSLog(@"%@",d); //*负数是指过去时间
//自己设定的形式(这是个形式) NSDateFormatter *df = [[NSDateFormatter alloc] init]; df.dateFormat = @"yyyy/MM/dd HH:mm:ss"; NSLog(@"这个时间是:%@",df); //或 [df setDateFormat:@"yyyy/MM/dd HH:mm:ss"]; //取得现在日期时间:(连接上面设定形式) NSString *str1 = [df stringFromDate:[NSDate date]]; NSLog(@"--->%@",str1); //设定日期: (连接上面设定形式) NSDate *aDate = [df dateFromString: @"2000/03/01 00:00:00"]; NSLog(@"这个是:%@",aDate); //从某时间开始经过多长时间后的日期时间: NSDate *bDate; bDate = [aDate initWithTimeInterval:3*60 sinceDate:aDate]; //从aDate过3分钟 NSLog(@"从A时间过去了%@",bDate); //只取得日期不要时间: NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setTimeStyle: NSDateFormatterNoStyle]; [df setDateStyle: NSDateFormatterMediumStyle]; NSString *nowStr = [df stringFromDate:[NSDate date]]; NSLog(@"日期:%@",nowStr);