How to format data from string variable

2019-07-15 10:49发布

I have a NSString that keeps date:

1900-01-01T11:00:00

I need to format it, so I have a formatter:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];

How to format this string now?

NSString* formatedDateString = [df ???];

3条回答
Anthone
2楼-- · 2019-07-15 11:34

example for getting current date :

-(void) getDate

{
    NSString *theDate;    
    NSString *theTime;
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];        
    NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
    [timeFormat setDateFormat:@"HH:mm:ss"];
    NSDate *now = [[NSDate alloc] init];
    theDate = [dateFormat stringFromDate:now];
    theTime = [timeFormat stringFromDate:now];
    //2012-09-21 02:00:00
    self.deviceCurrentTime=[NSString stringWithFormat:@"%@ %@",theDate,theTime];
}
查看更多
我想做一个坏孩纸
3楼-- · 2019-07-15 11:36

You need to create two date formatter, one for parsing, one for formatting; Your parsing format is "yyyy-MM-ddThh:mm:ss" and your output format is "dd/MM/yyyy hh:mm:ss a". So, something like this :

NSString * targetString = @"1900-01-01'T'11:00:00";

NSDateFormatter *parseFormat = [[NSDateFormatter alloc] init];
[parseFormat setDateFormat:@"yyyy-MM-ddThh:mm:ss"];
NSDate * date = [parseFormat dateFromString:targetString];

NSDateFormatter * outputFormat = [[NSDateFormatter alloc] init];
[outputFormat setDateFormat:@"dd/MM/yyyy hh:mm:ss a";
NSString * outputString = [parseFormat stringFromDate:date];
查看更多
迷人小祖宗
4楼-- · 2019-07-15 11:41

Create a date formatter, that returns a date object for for the given string, create a second date formatter, that returns a string in the desired format from that date.

NSDateformatter *inputFormatter = [[NSDateformatter alloc] init];
[inputFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];  
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
NSDate *date = [inputFormatter dateFromString:dateString];

NSDateformatter *outputFormatter = [[NSDateformatter alloc] init];
[outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[outputFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];  

NSString *outputString = [outputFormatter stringFromDate:date];

But you could also let the output date formatter decide the style in respect to the locale:

NSDateformatter *outputFormatter = [[NSDateformatter alloc] init];
[outputFormatter setDateStyle:NSDateFormatterShortStyle];  
[outputFormatter setTimeStyle:NSDateFormatterShortStyle]; 
NSString *outputString = [outputFormatter stringFromDate:date];

for a american locale, you should now cat the desired format.


if you want to enforce format "dd/MM/yyyy hh:mm:ss a" for the user, you should also set the locale of the output date formatter.

a complete command line example.

int main(int argc, const char * argv[])
{

    @autoreleasepool {


        NSString *dateString1 = @"2012-12-31T11:00:00";
        NSString *dateString2 = @"2012-12-31T14:00:00";
        NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
        [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
        [inputFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];

        NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
        [outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
        [outputFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];


        NSDate *date1 = [inputFormatter dateFromString:dateString1];
        NSDate *date2 = [inputFormatter dateFromString:dateString2];

        NSString *outputString1 = [outputFormatter stringFromDate:date1];
        NSString *outputString2 = [outputFormatter stringFromDate:date2];

        NSLog(@"%@", outputString1);
        NSLog(@"%@", outputString2);

    }
    return 0;
}

If you dont set the locale like [outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]]; it will lead do a ugly mix of formats with user's sefault locale, if it not happend to be 'en_US'

ie in German: 31/12/2012 02:00:00 nachm. This is a format not used in german.

To support user's language and locale you should instead use the different styles.


The q&a rmaddy provided indicates, that there are rare cases, where the parse format gets rewritten. To avoid this, set the input formatted's format to a certain locale, like [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];

查看更多
登录 后发表回答