Pra*_*Das 1 nsdateformatter ios nslocale
如何将任何语言(阿拉伯语)的日期转换为特定语言(英语).
在将区域格式更改为阿拉伯语时,日期会发生变化.当我从某些控件(UIButtons或UILabels)中选择日期值以保存数据库时,它以阿拉伯语选择日期并以相同的格式保存.在这里,我想将日期从阿拉伯语转换为英语,然后将其保存在数据库中.
我尝试了这个,但它回来了
NSDate *currentDate = //My date coming like “????-??-?? ??:??:?? ”
NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[formatter setLocale: usLocale];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:s a"];
NSString *formattedDate = [formatter stringFromDate: currentDate];
Run Code Online (Sandbox Code Playgroud)
谁能帮帮我吗 ?
我这样解决了
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:s"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLocale];
NSString *dateString = [dateFormatter stringFromDate:myDate];
Run Code Online (Sandbox Code Playgroud)