NSDate to String conversion and display in the UILabel - ios

I am trying to get a string from NSDate object type and display it in a label.
Here is the code I am using,
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init] ;
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
//[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSDate *datey = [dateFormatter dateFromString:selectedDate];
NSString *labelData = [dateFormatter stringFromDate:datey];
dateLabel.Text = labelData;
where selectedDate is a String containing date in yyyy-mm-dd format.
The label is not showing up the date. But if I try giving it a string like #"someblah", it is displaying. Where am I going wrong??
Help appreciated. Thanks

Your line dateLabel.Text should be dateLabel.text.
So you start with a date string, convert it to a date, and then convert it back to a string using the same formatter. Isn't it easier to use:
dateLabel.text = selectedDate;
In all likelihood you are trying to create an NSDate using a date string that is inconsistent with the format. For example, if the format is #"yyyy-MM-dd" but your selectedDate differs from that format then the formatter won't return a NSDate. You can avoid this by setting the DateFormat to be correct for the selectedDate and then, once parsed, change the DateFormat for the desired output. Or use two NSDateFormatter instances.

Try specifying a specific format instead of NSDateFormatLongStyle, for example
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
I found this answer in 2 seconds using google.. (Yes thats a hint)

Related

Change iOS Datepicker Display Format

How can I change the iOS Datepicker date selected display format? The format displays as 2015-12-30. I want the date to display as 12/30/2015.
Ok, as I told you in comment above. Here is how you can get the date in string from UIDatePicker
in Objective-C
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"];
NSString *stringFromDate = [formatter stringFromDate:self.yourDatePicker.date];
in Swift
let dateFormater: NSDateFormatter = NSDateFormatter()
dateFormater.dateFormat = "MM/dd/yyyy"
let stringFromDate: String = dateFormater.stringFromDate(self.yourDatePicker.date) as String
I have set up my datePicker in Storyboard. In Swift 5, in order to get dates in correct format, you should do the following:
Specify datePicker's locale in Storyboard (that would translate months' names to your language).
Create dateFormatter like #Cong Tran wrote. Though now you could use DateFormatter instead of NSDateFormatter.
Convert string back to date using date(from:) method using optional binding.
Assign formatted date to datePicker's date.

Convert a date to English format form Bengali

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;
}

Correct Format for String to Date?

I m sorry stackoverflow is full of dateformatter questions but i m having a really hard time to get a string into a NSDate.
This is the String i receive:
2014-12-22T06:49:40+0000
And this is how i m trying to format it and get it to a NSDate:
NSString *time = #"2014-12-22T06:49:40+0000";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd'T'hh:mm:ss'Z'"];
[df setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *myDate = [df dateFromString:time];
Unfortunately myDate is always nil.
Am i missing somtheing or is my formatting simply wrong?
Thanks in advance
Try this:"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSSSSSSZ"
You've wrapped your final Z in single quotes, which means you want it to be a literal Z character, but no such character exists in your time string. Removing the single quotes means it would be looking for a timezone offset, like you want.

how to convert Javascript Date to iOS date format

I am new in iOS development. Actually I am trying to show some information which I get from a web service in a table view.
I have successfully retrieved the response as JSON in my code, but in my table view there is a label to show date.
But in my response the date is something like this
/Date(1391068800000)/
How can I convert this to show in my table view? I think this is Javascript date.
But I'm not sure how to convert it.
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[your timestamp doublevalue]/1000];
divide value by 1000 cause of milliseconds (13 digit)
The date in your response is a timestamp. You can create a date object by:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:1391068800000];
After that you can convert this date to a string in an appropriate format by using the NSDateFormatter class. For example:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
NSString *dateString = [formatter stringFromDate:date];
For more information about formatting dates see: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html

How to convert NSString to NSDate with specific format

I'm sorry if this has been asked before but I did a lot of checking and I can't seem to find an answer. :(
I have a string "2013-05-7 05:53:15 +0000" and I want to convert it to a NSDate so I can compare it with the current date and time. I can't figure out how to do this. :( I successfully converted it but it changes the look of the string and I want it to be exactly 2013-05-7 05:53:15 +0000 as that is the same format I have the other NSDate item.
Any and all help is greatly appreciated.
I am confused. ;) new to objective C. old VB / older C programmer from the 80s trying to figure this stuff out. Thank you for your help. :)
My code looks like this:
NSDateFormatter *df1 = [[NSDateFormatter alloc] init];
// [df1 setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZ"];
[df1 setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
df1.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"EN"];
NSDate *titemdate1 = [df1 dateFromString: itemdate];
what i have is two dates. one that is the last date the app was run and the other is the date of the item i'm pulling down using JSON. I want to compare the two dates (which are in string format as stated above). when I do a NSLog of the strings they look perfect but when i convert to NSDate to compare i have problems. any ideas?
Try with using below code..
-(NSDate*)dateFromJsonString:(NSString*)string
{
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *xExpDate = [inputFormatter dateFromString:string];
return xExpDate;
}
First of all you should know that if you convert the dateString to date and do NSLog of date, it returns the GMT time, which will be different from your system time in most cases. So if you are getting the string in correct format, then the date formatter is probably working fine. To check this, just convert your formatted date to string and print the output.
NSLog(#"formatted date: %#",[df1 stringFromDate:titemdate1]);
If that doesnt change anything and the output is still wrong, try changing the format of your formatter.
[df1 setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];

Resources