“This is the 25th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

The introduction

The application scenario of iOS time filtering is as follows: Report time filtering

I Acquisition month

1.1 Obtaining last Month

  • usage
        self.viewModel.multipleSwitchCellTableViewCellModel.MonthlyDateStr = [QCT_Common strdate4lastMonthlyStrYYMM];

Copy the code
  • strdate4lastMonthlyStrYYMM
/** Get last month's string */
+ (NSString *)strdate4lastMonthlyStrYYMM{
    
    
    
    
    NSDate *currentDate = [NSDate date];
        
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
        NSDateComponents *lastMonthComps = [[NSDateComponents alloc] init];
    // [lastMonthComps setYear:1]; // Year = 1 Indicates the date one year ago. Year = -1 indicates the date one year ago
        [lastMonthComps setMonth:- 1];
        NSDate *newdate = [calendar dateByAddingComponents:lastMonthComps toDate:currentDate options:0];
    
    

    NSString *tmpbirthday = @ "";
    tmpbirthday  = [QCT_Common date4YYMM:newdate];
            NSLog(@"date str = %@", tmpbirthday);
    // NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // [formatter setDateFormat:@"yyyy-MM"];
        // NSString *dateStr = [formatter stringFromDate:newdate];

    return tmpbirthday;
}


Copy the code

1.2 Obtaining this Month

/** Gets the current month string */
+ (NSString *)strdate4TodaydateMonthlyStrYYMM{
    
    
    
    NSDate *lastDay =[NSDate date];//
    
// NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:[NSDate date]]; The day before / /

    
    

    NSString *tmpbirthday = @ "";

    tmpbirthday  = [QCT_Common date4YYMM:lastDay];
    
    return tmpbirthday;
    
}

Copy the code

II One day before acquisition

+ (NSString *)strdate4lastDayYYMMDD{
    
    NSDate *lastDay = [NSDate dateWithTimeInterval:- 24*60*60 sinceDate:[NSDate date]];The day before / /
    
    

    NSString *tmpbirthday = @ "";

    tmpbirthday  = [QCT_Common date4YYMMDD:lastDay];
    
    return tmpbirthday;

    
}

Copy the code

III NSDate->NSString

+ (NSString *)date4YYMMDD:(NSDate *)date{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    return [formatter stringFromDate:date];
}

+ (NSString *)date4YYMM:(NSDate *)date{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM"];
    return [formatter stringFromDate:date];
}


Copy the code

IV cases:

4.1 Constructing the refund slip number

Merchant refund Receipt No. (Merchant refund Receipt No., 32 characters in length, which can contain letters, must be unique in merchant system. If the same refund order number is requested for several times, the platform will treat it as one order and only refund once. If the refund is not successful, please use the original refund bill number to initiate again to avoid repeated refund.)

Merchant Refund Receipt Number (OUT_REFUND_NO) : yyMMddHHmmss + XXXXXX (SID) + Increment Serial number (reset every other day)

- (NSString *)out_refund_no{
    
    
    NSMutableString *str = [[NSMutableString alloc]initWithString: [QCT_Common getyyMMddHHmmss4TodayTime]];
    [str appendString:xxx.id];   
    [str appendString:QCTSession.shareQCTSession.snid];
    

    
    
    return str;
    
    
    
    
}

Copy the code
  • Snid (Incremental sequence number (reset every other day))

/** yyMMddHHmmss + XXXXXX (6 bits SID) + 5 bits serial */
- (NSString *)snid{
    
    
    // Get the object from the preference first, and store it if it doesn't exist
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary *k_sndi_keydic = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] dataForKey:k_sndi_key]];
    
    
     
    NSString *str =@ "00000";
    
    

    
    NSString *todayStr =     [QCT_Common  date4YYMMDD:[NSDate date]];
    
                              
                              
                              
                              
                              

    if([k_sndi_keydic.allKeys containsObject:k_sndi_date_key]){
        
        
        
        NSString *lastday = k_sndi_keydic[k_sndi_date_key];
        
        
        str = [[NSMutableString alloc]initWithString: k_sndi_keydic[k_sndi_key]];
        
        
        
        

        
        if([todayStr isEqualToString:lastday]){
            
            int d = [str intValue]+1;
            
            
            NSNumber *t = [NSNumber numberWithInt:d];
            
            
            
            
            str =  [QCT_Common addComplementspaces:t.description count:5 placeholder:@ "0" isdirectionRight:NO];//
            
            
            
        }else{
            
            
            
            str = @ "00000"; [k_sndi_keydic setValue:todayStr forKey:k_sndi_date_key]; }}else{
        
        k_sndi_keydic = [NSMutableDictionary dictionary];
        
        
        [k_sndi_keydic setValue:todayStr forKey:k_sndi_date_key];
        
        str = @ "00000";
        
        
        
        
        
    }
                              
    
    
    [k_sndi_keydic setValue:str forKey:k_sndi_key];
    
    
    
                              NSData *data  = [NSKeyedArchiver archivedDataWithRootObject: k_sndi_keydic];
                              
                              
                              
                              
                              [user setValue:data forKey:k_sndi_key];
                              
                              [user synchronize];

    
    
    
    NSLog(@"str:%@",FMSTR(@ "% @",str));/ / 02022
    
    
    
    return  str;
    
}


Copy the code
  • Complement a specific symbol scheme

Kunnan.blog.csdn.net/article/det…

/** STR: placeholder string count: placeholder: placeholder symbol isdirectionRight: placeholder */
+ (NSString*)addComplementspaces:(NSString*)str count:(NSInteger)count  placeholder:(NSString*)placeholder isdirectionRight:(BOOL)isdirectionRight{
    
    
    
    NSLog(@"addComplementspacesstr:%@",FMSTR(@ "% @",str));/ / 02022

    
    if (str.length>=count) {
        
        return str;
        
    }
    
    
    
    NSMutableString *tmp = [NSMutableString new];
    
    if(isdirectionRight){
        
        tmp = [[NSMutableString alloc]initWithString:str];/ / right up
        
        
    }
    
    
    NSInteger tmpcount = count -str.length;
    
    for (int i =0; i< tmpcount; i++) {
        
        [tmp appendString:placeholder];/ / is lacking
        
        
    }
    
    if(! isdirectionRight){ [tmp appendString:str];// Complete to the left

    }
    
    NSLog(@"addComplementspacesstr:%@",FMSTR(@ "% @",tmp.description));/ / 02022
    

    return tmp.description;
    
}


Copy the code

4.2 Obtaining Data Of the last 30 days

  • usage
                    NSInteger day  = [QCT_Common contrastTimeBackDayWithStartDate:weakSelf.filterViewModel.timeModel.startStr endDate:weakSelf.filterViewModel.timeModel.endStr];
                    NSLog(@"day: %ld", (long)day);
                    
                    
                    if (day > 29) {
                        [weakSelf showHUDMessage:QCTLocal(@"time_interval_should_not_exceed_30_days")];
                        return;
                    }

Copy the code
  • Utility methods
/** Calculate the interval between two times (days) @param start Start time @Param End end time @return Interval */
+ (NSInteger)contrastTimeBackDayWithStartDate:(NSString *)start endDate:(NSString *)end
{
    NSTimeInterval time = [self contrastTimeWithyyyyMMddStartDate:start endDate:end];
    NSInteger minute,second,hour,day;
    second=(NSInteger)time%1000;
    minute = (NSInteger)(time/60) %60;
    hour = (NSInteger)(time/3600) %24;
    day = (time/3600/24);
    return day;
}

/** Calculate the interval between two times (ms) @param start Start time @Param End End time @return Interval */
+ (NSTimeInterval)contrastTimeWithyyyyMMddStartDate:(NSString *)start endDate:(NSString *)end
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];

    
    NSDate *startDate = [dateFormatter dateFromString:start];
    NSDate *endDate = [dateFormatter dateFromString:end];
    NSTimeInterval time = [endDate timeIntervalSinceDate:startDate];
    return time;
}


Copy the code
  • Data filtering: Obtains the data of the last 7 days
        // Data filtering: Obtain the data of the last 7 days
        NSMutableString *str = [[NSMutableString alloc]initWithString: [QCT_Common get4TodayTimeWithDateFormat:@"yyyy-MM-dd HH:mm:ss"]].NSMutableArray *tmp = [NSMutableArray array];

        for (QCTReceiptDetailModel *obj in tmparr) {
            
            

            NSInteger day  = [QCT_Common contrastTimeBackDayWithStartDate:obj.completeTime endDate:str DateFormat:@"yyyy-MM-dd HH:mm:ss"];/ / endDate is big
            
            
            
            
            NSLog(@"day: %ld", (long)day);

            if (day > 6) {}else{ [tmp addObject:obj]; }}/** Calculate the interval between two times (days) @param start Start time @Param End end time @return Interval */
    + (NSInteger)contrastTimeBackDayWithStartDate:(NSString *)start endDate:(NSString *)end DateFormat:(NSString*)Format
    {
        
        NSTimeInterval time = [self contrastTimeWithStartDate:start endDate:end dateFormatter:Format];
        
        
        
        NSInteger minute,second,hour,day;
        second=(NSInteger)time%1000;
        minute = (NSInteger)(time/60) %60;
        hour = (NSInteger)(time/3600) %24;
        day = (time/3600/24);
        return day;
    }

    /** Calculate the interval between two times (ms) @param start Start time @Param End End time @return Interval */
    + (NSTimeInterval)contrastTimeWithStartDate:(NSString *)start endDate:(NSString *)end dateFormatter:(NSString*)Formatter
    {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    // [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        
        [dateFormatter setDateFormat:Formatter];
        
        
        
        NSDate *startDate = [dateFormatter dateFromString:start];
        NSDate *endDate = [dateFormatter dateFromString:end];
        NSTimeInterval time = [endDate timeIntervalSinceDate:startDate];
        return time;
    }


Copy the code

4.3 Time Formatting

  • 20210330105712 – > 2021-03-30 10:57:12

In the same way, if the date of January 01, 2020 is converted to 2020-01-01, NSDate can be converted to 2020-01-01 by using NSDateFormatter

            payinfomodel.createTime  = [QCT_Common strdatedateFormat:@"yyyy-MM-dd HH:mm:ss" fromDateFormat:@"yyyyMMddHHmmss" objstr:refund_time_];// Format the timestamp

Copy the code

+ (NSString *)strdatedateFormat:(NSString*)toDateFormat fromDateFormat:(NSString *)fromDateFormat objstr:(NSString*)objstr{

    
    NSString *tmpbirthday = @ "";
    
    if(! [NSStringQCTtoll isBlankString:objstr]){
        tmpbirthday = objstr;
        

        
        tmpbirthday  = [QCT_Common date4ateFormat:toDateFormat withdate:[QCT_Common dateWithFormatterString:tmpbirthday dateFormat:fromDateFormat]];
        
        
    }
    return tmpbirthday;
}


+ (NSString *)date4ateFormat:(NSString *)DateFormat withdate:(NSDate *)date{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:DateFormat];
    
    return [formatter stringFromDate:date];
}

+ (NSDate*)dateWithFormatterString:(NSString*)dateWithString dateFormat:(NSString*)dateFormat {
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    dateFormatter.dateFormat = dateFormat;
    NSDate *date = [dateFormatter dateFromString:dateWithString];
    return date;
    
}


Copy the code

see also

1. Please input the correct settlement card number [Luhn algorithm of bank Card Number validity verification] – Bank card number verification algorithm 2. Id card verification: [Verification age and whether verification meets the rules of ID card number generation]

Blog.csdn.net/z929118967/…

BOOL in OC is not as good as BOOL

BOOL means 1 is YES, so non-1 is NO. BOOL means 0 is false, so non-0 is true;

🍅 Contact author: iOS Reverse (public number: iosrev)


🍅 Author profile: CSDN blog expert certification 🏆 international Top 50, Huawei Cloud enjoy expert certification 🏆, iOS reverse public number master


🍅 Resume template, technical assistance. Pay attention to me, all for you.