How to handle different date time formats using NSDateFormatter

后端 未结 4 1916
时光说笑
时光说笑 2020-12-15 05:18

I have a String with a datetime format: \"YYYY-MM-DD HH:MM:SS\".

I use this in my source code:

NSDateFormatter *formatter = [[[NSDateFormatter alloc]         


        
4条回答
  •  囚心锁ツ
    2020-12-15 05:53

    Here are a few examples of working with data formatters from my code. You should be able to take any one of these functions and tweak it for your format.

    USAGE

    NSDate *today = [NSDate date];
    NSDateFormatter *dateFormatter = [Constants getTitleDateFormatter];
    NSString *dateString = [dateFormatter stringFromDate:today];        
    [dateFormatter release];
    

    FUNCTIONS

    + (NSDateFormatter *) getDateFormatterWithTimeZone {
      //Returns the following information in the format of the locale:
      //YYYY-MM-dd HH:mm:ss z (Z is time zone)
      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
      [dateFormatter setLocale:[NSLocale currentLocale]];
      [dateFormatter setDateStyle:NSDateFormatterShortStyle];
      [dateFormatter setTimeStyle:NSDateFormatterLongStyle];
      return dateFormatter;
    

    }

    + (NSDateFormatter *)dateFormatterWithoutYear {
      NSDateFormatter *dateFormatter = [Constants getDateFormatterWithTimeZone];
      [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
      NSString *format = [dateFormatter dateFormat];
      format = [format stringByReplacingOccurrencesOfString:@"/yy" withString:@""];
      NSRange secondSpace;
      secondSpace.location = format.length-2;
      secondSpace.length = 1;
      format = [format stringByReplacingCharactersInRange:secondSpace withString:@""];
      [dateFormatter setDateFormat:format];
      return dateFormatter;
    }
    
    + (NSDateFormatter *) dateFormatterMonthDayOnly {
        NSDateFormatter *dateFormatter = [Constants getDateFormatterWithTimeZone];
        [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
        NSString *format = [dateFormatter dateFormat];
        format = [format stringByReplacingOccurrencesOfString:@"/yy" withString:@""];
        NSRange range;
        range.location = 0;
        range.length = 3;
        format = [format substringWithRange:range];
        [dateFormatter setDateFormat:format];
        return dateFormatter;
    }
    
    + (NSDateFormatter *) getTitleDateFormatter {
        //Returns the following information in the format of the locale:
        //MM-dd-yyyy hh:mm:ssa
        NSDateFormatter *dateFormatter = [Constants getDateFormatterWithTimeZone];
        [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
        NSString *format = [dateFormatter dateFormat];
        NSRange secondSpace;
        secondSpace.location = format.length-2;
        secondSpace.length = 1;
        format = [format stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
        format = [format stringByReplacingCharactersInRange:secondSpace withString:@""];
        [dateFormatter setDateFormat:format];   
        return dateFormatter;
    }
    

提交回复
热议问题