NSDateformatter setDateFormat according to currentLocale

前端 未结 6 2250
孤城傲影
孤城傲影 2021-02-14 12:17

I\'m going mad with, probably, a stupid problem.

I have 3 strings: year, month and day. I need to have a date in the right format based on currentLocale, so i.e. if curr

6条回答
  •  半阙折子戏
    2021-02-14 12:41

    -(NSString *) stringFromDate:(NSDate *) date{
    
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
        [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
        [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    
        [dateFormatter setLocale:[NSLocale currentLocale]];
    
        NSString *dateString = [dateFormatter stringFromDate:date];
    
        [dateFormatter release];
    
        return dateString;
    }
    
    -(NSDate *) dateFromString:(NSString *) dateInString{
    
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
        [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
        [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    
        [dateFormatter setLocale:[NSLocale currentLocale]];
    
        NSDate *dateFromString = [dateFormatter dateFromString:dateInString];
    
        [dateFormatter release];
    
        return dateFromString;
    }
    

    I hope this helps.

提交回复
热议问题