ios时间戳和日期的一些转换,如具体时间,年龄,星座等等

1、

///将时间戳转为日期时间

/// @param timestamp 时间戳

/// @param dateFormat 日期样式 YYYY-MM-dd HH:mm:ss

+ (NSString *)getTimeFromTimestamp:(NSInteger )timestamp andDateFormat:(NSString *)dateFormat{

    //将对象类型的时间转换为NSDate类型

    NSDate * myDate = [NSDate dateWithTimeIntervalSince1970:timestamp];

    //设置时间格式

    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];

    [formatter setDateFormat:dateFormat];

    //将时间转换为字符串

    NSString *timeStr = [formatter stringFromDate:myDate];

    return timeStr;

}

2、

/// 将时间戳转具体日期如刚刚几分钟之前等

/// @param timestamp 时间戳

+ (NSString *)  timeStr:(NSInteger )timestamp{

    // 创建日历对象

    NSCalendar *calendar = [NSCalendar currentCalendar];

    // 获取当前时间

    NSDate *currentDate = [NSDate date];

    // 获取当前时间的年、月、日。利用日历

    NSDateComponents *components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:currentDate];

    NSInteger currentYear   = components.year;

    NSInteger currentMonth  = components.month;

    NSInteger currentDay    = components.day;

    NSInteger currentHour   = components.hour;

    NSInteger currentMinute = components.minute;

    NSInteger currentSecond  = components.second;

    // 获取消息发送时间的年、月、日

    NSDate *msgDate = [NSDate dateWithTimeIntervalSince1970:timestamp];

    components = [calendar components:NSCalendarUnitYear| NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:msgDate];

    CGFloat msgYear     = components.year;

    CGFloat msgMonth    = components.month;

    CGFloat msgDay      = components.day;

    NSInteger msgHour   = components.hour;

    NSInteger msgMinute = components.minute;

    NSInteger msgSecond = components.second;

    // 进行判断

    NSDateFormatter *dateFmt = [[NSDateFormatter alloc] init];

    if (currentYear == msgYear && currentMonth == msgMonth && currentDay == msgDay) {

         //今天

        if (currentHour == msgHour) {

            

            if (currentMinute == msgMinute || currentMinute-1 ==msgMinute) {

                return langaugeString(@"commonPart", @"just");//刚刚

            }else  if (currentMinute-1 >msgMinute) {

                return [NSString stringWithFormat:@"%ld%@",(currentMinute*60+currentSecond - msgMinute*60-msgSecond)/60,langaugeString(@"commonPart", @"minutes")];// xx 分钟前

            }

        }else if(currentHour-1 == msgHour){

                //3点10分      1点11分

            if (currentMinute < msgMinute) {

                return [NSString stringWithFormat:@"%ld%@",(60*60+currentMinute*60+currentSecond - msgMinute*60-msgSecond)/60,langaugeString(@"commonPart", @"minutes")];// xx 分钟前

            }else{

                return [NSString stringWithFormat:@"%ld%@",(currentHour*60+currentMinute - msgHour*60-msgMinute)/60,langaugeString(@"commonPart", @"hours")]; //xx 小时前

            }

        }else if(currentHour-1 > msgHour){

            

            return [NSString stringWithFormat:@"%ld%@",(currentHour*60+currentMinute - msgHour*60-msgMinute)/60,langaugeString(@"commonPart", @"hours")];//xx 小时前

        }

    }else if (currentYear == msgYear && currentMonth == msgMonth && currentDay-1 == msgDay&&currentHour<msgHour){

        

         return [NSString stringWithFormat:@"%ld%@",(24*60-(msgHour*60+ msgMinute- currentHour*60-currentMinute))/60,langaugeString(@"commonPart", @"hours")];//xx 小时前



    }else{

        if (currentYear == msgYear) {

            

             dateFmt.dateFormat = @"MM-dd";// MM月dd日

        }else{

         

            dateFmt.dateFormat = @"yyyy-MM-dd";

        }

    }

    // 返回处理后的结果

    NSString *returnTimeString = [dateFmt stringFromDate:msgDate];

    return returnTimeString;

}

3//获取年龄

+ (NSString *) getAge:(NSString *)birthday {

    NSDateFormatter*df = [[NSDateFormatter alloc] init];//格式化

    [df setDateFormat:@"yyyy/MM/dd"];

    NSString *dateStr = birthday;

    NSTimeInterval dateDiff = [[df dateFromString:dateStr] timeIntervalSinceNow];

    long age = fabs(dateDiff/(60*60*24))/365;

    NSLog(@"年龄是:%@",[NSString stringWithFormat:@"%ld岁",age]);

        

    NSString *year = [dateStr substringWithRange:NSMakeRange(0, 4)];

    NSString *month = [dateStr substringWithRange:NSMakeRange(5, 2)];

    NSString *day = [dateStr substringWithRange:NSMakeRange(dateStr.length-2, 2)];

    NSLog(@"出生于%@年%@月%@日", year, month, day);

        

    NSDate *nowDate = [NSDate date];

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601];

    NSDateComponents *compomemts = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:nowDate];

    NSInteger nowYear = compomemts.year;

    NSInteger nowMonth = compomemts.month;

    NSInteger nowDay = compomemts.day;

    NSLog(@"今天是%ld年%ld月%ld日", nowYear, nowMonth, nowDay);

        

      // 计算年龄

    NSInteger userAge = nowYear - year.intValue - 1;

    if ((nowMonth > month.intValue) || (nowMonth == month.intValue && nowDay >= day.intValue)) {

            userAge++;

    }

    NSLog(@"用户年龄是%ld",userAge);

    return [NSString stringWithFormat:@"%ld",(long)userAge];

}

4/// 将日期转为时间戳

/// @param CurrentTime 所转的时间

+ (NSString *)getTimestampFromTime:(NSString *)CurrentTime{

    

    ZTLog(@"formatTime - - - - - -%@",CurrentTime);

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateStyle:NSDateFormatterMediumStyle];

    [formatter setTimeStyle:NSDateFormatterShortStyle];

    [formatter setDateFormat:@"yyyy/MM/dd"]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制

    //设置时区选择北京时间

    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];

    [formatter setTimeZone:timeZone];

    NSDate* date = [formatter dateFromString:CurrentTime]; //------------将字符串按formatter转成nsdate

    //时间转时间戳的方法:

    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue] * 1000;

    ZTLog(@"将某个时间转化成 时间戳timeSp:%ld",(long)timeSp); //时间戳的值

    return [NSString stringWithFormat:@"%ld",(long)timeSp];

    

}

5、/// 根据时间戳得到星座

/// @param birthdayTime 时间戳

+ (NSString *)getConstellatoryByBirthdayTime:(NSTimeInterval )birthdayTime{

    

    // 时间戳转为 date

    NSDate *birthdayDate = [NSDate dateWithTimeIntervalSince1970:birthdayTime/1000]; // 毫秒时间戳要/1000

    

    // 出生日期转换 年月日

    NSDateComponents *components1 = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:birthdayDate];

    NSInteger brithDateMonth = [components1 month];

    NSInteger brithDateDay   = [components1 day];

    

    NSString *astroString = @"魔羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";

    NSString *astroFormat = @"102123444543";

    NSString *result;

    

    if (brithDateMonth < 1||brithDateMonth > 12||brithDateDay < 1||brithDateDay > 31 ){

        return @"错误日期格式!";

    }

    

    if(brithDateMonth ==2 && brithDateMonth > 29 ){

        

        return @"错误日期格式!!";

    }else if(brithDateMonth == 4 || brithDateMonth == 6 || brithDateMonth == 9 || brithDateMonth == 11) {

        

        if (brithDateDay > 30) {

            return @"错误日期格式!!!";

        }

    }

    

    result = [NSString stringWithFormat:@"%@",[astroString substringWithRange:NSMakeRange(brithDateMonth*2-(brithDateDay < [[astroFormat substringWithRange:NSMakeRange((brithDateMonth-1), 1)] intValue] - (-19))*2,2)]];

    

    return result;

    /*

     白羊座: 3月21日~4月20日 (Aries)

     金牛座: 4月21日~5月21日 (Taurus)

     双子座: 5月22日~6月21日 (Gemini)

     巨蟹座: 6月22日~7月22日 (Cancer)

     狮子座: 7月23日~8月23日 (Leo)

     处女座: 8月24日~9月23日 (Virgo)

     天秤座: 9月24日~10月23日 (Libra)

     天蝎座: 10月24日~11月22日 (Scorpio)

     射手座: 11月23日~12月21日 (Sagittarius)

     摩羯座: 12月22日~1月20日 (Capricorn)

     水瓶座: 1月21日~2月19日 (Aquarius)

     双鱼座: 2月20日~3月20日 (Pisces)

     */

}

/// 获取当前时间的时间戳

+ (NSString *)getNowTimeTimestamp

{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;

    [formatter setDateStyle:NSDateFormatterMediumStyle];

    [formatter setTimeStyle:NSDateFormatterShortStyle];

    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制

    //设置时区,这个对于时间的处理有时很重要

    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];

    [formatter setTimeZone:timeZone];

    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式

    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]*1000];

    return timeSp;

}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值