NSDate 哪些事
.什么是时间戳?
时间戳是自 1970 年 1 月 1 日(00:00:00 GMT)至当前时间的总秒数。
2.NSDate,时间戳,NSString 之间的转换
//string 转 date + (NSDate *)dateWithString:(NSString *)str dateFormater:(NSString *)dateFormat{ NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:dateFormat]; // 年-月-日 时:分:秒如@"yyyy-MM-dd hh:mm:ss" NSDate * date = [formatter dateFromString:str]; return date; } //date 转 string + (NSString *)stringWithDate:(NSDate *)date dateFormater:(NSString *)dateFormat{ NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:dateFormat]; NSString *str = [formatter stringFromDate:date]; return str; } //字符串转时间戳 +(NSTimeInterval)dateStrToInterval:(NSString *)dateStr dateFormater:(NSString *)dateFormat{ NSDateFormatter *fo = [[NSDateFormatter alloc] init]; [fo setDateFormat:dateFormat]; NSDate *da = [fo dateFromString:dateStr]; if( da == nil ) return 0; return da.timeIntervalSince1970; } //时间戳转字符串 +(NSString*)dateIntervalToString:(NSTimeInterval)timeInterval dateFormater:(NSString *)dateFormat{ if( !dateFormat ) return nil; NSDateFormatter *fo = [[NSDateFormatter alloc] init]; [fo setDateFormat:dateFormat]; NSDate *da = [NSDate dateWithTimeIntervalSinceNow:(timeInterval+28800)/1000]; // [fo setDateStyle:kCFDateFormatterNoStyle]; NSString *daStr = [fo stringFromDate:da]; return daStr; }
3.增加天数
NSDate * date = [NSDate date];
//明天
NSDate *afterToday = [date dateByAddingTimeInterval:60*60*24];
//昨天
NSDate *yesterday = [date dateByAddingTimeInterval: -60*60*24 ];
4.NSDate比较
if (!([[NSDate date] timeIntervalSinceDate:[[NSDate date] dateByAddingTimeInterval:60*60*24]]]<60))
//多余的天数
NSInteger num = [NSNumber numberWithDouble:[_currentOutDate timeIntervalSinceDate:_currentInDate]/3600/24].integerValue;
self.accommodationL.text = [NSString stringWithFormat:@"住%ld晚",num]
将来的自己,会感谢现在不放弃的自己!