iOS NSDate 相关知识整理

项目最近用到一些关于 NSDate 的相关知识,特此记录。需要用的朋友可以创建一个类目,把需要用到的方法复制过去就行。

目录

1.是否为今天
2.是否为昨天
3.根据时间戳获取时间
4.根据时间获取时间戳
5.获取当前时间的时间戳
6.计算两个日期间隔天数
7.获取当前是星期几
8.获取当前是几月几日
9.计算时间差
10.字符串转日期格式
11.将世界时间转化为中国区时间

// 是否为今天
- (BOOL)isToday{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];
    NSDate *today = [cal dateFromComponents:components];
    
    components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:self];
    NSDate *otherDate = [cal dateFromComponents:components];
    
    if([today isEqualToDate:otherDate]){
        return YES;
    }
    return NO;
}
// 是否为昨天
- (BOOL)isYesterday{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];
    NSDate *today = [cal dateFromComponents:components];
    NSDate *yesterday = [today dateByAddingTimeInterval: -kDayTimeInterval];
    
    components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:self];
    NSDate *otherDate = [cal dateFromComponents:components];
    
    if([yesterday isEqualToDate:otherDate]){
        return YES;
    }
    return NO;
}
// 根据时间戳获取时间
+ (NSString *)timeWithTimeStampString:(NSString *)timeStamp{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeStamp doubleValue]/ 1000.0];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}
// 根据时间获取时间戳
+ (NSString *)timeStampWithDade:(NSDate *)date{
    NSTimeInterval a=[date timeIntervalSince1970]*1000; // *1000 是精确到毫秒,不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", a]; //转为字符型
    return timeString;
}
// 获取当前时间的时间戳
+ (NSInteger)getNowTimestamp{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; //
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    
    NSDate *datenow = [NSDate date];//现在时间
    
    //时间转时间戳的方法:
    NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];
    
    return timeSp;
}
// 计算两个日期间隔天数
+ (NSInteger)getDaysFrom:(NSString *)beginDate To:(NSString *)endDate{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    
    // 将字符串日期 转换为 NSDate 类型,并去掉时分秒信息
    NSDate *fromDate = [NSDate worldTimeToChina:[formatter dateFromString:[beginDate substringToIndex:10]]];
    NSDate *toDate = [NSDate worldTimeToChina:[formatter dateFromString:[endDate substringToIndex:10]]];

    NSTimeInterval time = [toDate timeIntervalSinceDate:fromDate];
    NSInteger days = ((int)time) / (3600 * 24);

    return days;
}
// 获取当前是星期几
+ (NSInteger)getNowWeekday {
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday |
    NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDate *now = [NSDate date];
    // 在真机上需要设置区域,才能正确获取本地日期,中国区代码:zh_CN
    calendar.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    comps = [calendar components:unitFlags fromDate:now];
    
    return [comps weekday];
}
// 获取当前是几月几日
+ (NSString *)getTodayStr{
    NSCalendar *cal = [NSCalendar currentCalendar];
    
    NSDateComponents *components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];
    NSDate *today = [cal dateFromComponents:components];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"MM月dd日";
    return [formatter stringFromDate:today];
}
// 计算时间差
+ (NSTimeInterval)timeInterValWithDate:(NSString *)featureDate{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 设置日期格式,可以根据自己的需求随时调整,否则计算的结果为 nil
    formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    // 将字符串日期 转换为 NSDate 类型
    NSDate *endDate = [formatter dateFromString:featureDate];
    
    return [endDate timeIntervalSinceDate:[NSDate new]];
}
// 字符串转日期格式
+ (NSDate *)stringToDate:(NSString *)dateString withDateFormat:(NSString *)format{

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    
    NSDate *date = [dateFormatter dateFromString:dateString];
    return date;
}
// 将世界时间转化为中国区时间
+ (NSDate *)worldTimeToChina:(NSDate *)date{
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    NSInteger interval = [timeZone secondsFromGMTForDate:date];
    NSDate *localeDate = [date  dateByAddingTimeInterval:interval];
    return localeDate;
}

内容整理自网络,如有侵权请联系删除。

联系作者:·DH_Fantasy 新浪微博·DH_Fantasy
版权声明:自由转载-非商用-非衍生-保持署名(CC BY-NC-ND 3.0)

你可能感兴趣的:(iOS NSDate 相关知识整理)