如何在日期时间格式化程序区域设置JP中删除汉字字符?

时间:2018-12-13 10:18:27

标签: ios objective-c nsdateformatter

NSDateFormatter *stringFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [NSLocale currentLocale];

NSLocale *ja;

if ([[locale localeIdentifier] rangeOfString:@"ja_JP"].location == NSNotFound) {
    ja = [[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"];
    [stringFormatter setLocale:ja];
}else{
    ja = [NSLocale currentLocale];
}

[stringFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Tokyo"]];
[stringFormatter setDateFormat:@"yyyyMMddHHmmss"];

有时它返回如下结果:20190211午后。我想保留日本的语言环境,但结果删除了午后。结果应该是:20190211152022。

1 个答案:

答案 0 :(得分:0)

尝试一下

NSDateFormatter *stringFormatter = [[NSDateFormatter alloc] init];
    NSLocale *locale = [NSLocale currentLocale];

    NSLocale *ja;

    if ([[locale localeIdentifier] rangeOfString:@"ja_JP"].location == NSNotFound) {
        ja = [[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"];
        [stringFormatter setLocale:ja];
    }else{
        ja = [NSLocale currentLocale];
    }

    [stringFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Tokyo"]];
    [stringFormatter setDateFormat:@"yyyyMMddHHmmss"];

    NSString * strdate = [stringFormatter stringFromDate:[NSDate date]];

    NSLog(@"%@", strdate);

它会变成这样

20181213195829

希望有帮助