NSDateFormatter locale is not working - ios

I am using the following code to get the NSString from date
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = kCFDateFormatterLongStyle;
df.timeStyle = kCFDateFormatterNoStyle;
df.doesRelativeDateFormatting = YES;
[df setLocale:[NSLocale currentLocale]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
NSString *dateString=[df stringFromDate:myDate];
But it displaying like 'Today','Yesterday' even after changing Locale.I want to display the localized language of that also ?

I used your code and created this:
NSDate *yesterday;
NSTimeInterval interval;
NSCalendar *cal = [NSCalendar currentCalendar];
[cal rangeOfUnit:NSDayCalendarUnit
startDate:&yesterday
interval:&interval
forDate:[NSDate date]];
yesterday = [yesterday dateByAddingTimeInterval:-interval];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = kCFDateFormatterLongStyle;
df.timeStyle = kCFDateFormatterNoStyle;
df.doesRelativeDateFormatting = YES;
[#[[NSLocale localeWithLocaleIdentifier:#"de_DE"], [NSLocale localeWithLocaleIdentifier:#"es_ES"],[NSLocale localeWithLocaleIdentifier:#"en_US"]] enumerateObjectsUsingBlock:^(NSLocale *locale, NSUInteger idx, BOOL *stop) {
[df setLocale:locale];
[df setTimeZone:[NSTimeZone systemTimeZone]];
NSString *dateString=[df stringFromDate:yesterday];
NSLog(#"%#", dateString);
}];
It outputs correctly
Gestern
ayer
Yesterday
Your problem must lay else-where.
Tested within a command-line program
Make sure that after you changed the language on device/simulator you kill the app completely before you open it again.

Hi I write this Function for LocalTime it Takes input of date as String and covert to Local Time String. May it help you
+(NSString*)LocalTimeWitheDateString:(NSString*)dateString{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date =[formatter dateFromString:dateString];
NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
[formatter setTimeZone:localTimeZone];
NSString *LnewTimeZoneDateString = [formatter stringFromDate:date];
return LnewTimeZoneDateString;
}

Related

NSDate current date returning nil

In some scenario, I am getting currentDateString as nil.My app is in live so i can't find out exact scenario.
NSDate *currentDate = [NSDate date];
NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSString *currentDateString = [dateFormatter stringFromDate:currentDate];
I tried this code. I got correct results. Please set DateFormatter Locale
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSString *currentDateString = [dateFormatter stringFromDate:currentDate];

How to convert my server time to other country time?

I am getting time from server is 2016/07/19--12:20:00
Now I have to change this time according to country.
For example If I am in Auckland then time has to like 2016/07/19--18:50:00
I found so many solutions in stack overflow but I can't solve this.
Please help me
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY/MM/dd--HH:mm:ss"];
NSDate* sourceDate = [dateFormatter dateFromString:model.tripEndTime];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setDateFormat:#"YYYY/MM/dd--HH:mm:ss"];
NSString* localTime = [dateFormat stringFromDate:destinationDate];
NSLog(#"localTime:%#", localTime);
I tried with this code
Just convert the server time to UTC time, then convert to local country time. Just write a separate method for this
+(NSString*)convertLocaleTimeToChatTimeStampWithDateStr:(NSString *)strDate
{
NSDateFormatter *df = [DateHelper getDefaultTimeFormatter];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
[df setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date = [df dateFromString:strDate];
[df setDateFormat:#"dd MMM, hh:mm a"];
[df setTimeZone:[NSTimeZone localTimeZone]];
return [df stringFromDate:date];
}
+(NSDateFormatter*)getDefaultTimeFormatter
{
NSDateFormatter *df = [NSDateFormatter new];
[df setDateStyle:NSDateFormatterNoStyle];
[df setTimeStyle:NSDateFormatterShortStyle];
return df;
}
hope this will solve your problem. Thanks
It is better to use server timestamp. After getting timestamp convert it according to timezone

Change date format using NSDateFormatter

I want to use date format like this 2015-08-14 but unfortunately I am getting this format 16/11/15
Below is the code I have used to get desired output.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
[dateFormatter setDateStyle:kCFDateFormatterShortStyle];
NSString *formattedDateString = [dateFormatter stringFromDate:selectedDate];
Can anyone please suggest how to fix this issue?
Just remove the line [dateFormatter setDateStyle:NSDateFormatterShortStyle];
-(NSString*)stringFromDateWithFormat:(NSString*)formatString{
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
dateFormatter.dateFormat = formatString;
return [dateFormatter stringFromDate:self];
}
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"YYYY-MM-dd"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
NSLog(#"date is %#",dateString);
NSDate *selectedDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSString *formattedDateString = [dateFormatter stringFromDate:selectedDate];
NSLog(#"formattedDateString = %#", formattedDateString); // Prints 2015-10-15

Convert date to device timezone

I'm parsing a JSON from my server, one of the values is a date like this: 2013-01-21 18:22:35 +00000
My problem is how to transform this date to the local time like 2013-01-21 12:22:35 -0600
I need to transform the date to other format?
Thanks!
EDIT.
Using the #Gabriele Petronella solution:
NSString * yourJSONString = #"2013-01-21 18:22:35 +0000";
NSTimeZone* localTimeZone = [NSTimeZone localTimeZone];
NSString* localAbbreviation = [localTimeZone abbreviation];
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:#"yyyy-MM-dd H:mm:ss Z"];
NSDate *aDate = [dateFormatter dateFromString:yourJSONString];
NSTimeZone *timezone = [NSTimeZone timeZoneWithName:localAbbreviation];
[dateFormatter setTimeZone:timezone];
NSLog(#"%#", [dateFormatter stringFromDate:aDate]);
Everything works fine!
You use a NSDateFormatter to read the date from a NSString, then you can display the date changing the timezone using another (possibly the same) NSDateFormatter.
NSString * yourJSONString = #"2013-01-21 18:22:35 +0000";
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle
];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
NSDate * aDate = [dateFormatter dateFromString:yourJSONString];
NSTimeZone * timezone = [NSTimeZone timeZoneWithName:#"America/Chicago"];
[dateFormatter setTimeZone:timezone];
NSLog(#"%#", [dateFormatter stringFromDate:aDate);
Please note that the timezone is just a matter of display, whereas the NSDate object is just an abstraction to represent the date and it is independent by its displaying.
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSTimeZone *timeZone = [NSTimeZone localTimeZone]; [dateFormatter setTimeZone:timeZone];
NSString *myCurrentDeviceDate = [dateFormatter stringFromDate:[NSDate date]];

Convert number to character in hebrew date

I build label with the date for today (hebrew date)
its work but is number and not character
for exapmle:28/4 to 28 Tavat
code:
NSLocale *hebrew = [[NSLocale alloc] initWithLocaleIdentifier:#"he_IL"]; // Hebrew, Israel
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSHebrewCalendar];
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = hebrew;
dateFormat.calendar = calendar;
[dateFormat setDateStyle:NSDateFormatterShortStyle];
NSString *dateString = [dateFormat stringFromDate:today];
[_label setText:dateString];
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
NSLog(#"hebrew: %#", [formatter stringFromDate:[NSDate date]]);
[formatter release];
You need to change the style to medium or higher:
[dateFormat setDateStyle:NSDateFormatterMediumStyle];
If you use the medium style then the month will be abbreviated, switch to the long style (NSDateFormatterLongStyle) for the full month name.

Resources