NSDate的一些扩展

//从1970年开始到现在经过了多少秒

+(NSString *)getTimeSp
{
    NSString *time;
    NSDate *fromdate=[NSDate date];
    time = [NSString stringWithFormat:@"%f",[fromdate timeIntervalSince1970]];
    return time;
}

//转毫秒

+(NSString *)getTimeSpmillisecond
{
    NSString *time;
    NSDate *fromdate=[NSDate date];
    time = [NSString stringWithFormat:@"%f",[fromdate timeIntervalSince1970]*1000];
    return time;
}

//将时间戳转换成NSDate,转换的时间是格林尼治时间

+(NSDate *)changeSpToTime:(NSString*)spString
{
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[spString intValue]];
    NSLog(@"%@",confromTimesp);
    return confromTimesp;
}

//将时间戳转换成NSDate,加上时区偏移。这个转换之后是北京时间

+(NSDate*)zoneChange:(NSString*)spString
{
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[spString intValue]];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:confromTimesp];
    NSDate *localeDate = [confromTimesp  dateByAddingTimeInterval: interval];
    NSLog(@"%@",localeDate);
    return localeDate;
}

//比较给定NSDate与当前时间的时间差,返回相差的秒数

+(long)timeDifference:(NSDate *)date
{
    NSDate *localeDate = [NSDate date];
    long difference =fabs([localeDate timeIntervalSinceDate:date]);
    return difference;
}

//比较两个NSDte之间的时间差,返回相差秒数

+(long)timeDifferents:(NSDate*)startTime andEndDate:(NSDate*)endDate{
    
    long difference =fabs([endDate timeIntervalSinceDate:startTime]);
    return difference;
}

//将NSDate按yyyy-MM-dd HH:mm:ss格式时间输出

+(NSString*)nsdateToString:(NSDate *)date Andformat:(NSString *)formatStr
{
    NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:formatStr];
    NSString* string=[dateFormat stringFromDate:date];
    NSLog(@"%@",string);
    return string;
}

//将yyyy-MM-dd HH:mm:ss格式时间转换成时间戳

+(long)changeTimeToTimeSp:(NSString *)timeStr Andformat:(NSString *)formatStr
{
    long time;
    NSDateFormatter *format=[[NSDateFormatter alloc] init];
    [format setDateFormat:formatStr];
    NSDate *fromdate=[format dateFromString:timeStr];
    time= (long)[fromdate timeIntervalSince1970];
    NSLog(@"%ld",time);
    return time;
}

//获取当前时间的时间戳加随机串

+ (NSString *)getTimeSpNowAndRandom{

    return [NSString stringWithFormat:@"%@-%u",[self getTimeSp], (arc4random() % 900) + 100];// 100-999随机数

}

//获取当前系统的yyyy-MM-dd HH:mm:ss格式时间

{
    NSDate *fromdate=[NSDate date];
    NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:formatStr];
    NSString* string=[dateFormat stringFromDate:fromdate];
    return string;
}

//将当前时间转化为年月日格式


{
    NSDate *date = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSInteger unitFlags =  NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday |
    NSCalendarUnitHour  | NSCalendarUnitMinute | NSCalendarUnitSecond;
    
    NSDateComponents *comps = [calendar components:unitFlags fromDate:date];
    NSInteger year = [comps year];
    NSInteger month = [comps month];
    NSInteger day = [comps day];
    NSInteger hour = [comps hour];
    NSInteger min = [comps minute];
    NSInteger sec = [comps second];
    
    NSString *string = [NSString stringWithFormat:@"%ld年%ld月%ld日%ld时%ld分%ld秒",(long)year,(long)month,(long)day,(long)hour,(long)min,(long)sec];
    
    NSLog(@"%@",string);
    return string;
}

/* 计算消息时间距离当前时差 */

+ (NSString *)compareWithDateNow:(NSString *)oldtime {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
    [dateFormatter setTimeZone:localTimeZone];
    NSTimeInterval timeInterval= [oldtime doubleValue] / 1000;
    NSDate *oldDate= [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDate *dateNow = [NSDate date];
    NSCalendar* chineseClendar = [[NSCalendar alloc ] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSUInteger unitFlags = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear;
    NSDateComponents *cps = [chineseClendar components:unitFlags fromDate:oldDate  toDate:dateNow  options:0];
    //判断时间与现在时间的差值
    NSInteger diffYear = [cps year];
    
    NSInteger diffMon = [cps month];
    
    NSInteger diffDay = [cps day];
    
    NSInteger diffHour = [cps hour];
    
    NSInteger diffMin = [cps minute];
    
    if(diffYear == 0)
    {
        if(diffDay == 0 && diffMon == 0 && diffHour == 0 && diffMin < 10) {
            return @"刚刚";
        }
        else if(diffDay ==0 && diffMon == 0 && diffHour == 0 && diffMin < 60 && diffMin > 10) {
            return @"10分钟前";
        }
        else if(diffDay == 0 && diffMon == 0 && diffHour >= 1 && diffHour < 2) {
            return @"1个小时前";
        }
        else if(diffDay == 0 && diffMon == 0 && diffHour > 2 && diffHour < 24) {
            NSTimeInterval timeInterval= [oldtime doubleValue] / 1000;
            NSDate *resDate= [NSDate dateWithTimeIntervalSince1970:timeInterval];
            [dateFormatter setDateFormat:@"HH:mm"];
            NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
            [dateFormatter setTimeZone:localTimeZone];
            NSString *strTime = [dateFormatter stringFromDate:resDate];
            return strTime;
        }else {
            NSTimeInterval timeInterval= [oldtime doubleValue] / 1000;
            NSDate *resDate= [NSDate dateWithTimeIntervalSince1970:timeInterval];
            [dateFormatter setDateFormat:@"MM-dd"];
            NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
            [dateFormatter setTimeZone:localTimeZone];
            NSString *strTime = [dateFormatter stringFromDate:resDate];
            return strTime;
        }
    }else {
        NSTimeInterval timeInterval= [oldtime doubleValue] / 1000;
        NSDate *resDate= [NSDate dateWithTimeIntervalSince1970:timeInterval];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
        [dateFormatter setTimeZone:localTimeZone];
        NSString *strTime = [dateFormatter stringFromDate:resDate];
        return strTime;
    }
}

//获取当前这一天所在周的开始时间

+(NSDate *)getCurrentDayStartDayWeekAndDate:(NSDate *)today{
    
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];// you can use your format.
    
    //Week Start Date
    
    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:today];
    
    NSInteger dayofweek = [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:today] weekday];// this will give you current day of week
    
    [components setDay:([components day] - ((dayofweek) - 3))];// for beginning of the week.
    
    NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
    
    
    return beginningOfWeek;
}

//获取当前这一天所在周的结束时间

+(NSDate *)getCurrentDayEndDayWeekAndDate:(NSDate *)today{
    
    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSCalendar *gregorianEnd = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSDateComponents *componentsEnd = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:today];
    
    NSInteger Enddayofweek = [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:today] weekday];// this will give you current day of week
    
    [componentsEnd setDay:([componentsEnd day]+(7-Enddayofweek)+2)];// for end day of the week
    
    NSDate *EndOfWeek = [gregorianEnd dateFromComponents:componentsEnd];
    
    return EndOfWeek;
}

//获取年月日Formatter

+ (NSDateFormatter *)getDayFormatter{
    NSTimeZone *sourceTimeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    NSDateFormatter *dateFormat_first = [[NSDateFormatter alloc] init];
    [dateFormat_first setDateFormat:@"yyyy-MM-dd"];
    dateFormat_first.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    dateFormat_first.timeZone= sourceTimeZone;
    return dateFormat_first;
}

//将日期转化为本地时间

+ (NSDate *)localeDate:(NSDate *)date

{
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    
    NSInteger interval = [zone secondsFromGMTForDate: date];
    
    NSDate *localeDate = [date dateByAddingTimeInterval: interval];
    
    return localeDate;
}

//获取本月天数

+(NSInteger)returnMonthDayNum{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:[NSDate date]];
    
    NSUInteger numberOfDaysInMonth = range.length;
    return numberOfDaysInMonth;
}

//获取每周的第一天和最后一天

+(NSArray *)getFirstAndLastDayOfThisWeek
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dateComponents = [calendar components:NSCalendarUnitWeekday  | kCFCalendarUnitDay | NSCalendarUnitMonth| NSCalendarUnitYear fromDate:[NSDate date]];
    NSInteger weekday = [dateComponents weekday]-1;   //第几天(从周一开始)
    
    NSInteger firstDiff,lastDiff;
    if (weekday == 0) {
        firstDiff = -6;
        lastDiff = 0;
    }else {
        firstDiff =  - weekday + 2;
        lastDiff = 8 - weekday;
    }
    NSInteger day = [dateComponents day];
    NSDateComponents *firstComponents = [calendar components:NSCalendarUnitWeekday  | kCFCalendarUnitDay | NSCalendarUnitMonth  | NSCalendarUnitYear fromDate:[NSDate date]];
    [firstComponents setDay:day+firstDiff];
    NSDate *firstDay = [calendar dateFromComponents:firstComponents];
    
    NSDateComponents *lastComponents = [calendar components:NSCalendarUnitWeekday  | kCFCalendarUnitDay | NSCalendarUnitMonth| NSCalendarUnitYear fromDate:[NSDate date]];
    [lastComponents setDay:day+lastDiff];
    NSDate *lastDay = [calendar dateFromComponents:lastComponents];
    return [NSArray arrayWithObjects:firstDay,lastDay, nil];
}

//将yyyy-MM-dd HH:mm:ss格式时间转换成时间戳

+ (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}

按时间格式将时间戳(毫秒)转换成指定时间

+ (NSString *)timeWithTimeIntervalString:(NSString *)timeString withFormat:(NSString *)format
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}

按时间格式将时间戳(秒)转换成指定时间

+ (NSString *)timeWithTimeInterval:(NSInteger)timeinterval withFormat:(NSString *)format
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *zone = [NSTimeZone localTimeZone];
    formatter.timeZone = zone;
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format];
    
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:timeinterval];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}

//获取现在yyyy-MM-dd的格式时间

+ (NSString *)getYYYYMMDDDateNow {
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone systemTimeZone];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate date];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}

//获取当前时间格式

+ (NSString *)getCurrentTimeWithFormat:(NSString *)format{
    // 格式化时间
       NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
       formatter.timeZone = [NSTimeZone systemTimeZone];
       [formatter setDateStyle:NSDateFormatterMediumStyle];
       [formatter setTimeStyle:NSDateFormatterShortStyle];
       [formatter setDateFormat:format];
       
       // 毫秒值转化为秒
       NSDate* date = [NSDate date];
       NSString* dateString = [formatter stringFromDate:date];
       return dateString;
    
}

//将时间戳转换成yyyy-MM-dd HH:mm:ss格式时间

+ (NSString *)timeWithNormalTimeIntervalString:(NSString *)timeString {
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}

//获取现在年月的时间格式

+ (NSString *)timeWithYearTimeIntervalString:(NSString *)timeString {
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy年MM月"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}

//获取现在月号的时间格式

+ (NSString *)timeWithMonthTimeIntervalString:(NSString *)timeString {
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"MM月dd号"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}

//获取现在号的时间格式

+ (NSString *)timeWithDayTimeIntervalString:(NSString *)timeString {
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"dd号"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
    NSString* dateString = [formatter stringFromDate:date];
    return dateString;
}

/// 将秒数转换成时分秒的格式(x时x分x秒)

+ (NSString *)transformHourMinSecWithCount:(long)count{

    NSString *hourString;
    NSString *minString;
    NSString *secondString;
    if (count / 3600 >0 ) {
        hourString = [NSString stringWithFormat:@"%ld时",count / 3600];
    }else{// <3600
        hourString = @"";
    }
    if (count % 3600  / 60 >0) {
        minString = [NSString stringWithFormat:@"%ld分钟",count %3600/60];
    }else{
        minString = @"";
    }
    if (count %3600 %60 > 0) {
        secondString = [NSString stringWithFormat:@"%ld秒",count % 3600 % 60];
    }else{
        secondString = @"";
    }
    return [NSString stringWithFormat:@"%@%@%@",hourString,minString,secondString];
}

/// 将秒数转换成时分秒的格式(HH:mm:ss)

+ (NSString *)transformHourMinSecWithCount2:(long)count{

    NSString *hourString;
    NSString *minString;
    NSString *secondString;
    if (count / 3600 >0 ) {
        NSInteger hours = count / 3600;
        hourString = hours<10?[NSString stringWithFormat:@"0%ld",hours]:[NSString stringWithFormat:@"%ld",hours];//[NSString stringWithFormat:@"%ld",];
    }else{// <3600
        hourString = @"00";
    }
    if (count % 3600  / 60 >0) {
        NSInteger min = count %3600/60;
         minString = min <10? [NSString stringWithFormat:@"0%ld",min]:[NSString stringWithFormat:@"%ld",min];//[NSString stringWithFormat:@"%@:",min<10?@"0"];
    }else{
        minString = @"00";
    }
    if (count %3600 %60 > 0) {
        NSInteger second = count % 3600 % 60;
         secondString = second <10?[NSString stringWithFormat:@"0%ld",second]:[NSString stringWithFormat:@"%ld",second];//[NSString stringWithFormat:@"%ld",count % 3600 % 60];
    }else{
        secondString = @"00";
    }
    return [NSString stringWithFormat:@"%@:%@:%@",hourString,minString,secondString];
}

你可能感兴趣的:(NSDate的一些扩展)