Convert a date to English format form Bengali - ios

I have a date picker in my app. The phone is set to Bangladesh local settings. When I select a date from datepicker is always returns the date in Bengali. It return a date in local format.
Like, it returns ০৬/১১/২০১৪
but I want it to be 06/11/2014.
I've tried converting it by date formatter. This is what I tried:
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
NSDate* date = [formatter dateFromString: self.birthDate.text];
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US"]];
[formater setDateFormat:#"dd/MM/yyyy"];
NSLog(#"%#",[formater stringFromDate:date]);
The output is null.

You are incorrect in your assumption when you say...
When I select a date from datepicker is always returns the date in Bengali. It return a date in local format.
UIDatePicker returns an NSDate object. NSDate has no formatting at all. It has no language, it is purely a point in time.
When you do this...
NSLog(#"%#", someDate);
The system will render that point in time into a string and then print it. It is the rendering into a string that contains the format.
I'm guessing what you are doing is this...
Get a date from a UIDatePicker.
Render that date into a UITextField in Bengali. (or label, or text view or something)
Trying to read the text and store it into a date.
Trying to then "convert" the date to an English string.
What you should be doing is just saving the date that comes from the date picker.
Put it into a property or something.
In your above code I think the bit that is failing is actually the first bit...
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
NSDate* date = [formatter dateFromString: self.birthDate.text];
Because you're not giving it a format it will fail. But this is the wrong way to go about it anyway.
You should have something like this...
- (void)datePickerChoseADate
{
self.date = self.datePicker.date;
}

Related

NSDateFormatter dateFromString incorrect

I am setting the value of a UITableViewCell's detailTextLabel to the current date and am then allowing the user to modify that date using a UIDatePicker. I set the text label like this
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a MM/dd/YYYY"];
cell.detailTextLabel.text = [dateFormatter stringFromDate:currentDate];
This sets the text label properly, and it displays something like 08:20 AM 08/07/2015. This is the desired output. When the user selects this cell, I want the date property of my date picker to be set to the date displayed in the cell. To implement this, I have the following in my tableView:didSelectRowAtIndexPath: method
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a MM/dd/YYYY"];
NSString *dateString = [tableView cellForRowAtIndexPath:indexPath].detailTextLabel.text;
self.datePicker.date = [dateFormatter dateFromString:dateString];
However, rather than setting the date to the one that is in the cell, this sets the date of the picker to 08:20 AM 12/21/2014, which is not what it should be. Logging dateString outputs the correct string that is in the table view cell.
Is there a reason that I am experiencing this issue?
Instead of #"hh:mm a MM/dd/YYYY" formatter try to use #"hh:mm a MM/dd/yyyy".
According to the Apple's docs:
"A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year."
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1
The year portion of your formatter string should be yyyy, not YYYY.

IOS - Date formatting from string

I would like to format a date to display in a uitableview custom cell
The data is passed into the app from a CMS - it is provided as a string in the following format and stored in a date type variable-
2014-04-15 10:10:45 +0000
Our app will initially be UK based - so I need to convert the format into DD/MM/YYYY format.
I tried the following code to parse my date (dateadded which is of type date).
NSDateFormatter *formatter = [NSDateFormatter new];
cellRecP.artDate.text = [formatter stringFromDate:resItem.dateadded];
but this just returns null - i guess the date format provided above is not anything that stringfromdate understands - is there any other way to format date?
Use an 'NSDateFormatter'
NSDateFormatter* newFormatter = [[[NSDateFormatter alloc] init] autorelease];
with this format to parse the string
[newFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss zzz"];
NSDate* aDate = [newFormatter dateFromString:dateString];
and you should get a valid 'NSDate' object
Try This and also Checkout this All Formate It is really good and helpful.
NSString *yourString = #"2014-04-15 10:10:45 +0000";
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setTimeZone:[NSTimeZone systemTimeZone]];//Set Your Timezone
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];//You have to set this formate.
NSDate *dd = [df dateFromString:yourString];//This will convert into date;
//Now you can set your formate.
[df setDateFormat:#"dd/MM/yyyy"];
NSString *str = [df stringFromDate:dd];
You need an NSDateFormatter and a proper unicode parse string. NSDateFormatter by default automatically checks the device locale setting the correct output.
This is an example from some code of mine:
NSDateFormatter *dateWriter = [[NSDateFormatter alloc] init];
dateWriter.dateFormat = #"yyyy'-'MM'-'dd' 'HH':'mm':'ss' 'ZZZ";
dateWriter.dateStyle = NSDateFormatterMediumStyle;
dateWriter.timeStyle = NSDateFormatterMediumStyle;
Pay attention that date formatters are pretty expensive to create.

SQLite storing, retrieving and comparing a DATETIME field

I am really stuck trying to compare dates in SQLite queries in Objective C. Here's what I'm doing:
Storing the date:
This document tells me to use the dateformat specified below, but it doesn't seem right. I tried using yyyy-MM-dd hh:mm:ss without success too though.
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-DD HH:MM:SS"];
NSString *dateString=[dateFormat stringFromDate:today];
NSString *query = [NSString stringWithFormat: #"INSERT INTO user (edited) VALUES (\"%#\")", dateString];
Comparing the date
I am using a timestamp for this, which I convert to a datetime string
long stamp = [sessie integerForKey:#"stamp"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY-MM-DD HH:MM:SS"];
NSString *dateString = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:stamp]];
sqlite3_stmt *result = [db selectQuery:[NSString stringWithFormat:#"SELECT * FROM user WHERE edited > '%#'", dateString]];
The timestamp is simply generated using [[NSDate date] timeIntervalSince1970]. The problem is that the query won't give the correct results, and I don't even know if the date is stored correctly in the first place. Any help would be greatly appreciated!
A couple of observations:
For your date string, you do definitely do not want to use YYYY-MM-DD HH:MM:SS. That will not generate a valid date string. Using yyyy-MM-dd hh:mm:ss is much closer, but not quite right, either (since you'll use 12-hour hh). Use yyyy-MM-dd HH:mm:ss instead.
This date format, though, does not capture time zone, so, if you store date strings in your SQLite database, you should use UTC (GMT) as discussed in the SQLite Date And Time Functions documentation.
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
formatter.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
NSString *dateString = [formatter stringFromDate:today];
As shown above, you probably want to also specify the locale so that the format of the dates will not change depending upon the localization settings of the device.
Note that you might consider using timeIntervalSince1970 to store the date as a REAL value in your database (as opposed to a TEXT). This can be a little more efficient and automatically addresses the UTC issue.

How do I NSLog an NSDate?

With the code pasted below, I am trying to log an NSDate. What am I doing wrong here?
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY-MM-dd"];
NSDate *todaysDate;
todaysDate = [NSDate date];
NSLog(#"Todays date is %#",formatter);
All you have to do is:
NSLog(#"Todays date is %#",[formatter stringFromDate:todaysDate]);
What you are doing wrong is you haven't done anything to associate the date with the formatter. So, you would want to do something like this:
NSLog(#"Todays date is %#", [formatter stringFromDate:todaysDate];
The NSDate doesn't know anything about formatting (just date information), and the NSDateFormatter doesnt really know anything about dates, just how to format them. So you have to use methods like -stringFromDate: to actually format the date for pretty human-readable display.
If what you need is to just see the date information and don't need a particular format, you don't need a formatter to log a date:
NSLog(#"Todays date is %#", todaysDate);
Will work fine to give you the -description of the NSDate object. I wouldn't use this for anything you display to the user (do use an NSDateFormatter for that), but this is handy if you're just debugging and need to see information about an NSDate object.
Complete example:
NSDate *today = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY-MM-dd"];
NSLog(#"%#",[formatter stringFromDate:today]);
[formatter release];

NSDate formatting from NSString

I have data in NSString, I need to display it as Oct 3, 2011. I am having trouble in converting nsstring into NSDate and then again display it as NSString.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM d, yyyy "];
NSDate *dateFromString = [dateFormatter dateFromString:myString];
myString is 2011-10-3 00:00:00
First set the date formatter time zone. If the string you're receiving is GMT/UTC, set the timezone to that.
Next set the date format to match the incoming date pattern. Do dateFromString.
Then set the date format to match the desired output format. Also set the output timezone, if different. Do stringFromDate, using as input the NSDate object from the previous operation.

Resources