码迷,mamicode.com
首页 > 其他好文 > 详细

获取倒计时距离某一时间点的时间,判断身份证,电话号码格式是否正确的简单封装

时间:2015-11-13 22:11:31      阅读:532      评论:0      收藏:0      [点我收藏+]

标签:

#import "HelpToolsDataOrPhoneOrIDCard.h"

 

@implementation HelpToolsDataOrPhoneOrIDCard

 

 

+ (NSString *)getTimeFromThatTime:(NSDateComponents *)componets{

    //    创建一个日历对象,并用构造方法选择不同地区的时间NSCalendarIdentifierChinese

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

    //    创建一个NSDate ,把NSDateComponents转化成日期类型

    NSDate *destinationDate =  [calendar dateFromComponents:componets];

    //   设置你要显示的时间格式,年月日时分秒

    NSInteger units = NSCalendarUnitDay |NSCalendarUnitHour|NSCalendarUnitMinute | NSCalendarUnitSecond;

    

    

    NSDateComponents *compoents1 = [calendar components:units fromDate:[NSDate date] toDate:destinationDate options:0];

    

    NSLog(@"%@",[NSDate date]);

    

    NSInteger days = [compoents1 day];

    NSInteger hour = [compoents1 hour];

    NSInteger min = [compoents1 minute];

    NSInteger second = [compoents1 second];

    

    NSString *strLabel = [NSString stringWithFormat:@"还有%ld%ld小时%ld分钟%ld",days,hour,min,second];

    

    return strLabel;

}

 

 

+(NSString *)getDayStringWithTime:(NSString *)time{

    //将传入时间转化成需要的格式

    static NSDateFormatter* formater = nil;

    if (formater == nil) {

        formater=[[NSDateFormatter alloc] init];

        [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    }

    

    

    NSDate *fromdate=[formater dateFromString:time];

    NSTimeZone *fromzone   = [NSTimeZone systemTimeZone];

    NSInteger frominterval = [fromzone secondsFromGMTForDate:fromdate];

    NSDate *fromDate = [fromdate  dateByAddingTimeInterval: frominterval];

    

    //获取当前时间

    NSDate *date       = [NSDate date];

    NSTimeZone *zone   = [NSTimeZone systemTimeZone];

    NSInteger interval = [zone  secondsFromGMTForDate: date];

    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];

    

    double intervalTime = [fromDate timeIntervalSinceReferenceDate] - [localeDate timeIntervalSinceReferenceDate];

    long lTime = (long)intervalTime;

    NSInteger iDays  = lTime/86400;

    NSInteger iHours = (lTime-iDays*86400)/3600;

    if (iDays == 0 ) {

        NSInteger iMinute = (lTime-iHours*3600)/60;

        return [NSString stringWithFormat:@"%ld小时%ld",iHours,iMinute];

    }else{

        NSInteger iMinute = (lTime-iDays*86400-iHours*3600)/60;

        return [NSString stringWithFormat:@"%ld%ld小时%ld",iDays,iHours,iMinute];

    }

}

 

// 判断是否有效身份证号码

+ (BOOL)isIDNumberValid:(NSString*)idnumber

{

    if ([idnumber length] != 15 &&

        [idnumber length] != 18)

    {

        return NO;

    }

    

    NSLog(@"%@",idnumber);

    int provCode = [[idnumber substringToIndex:2] intValue];

    if ( provCode < 11  ||

        (provCode > 15 && provCode < 21) ||

        (provCode > 23 && provCode < 31) ||

        (provCode > 37 && provCode < 41) ||

        (provCode > 46 && provCode < 50) ||

        (provCode > 54 && provCode < 61) ||

        (provCode > 65) )

    {

        // 行政区码错误

        NSLog(@"province code error");

        return NO;

    }

    

    NSUInteger len = [idnumber length];

    int year = 0;

    int month = 0;

    int day = 0;

    if (len == 15)

    {

        year = [[idnumber substringWithRange:NSMakeRange(6, 2)] intValue];

        if (year > 20)

        {

            year = 1900 + year;

        }

        else

        {

            year = 2000 + year;

        }

        

        month = [[idnumber substringWithRange:NSMakeRange(8, 2)] intValue];

        day = [[idnumber substringWithRange:NSMakeRange(10, 2)] intValue];

    }

    else if (len == 18)

    {

        year = [[idnumber substringWithRange:NSMakeRange(6, 4)] intValue];

        month = [[idnumber substringWithRange:NSMakeRange(10, 2)] intValue];

        day = [[idnumber substringWithRange:NSMakeRange(12, 2)] intValue];

    }

    if (year < 1900 ||

        year > 2200)

    {

        // 年份错误

        NSLog(@"year error");

        return NO;

    }

    

    if (month < 1 ||

        month > 12)

    {

        // 月份错误

        NSLog(@"month error");

        return NO;

    }

    

    if (day < 1 ||

        day > 31)

    {

        // 日期错误

        NSLog(@"day error");

        return NO;

    }

    

    // 检查最后一位

    NSRange xrange = [idnumber rangeOfString:@"x"];

    NSRange Xrange = [idnumber rangeOfString:@"X"];

    if ((xrange.length == 1 && xrange.location != len - 1) ||

        (Xrange.length == 1 && Xrange.location != len - 1))

    {

        NSLog(@"x position error");

        return NO;

    }

    

    if (len == 15)

    {

        return YES;

    }

    

    int Wi[17] = {7, 9, 10, 5, 8 ,4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

    int lastBit[11] = {1, 0, ‘x‘, 9, 8, 7, 6, 5, 4, 3, 2};

    int sum = 0;

    NSMutableString * idnum18 = [[NSMutableString alloc] initWithString:idnumber];

    

    for (int i = 0; i < 17; i++)

    {

        int nVal = [[idnum18 substringWithRange:NSMakeRange(i, 1)] intValue];

        sum += nVal*Wi[i];

    }

    

    int ret = sum%11;

    NSLog(@"ret=%d",ret);

    if ((ret == 2 && [[[idnum18 substringWithRange:NSMakeRange(17, 1)] lowercaseString] isEqualToString:@"x"]) ||

        lastBit[ret] == [[idnum18 substringWithRange:NSMakeRange(17, 1)] intValue])

    {

        // [idnum18 release];

        return YES;

    }

    else

    {

        // 校验码错误

        NSLog(@"verify code error");

        // [idnum18 release];

        return NO;

    }

    

    // [idnum18 release];

    return NO;

}

 

 

/*手机号码验证 MODIFIED BY HELENSONG*/

+ (BOOL)isMobileNumber:(NSString *)mobileNum

{

    /**

     

     * 手机号码

     

     * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188,1705

     

     * 联通:130,131,132,152,155,156,185,186,1709

     

     * 电信:133,1349,153,180,189,1700

     

     */

    

    NSString * MOBILE = @"^1((3//d|5[0-35-9]|8[025-9])//d|70[059])\\d{7}$";//总况

    

    

    

    /**

     

     10         * 中国移动:China Mobile

     

     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,1881705 178

     

     12         */

    

    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[23478])\\d|705|178[0-9])\\d{7}$";

    

    /**

     

     15         * 中国联通:China Unicom

     

     16         * 130,131,132,152,155,156,185,186,1709

     

     17         */

    

    NSString * CU = @"^1((3[0-2]|5[256]|8[56])\\d|709|76[0-9])\\d{7}$";

    

    /**

     

     20         * 中国电信:China Telecom

     

     21         * 133,1349,153,180,189,1700

     

     22         */

    

    NSString * CT = @"^1((33|53|8[019])\\d|349|700|77[0-9])\\d{7}$";

    

    

    /**

     25 * 大陆地区固话及小灵通

     26 * 区号:010,020,021,022,023,024,025,027,028,029

     27 * 号码:七位或八位

     28 */

    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";

    

    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];

    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];

    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];

    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];

    

    if (([regextestmobile evaluateWithObject:mobileNum] == YES)

        || ([regextestcm evaluateWithObject:mobileNum] == YES)

        || ([regextestct evaluateWithObject:mobileNum] == YES)

        || ([regextestcu evaluateWithObject:mobileNum] == YES))

    {

        return YES;

    }

    else

    {

        return NO;

    }

    

}

 

//以上集合一起,并兼容14开头的

 

//+ (BOOL)isMobileNumber:(NSString *)mobileNum

//{

//    NSString * phoneRegex = @"^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$";

//    BOOL isMatch = [mobileNum isMatchedByRegex:phoneRegex];

//    return isMatch;

//}

 

 

 

 

@end

获取倒计时距离某一时间点的时间,判断身份证,电话号码格式是否正确的简单封装

标签:

原文地址:http://www.cnblogs.com/h-tao/p/4963314.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!