How to handle different date time formats using NSDateFormatter

后端 未结 4 1917
时光说笑
时光说笑 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;
    }
    
    0 讨论(0)
  • 2020-12-15 06:02

    A date formatter can only handle one format at a time. You need to take this approach:

    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [f dateFromString:@"2010-01-10 13:55:15"];
    
    NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
    [f2 setDateFormat:@"d. MMMM YYYY"];
    NSString *s = [f2 stringFromDate:date];
    

    s will now be "10. January 2010"

    0 讨论(0)
  • 2020-12-15 06:03

    You want to use [NSFormatter dateFromString:] to convert your string-based date to an NSDate instance. From there you want to use stringFromDate with the NSDate, not the string as you have written above. I'm not sure about using the same NSDateFormatter for both parsing and formatting - you may need two separate instances to handle the different format styles.

    0 讨论(0)
  • 2020-12-15 06:05

    First off, make sure you set the behavior to 10.4 - more modern, works better in my experience.

    [dateTimeFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
    

    Next, you can't use the same format to parse and format if they have 2 different string representations, so use 2 formatters, or change the string format between parsing and then formatting.

    Also make sure you consider the formatting options:

    http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1

    lowercase is e is the day of week, lowercase d is the day of the month.

    For month, use MMMM, not B.

    0 讨论(0)
提交回复
热议问题