简体   繁体   中英

NSDateFormatter printing current date in format “M/yyyy hh:mm” in any locale

Hi I want to print current date in any locale as M/yyyy hh:mm .

I was using this method with currentLocale as the option in locale.

+ (NSString *)dateFormatFromTemplate:(NSString *)template options:(NSUInteger)opts locale:(NSLocale *)locale

But it puts locale specific arrangements.

For example in US locale there is a comma in between date and time.

what to do ???? I want no comma just space in every locale...

If you want to format a date in a specific format, then why are you using dateFormatFromTemplate: ? The purpose of that method is to give you a locale specific format from a general format.

Just use normal NSDateFormatter functionality:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"M/yyyy hh:mm"];
NSDate *now = [NSDate date];
NSString *formattedDate = [formatter stringFromDate:now];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM