How to convert a date of any language to a specific language - ios

How to convert a date of any language(Arabic) to a specific language(English).
While changing the region format to arabic, the dates are getting changed. When I am picking the date value from some controls(UIButtons or UILabels) for saving in database, its picking the date in arabic language and saving in the same format. Here I want to convert the date from arabic to english before saving it in database.
I tried this but its returning nil
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];
Can anyone please help me ?

I solved in this way
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];

You shouldn't store dates in any language. You should store NSDate, which has no language or locale or time zone, and display it as appropriate. If your database cannot store NSDate, you can store [myDate timeIntervalSince1970] and convert it to an NSDate using the class method dateWithTimeIntervalSince1970:

You should use UIDatePicker as the input instead of UIButtons or UILabels. Save the timeIntervalSince1970 into your database, so that it can be presented by any language.
NSDate *currentDate = //My date coming like “٢٠١٤-٠٥-١٥ ١٢:١٠:٢٣ ”
NSTimeInterval timeToBeSavedInDatabase = [currentDate timeIntervalSince1970];
//in the future, you can show this date in any language
NSString *dateString = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970]];

You may use this to get the date with preferred local
NSDate * dateNow = [NSDate new];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormat setLocale: usLocale];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
NSString * dateString = [dateFormat stringFromDate:dateNow];

Related

Converting a NSString to NSDate

I am trying to convert a NSString into a NSDate as shown below.
The value of NSString *startTime is 2015-06-23T01:37:53Z,
but the value of NSDate *startTimeDate is nil. What is wrong with the code ?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"PST"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *startTimeDate = [dateFormatter dateFromString:startTime];
check your date format
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
change into
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
Swift
check your date format
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
change into
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
There are at least 2 issues with your date format:
.SSS is used to read milliseconds but variable startTime doesn't contains milliseconds value;
Z represent GMT time zone and must not be escaped in dateFormat string.
Let's try to fix this ussues:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *startTimeDate = [dateFormatter dateFromString:startTime];
I recommend to use this NSDateFormatter date formatting table. It's very comprehensive and helpful.
The startTime you've specified doesn't have any milliseconds, so you want to use a dateFormat of:
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
If you want to support both styles of XML date strings then I recommend creating two NSDateFormatter instances for both date formats, and try the other if you get nil from the first.
Its simple one, converting NSString to NSDate we use NSDateformatter using dateFromString method. We need to provide the NSDateFormatter style with existing style for NSString
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:#"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:#"HH:mm:ss zzz"];
[dateFormatter setDateFormat:#"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];

NSString to NSDate conversion using NSDateFormatter > date way off

Trying to convert a datetime from NSString to NSDate using NSDateFormatter. I know it should be a simple task, but it's not - the converted NSDate is whole 20 days off.
I cannot find the cause for this. Already tried every solution I could find (different locales, time zones, formattings ...) but no luck so far.
Here's a code sample:
NSString *strDate = #"24.01.2014 08:17:10";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *locale = [NSLocale currentLocale];
[dateFormat setDateFormat:#"dd.MM.YYYY HH:mm:ss"];
[dateFormat setLocale:locale];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"CET"]];
NSDate *dte = [dateFormat dateFromString:strDate];
output of dte is "04.01.2014 08:17:10". As you can see, the time is correct, but the date is way off.
There is an error in your time format, YYYY should be yyyy.
NSString *strDate = #"24.01.2014 08:17:10";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *locale = [NSLocale currentLocale];
[dateFormat setDateFormat:#"dd.MM.yyyy HH:mm:ss"];
[dateFormat setLocale:locale];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"CET"]];
NSDate *dte = [dateFormat dateFromString:strDate];
Will give me as output : 2014-01-24 07:17:10 +0000
Use the following code
NSString *strDate = #"24.01.2014 08:17:10";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd.MM.yyyy HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:strDate];

Getting null when trying to extract year value from a date [duplicate]

This question already has answers here:
NSDate from NSString gives null result
(3 answers)
Closed 9 years ago.
My code is the following:
NSString *dateString = #"1339007317";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSString *year = [dateFormatter stringFromDate:date];
NSLog(#"%#",year);//null
How to fix that? thanx.
It is because you are using unix timestamp and you can convert unix timestamp into year or any date format as :
//Posted Date Format
NSString *dateStr = #"1339007317";
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[dateStr doubleValue]];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en-US"];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy"]; //Here you can set any date format Ex:#"dd MMM, yyyy hh:mm a" or#"dd/MM/yyyyy" according to your requirement
[dateFormatter1 setLocale:locale];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
dateStr = [dateFormatter1 stringFromDate: date];
Hope it helps you.
You need two date formats. The first should match the format of the string you wish to convert to an NSDate. The second format needs to represent the format you want. Use the 2nd to convert the NSDate to the new string.
Also, why do you create a date object then immediately replace it with a new one?
Change this:
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:dateString];
to:
NSDate *date = [dateFormatter dateFromString:dateString];
Your code needs to be like this:
NSString *dateString = #"1339007317";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"???"]; // format that matches the dateString
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"yyyy"]; // the desired new format
NSString *year = [dateFormatter stringFromDate:date];
NSLog(#"%#",year);

NSDate comparison in custom format Issue

I want compare two dates. I am getting device date in yyyy-dd-mm format and getting mm-dd-yyyy from webservices. I want to change device date format and get that changed date in NSDate object. Here is my code:
NSDate *today = [[NSDate alloc] init];
NSLog(#"today : %#", today);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM dd yy"];
NSString *tString = [dateFormatter stringFromDate:today];
NSLog(#"tString : %#",tString);
NSDate *date = [dateFormatter dateFromString:tString];
NSLog(#"date : %#",date);
You are saying date coming from webservice is in mm-dd-yyyy format..Then convert it into NSDate like this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy"];
NSDate *convertedDate = [dateFormatter dateFromString:yourDateStringFromWeb];
You have device date already with you..Now you can compare two NSDate objects in many ways..See this and this..

Convert string to date in my iPhone app

I have made a calendar application for the iPhone in which I have a date in string format (e.g. "Tue, 25 May 2010 12:53:58 +0000").
I want to convert this to an NSDate.
What do I need to use to do that?
Take a look at the class reference for NSDateFormatter. You use it like this:
NSString *dateStr = #"Tue, 25 May 2010 12:53:58 +0000";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EE, d LLLL yyyy HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat release];
For more information on how to customize that NSDateFormatter, try this reference guide.
EDIT:
Just so you know, this is going to parse the full month name. If you want three letter month names, use LLL instead of LLLL.
-(NSString *)dateToFormatedDate:(NSString *)dateStr {
NSString *finalDate = #"2014-10-15";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:dateStr];
[dateFormatter setDateFormat:#"EE, d MMM, YYYY"];
return [dateFormatter stringFromDate:date];
}
If you are storing dates in one of iOS' styles, it's far easier and less error prone to use this method:
// Define a date formatter for storage, full style for more flexibility
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle];
// Use today as an example
NSDate *today = [NSDate date];
// Format to a string using predefined styles
NSString *dateString = [dateFormatter stringFromDate:today];
// Format back to a date using the same styles
NSDate *todayFromString = [dateFormatter dateFromString:dateString];
NSString *dateString=#"2017-05-25";
NSDateFormatter *dateFormatter=[NSDateFormatter alloc]init];
[dateFormatter setDateFormatter:#"MM-dd-yyyy"];
NSDate *date =[[NSDate alloc]init];
date=[datFormatter dateFromString:dateString];

Resources