Hide dates in UIDatePicker in iOS - ios

I want to hide the past as well as future dates from my UIDatePicker. Only want to show today's date. Is it possible?
Right now all the dates are coming, but only today's date is selecting. Time is also there. So in short I want to select today's date time by showing today's date only.

Than no need to add DatePicker.
NSDateFormatter *formatter;
NSString *dateString;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy"];
dateString = [formatter stringFromDate:[NSDate date]];

Related

Unable to get Hijri date from the UIDatePicker in ios Objective C?

I am not able to get the Hijri date format in objective c.I have used a UIDatePicker on which i have added below code for showing the date as hijri.
self.datepicker.datePickerMode = UIDatePickerModeDate;
self.datepicker.calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierIslamic];
I am getting picker as below
But when i get the date from the UIdatePikcer i get date as 08/03/2017 but not as hijri date.
I am using below code to convert date into string.
-(NSString *)convertStringFromDate:(NSDate *)date
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSLog(#"date is %#",date);
NSString *dateString = [dateFormatter stringFromDate:date];
return dateString;
}
Please tell me how i can solve this issue ?
A NSDate object is a object that holds a date but does not know how to present it will be presented to a user. See it an universal way to store dates, it does not use AM/PM, 24 hours or Gregorian calendar.
When you print/log a NSDate object it will use the systems calendar to represent the date.
To format a NSDate you use NSDateFormatter which will transform the date to something a user will understand. But you will have to tell the NSDateFormatter how to format the date.
The part you forgot is to tell NSDateFormatter to use the NSCalendarIdentifierIslamic calendar.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ;
dateFormatter.calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierIslamic];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"date is %#",dateString);
This gave me date is 10/06/1438 for today's date ( 8 March 2017)

Data sorting according to Date

I have some data which contains sold date. I want to sort them according to Week, Month, Year. For eg. If I select week then results returned should be within 7 days of current date. Same goes for Month and Year.
The relevant field from the data which is fetched from a web service looks like the following:
AuctionStartTime = "05/03/2016 09:30:00 AM"
You have use NSDateFormatter to convert the string to your date format and using NSDateFormat to get required date values and perform your task. The set date format using following code...
NSDateFormatter *dateformat = [[NSDateFormatter alloc] init];
[dateformat setDateFormat:#"Your Date Format"];
Convert string to date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:dateStr];
The your date format to set your required format. For more details of date format to click now.
You shoud use a NSDateFormatter to convert the NSString date to NSDate then you can compare that to current date. After that you can easily pick the last week's, month's or year's dates.

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.

how to add day to date in ios sqlite

I am creating a cricket app. I saved the following data in sqlite:
matchno
country1
flag1
country2
flag2
date
time
venue
This data is displayed in a UITableView.
My requirement is how to display the list of the next day's matches (example today Eng vs Ban). I want display tomorrow's match (Ind vs Ireland) in my tableview.
I tried to compare today's date and tomorrow's date, and how to write query.
My query is
select * from where date = 'Mon Mar 9'
It displays the list of today's matches, but I want to display the list of tomorrows matches.
Plz help me.
//get tomorrow date
NSDate *now = [NSDate date];
int daysToAdd = 1;
NSDate *nextDate = [now dateByAddingTimeInterval:60*60*24*daysToAdd];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
//Optionally for time zone conversions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"..."]];
NSString *tomorrowDate = [formatter stringFromDate:nextDate];
after this fire query
NSString *query=[[NSString alloc]initWithFormat:#"select * from ICCWorldCupMatchesList WHERE date = %#",tomorrowDate];
may help you.

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

Resources