I have a date in UILabel(like 'Dec 14, 2010). This date i want to set as minimum date of my UIDatepicker. How can i do this please reply?
Thanks
Nishant
Convert your UILabel text in to date format using NSDateFormatter and put that value replacing "Date" in following example:
UIDatepicker *datePicker;
NSDate *Date=[NSDate date];
datePicker.minimumDate=Date;
You can use NSDateFormatter and use setDateFormat method for setting Date format.
[formatter setDateFormat:#"MMM dd,yyyy"];
NSDate *date = [formatter dateFromString:lblDate.text];
datePicker.minimumDate=date;
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
[comps setYear:0];
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-5];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[datePicker setMaximumDate:maxDate];
[datePicker setMinimumDate:minDate];
it showing five year date.......
See the minimum time from the current date to one month:
NSDate* currentTime = [NSDate date];
[datePicker setMinimumDate:[currentTime dateByAddingTimeInterval:43200]];//min time +12:00 for the current date
[datePicker setMaximumDate:[currentTime dateByAddingTimeInterval:2592000]]; // max day (+ 30 )
Related
Strange behavior in iOS 6.1 I have set the minimum date to current date for my date picker like this
NSDate *currentTime = [NSDate date];
[picker setMinimumDate:currentTime];
But when I run the app I am able to scroll to past date, though its not selected, picker doesn't jump back to current date. It's happening only with iOS 6.1 version and in rest picker is behaving normally.
I got the same issue as you and fixed it with only setting the date to the maximum date manually (in this case I set the limit to the current date):
- (IBAction)pickerValueChanged:(id)sender {
dispatch_async(dispatch_get_main_queue(), ^{
UIDatePicker *datePicker = (UIDatePicker *)sender;
if ([self.datePicker.date compare:[NSDate date]] == NSOrderedDescending) {
datePicker.date = [NSDate date];
}
});
}
This function is triggered when the date value from the date picker did change. you can set a maximum or minimum value here.
Try This code
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
[comps setYear:30];
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-30];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[datePicker setMaximumDate:maxDate];
[datePicker setMinimumDate:minDate];
You have to set min and max date like:
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[datePicker setMinimumDate:minDate];
The property is an NSDate object or nil (the default), which means no maximum date. This property, along with the minimumDate property, lets you specify a valid date range. If the minimum date value is greater than the maximum date value, both properties are ignored. The minimum and maximum dates are also ignored in the countdown-timer mode (UIDatePickerModeCountDownTimer).
Try setting valid minimum and maximum date both.
It works for me.
the date Picker I have right now has the style like : mm/dd/YYYY and it displays at Textfield in "YYYY/MM/DD".
how can I change my code so that the datepicker shown is also changed to " YYYY/MM/DD".
Here is my code:
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] ;
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init] ;
[comps setYear:-10];
NSDate *maximumDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-10];
NSDate *minimumDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker setMaximumDate:maximumDate];
[datePicker setMinimumDate:minimumDate];
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
[self.Birthday setInputView:datePicker] ;
You can set the locale property of the DatePicker. The documentation says:
The default value is the current locale as returned by the currentLocale property of NSLocale, or the locale used by the date picker’s calendar. Locales encapsulate information about facets of a language or culture, such as the way dates are formatted.
It is generally considered poor form to force a user to select in a fashion not typical to their locale.
I think, you can't modify the UIDatePicker default style.
You can change to many formats after the date is picked by using NSDateFormatter
Example:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mma"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(#"date: %#", dateString);
Is there a way to subtract the current date by 5. say if today is 2008-12-9 i need to get the date 5 days back. If we output this, the date should display as 2008-12-4.
How can i code this programatically? or a tutorial that would help
Always use NSCalendar and NSDateComponents for date calculations. This will take into account oddities like leap years with 29 days in February, leap seconds and daylight saving changes.
NSDate *date = [NSDate date]; // Using current date here
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = - 5; // Find date for 5 days ago
NSDate *newDate = [calendar dateByAddingComponents:components toDate:date options:0];
Use NSDateComponents
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *offsetComponents = [[[NSDateComponents alloc] init] autorelease];
[offsetComponents setDays:-5];
NSDate *fiveDaysAgo = [gregorian dateByAddingComponents:offsetComponents toDate:today options:0];
to convert it to a string with the preferred format, use NSDateFormatter
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString *formattedDateString = [dateFormatter stringFromDate:fiveDaysAgo];
This is the brute-force way:
Sustract 5 from DAY.
If DAY < 0, add number of days of the previous month and sustract 1 from MONTH.
If MONTH < 0, add number of month of a year and sustract 1 from YEAR.
The advantage of the brute-force approach is that it will work with every language.
#define SOME_HOUR -24*5
NSDate *today = [NSDate date];
NSDate *someDay = [NSDate dateWithTimeInterval:60*60*SOME_HOUR sinceDate:today];
From today date to 5 years how to add the values in UIPickerView
Why should you use UIPickerView, Why not UIDatePicker?
You can set the MinimumDate and MaximumDate in UIDatePicker. You should code as,
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:5];
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps release];
[calendar release];
[datePicker setMaximumDate:maxDate];
[datePicker setMinimumDate:currentDate];
Use UIDatePickerView. And set value if this date picker as Start and End date..
UIDatePicker has a property of Start and End Date Values..
maximumDate
minimumDate
These two property helps you..
I think this is just a minor problem. After searching for some time I still can't figure it out. Hopefully you can. :-)
I want to get a full timestamp of today, passing own hours and minutes.
My approach was the following(dateStr is #"11:30" for example):
NSDateFormatter *myFormatter = [[[NSDateFormatter alloc] init] autorelease];
[myFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[myFormatter setDateFormat:#"HH:mm"];
NSDate *tmpDate = [myFormatter dateFromString:dateStr];
[myFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
...???
So now I get
1970-01-01 11:30:00 +0000
What do I habe to do to apply the dateStyle to my NSDate and get the correct date of today?
I know that I can use [NSDate date] to get the actual date but what can I do to "manipulate" only minutes and hours?
Would be great to get some help from you guys :)
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
[components setHour:hour];
[components setMinute:minute];
NSDate *date = [gregorian dateFromComponents:components];
[gregorian release];