NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// 设置日期格式(formatter对象就会按照这个格式去解析字符串,或者生成字符串)
// y年 M月 d日 H小时(24小时制) h小时(12小时制) m分 s秒
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *createDate = [formatter dateFromString:@"2015-08-23 19:46:14"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// 设置日期格式(formatter对象就会按照这个格式去解析字符串,或者生成字符串)
// y年 M月 d日 H小时(24小时制) h小时(12小时制) m分 s秒
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
// 当前时间
NSDate *date = [NSDate date];
NSString *string = [formatter stringFromDate:date];
// 获得当前时间
NSDate *nowDate = [NSDate date];
//从beforeDate -> nowDate的时间间隔,秒
NSTimeInterval interval = [nowDate timeIntervalSinceDate:beforeDate];
// 时间戳
// "time": "6456546455435",到1970年的时间
[NSDate dateWithTimeIntervalSince1970:6456546455435 / 1000.0];
// 日期组件 (年、月、日、小时、分、秒)
/** NSCalendarUnit 枚举量
* NSCalendarUnitEra
NSCalendarUnitYear
NSCalendarUnitMonth
NSCalendarUnitDay
NSCalendarUnitHour
NSCalendarUnitMinute
NSCalendarUnitSecond
NSCalendarUnitWeekday
NSCalendarUnitWeekdayOrdinal
NSCalendarUnitQuarter
NSCalendarUnitWeekOfMonth
*/
- (NSInteger)component:(NSCalendarUnit)unit fromDate:(NSDate *)date;
// 年
NSInteger year = [calendar component:NSCalendarUnitYear fromDate:createDate];
// 月
NSInteger month = [calendar component:NSCalendarUnitMonth fromDate:createDate];
// 日
NSInteger day = [calendar component:NSCalendarUnitDay fromDate:createDate];
NSLog(@"%zd %zd %zd", year, month, day);
// 创建日期类--获取当前时分秒
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *cmp = [calendar components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]];
//获得当前秒
NSInteger sec = cmp.second;
//获得当前分钟
NSInteger min = cmp.minute;
//获得当前小时
NSInteger hour = cmp.hour;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *cmps = [calendar components:unit fromDate:beforeDate toDate:nowDate options:0];