How to check the date is modified in UIDatePicker or not? - iphone

I am using UIDatePicker to select a date. When loading the view date picker minimum date was set with current date, For my specification I need to know is there any modification done in UIDatePicker or not.
if ([[date_picker minimumDate] compare:[date_picker date]] != NSOrderedSame) {
printf("value modified");
}
I have tried with the above condition but its always not matching even though I have not modified the date. Am I doing anything wrong? How do I find that out?

Set control events for datepicker like this
[date_picker addTarget:self
action:#selector(pickerChanged:)
forControlEvents:UIControlEventValueChanged];
and in the pickerChanged method get its value
- (void)pickerChanged:(id)sender{
selectedDate=[sender date];
}
date comparison with
if ([initialDate compare:selectedDate]!=NSOrderedSame)
{
NSLog(#"modified");
// two dates are same and the date formatting of the two dates must be same
}
so whenever you change the date this method will be triggered initialDate is when the view loaded the value is set

Related

Kendo date picker custom date format

I have an application which allows the customer to set the date format by setting screen.
I change the current thread culture date format, and set the date and parsing format in the kendo date picker.
DateTimeFormatInfo datetimeFormat = new DateTimeFormatInfo();
datetimeFormat.FullDateTimePattern = "MM.dd.yyyy hh:mm tt"; //(set static for testing)
Thread.CurrentThread.CurrentCulture.DateTimeFormat = datetimeFormat;
My problem is when I change the date format a client side validation error returns says that the field should be a date.
I tried to set custom rule in the kendo validator but that not helps me because when I change the date this rule not fired.
$("myForm").kendoValidator(
{
rules: {
dateValidation: function(input)
{
if (input.is("[data-role=datepicker]")) {
// My code should be here
}
return true;
}
}});
Anyone can help me to solve this issue.
There is no need to add custom rule you need to set the custom format in the kendo validator as following:
kendo.ui.validator.rules.mvcdate = function (input) {
return input.val() === "" || kendo.parseDate(input.val(), "dd/MM/yyyy") !== null;
}
This code will return true (valid) if the date picker has a value and the value is in the correct format (dd/MM/yyyy in the above)

setMinDate(...) for DatePicker doesn't work when invoked a second time

I'm in a particular situation in which I have to alter the min and max date of DatePicker according to the selected element of a Spinner. Here's the chunk of code I'm using to switch min and max date.
private void switchCalculationMethod(int method) {
calculationMethod = method;
switch (method) {
case METHOD_1:
datePicker.setMinDate(new LocalDate().minusWeeks(42).getMillis());
datePicker.setMaxDate(new LocalDate().plusDays(1).getMillis() - 1);
break;
case METHOD_2:
datePicker.setMinDate(new LocalDate().minusWeeks(2).getMillis()); // This don't work!!
datePicker.setMaxDate(new LocalDate().plusWeeks(40).getMillis()); // This works!!!
break;
}
datePicker.init(today.getYear(), today.getMonthOfYear() - 1,
today.getDayOfMonth(), this);
}
So, the DatePicker would get set up correctly the first time, problem occurs when I attempt to change the min date again (changing max date works). It would remain at the value I had set first. I'm thinking this is a bug. Am I doing something wrong here? Is there a workaround for this?.
PS : I'm using Joda time api.
This happens because method setMinDate() has check
if (mTempDate.get(Calendar.YEAR) == mMinDate.get(Calendar.YEAR)
&& mTempDate.get(Calendar.DAY_OF_YEAR) != mMinDate.get(Calendar.DAY_OF_YEAR){
return;
}
Simple workaround is to set min date with different year at first, for example
mPicker.setMinDate(0);
mPicker.setMinDate(new LocalDate().minusWeeks(2)
.toDateTimeAtStartOfDay().getMillis());
It works for me.
As said above, you can bypass the check by calling those before actually changing the value:
setMinDate(0);
setMaxDate(Long.MAX_VALUE);
If you want to reset the minimum or maximum value to its default, you can use the following values:
setMinDate(-2208902400000L); // Jan 1, 1900
setMaxDate(4102531200000L); // Jan 1, 2100
mPicker.setMinDate(0);
doesn't work for me.
Try to reinitialize the picker.
mPicker = new DatePickerDialog(..)
first update setMinDate to 0 after then setMinDate according to you dateobject
mPicker.setMinDate(0);
mPicker.setMinDate(datepickerObject.getTimeInMillis());

What if the user edits device date to bypass logic check that compares current date with a constant date or so?

I am making an app that makes a comparison between two dates - one being the current device date and the other being hardcoded date. But one way to fail this logic is, lets say, the hardcoded date is passed and the user changes the device date to a date, which is before the hardcoded date. So in this case the app fails.
How do I avoid this ?
NSDate * currentDate = [NSDate date];
NSDate * hardcodedDate = [[NSDate alloc] initWithString:#"2013-07-21 11:15:30 +0700"];
NSComparisonResult result = [currentDate compare:hardcodedDate];
if (!result == NSOrderedDescending)
{
// do something
}
You would need to get the current date from a web service (using https) in order to verify the date of the users device.

Indexing and Sorting a UITableView with Values Parsed from an XML by Date

I am parsing an XML file from a URL, from this XML I parse titles of events, their dates, and locations of the events among other things. These elements are cached in an array called stories which acts as my data source for my table view. My goal is to display and indexed UITableView with the sections based on the date at which my events take place by month (January's events in a section, February's events in another, with month names as the titles for my sections). The events that will take place soonest will be at the top of my view, events further out should be at the bottom of my list, I don't have to worry about events that have already taken place. So far I've been able to display my event names and their dates as subtitles in my UITableView not sorted or indexed, left in the order by which they were parsed. When I log the stories array I view this in the console:
2013-01-04 00:17:22.332 NumberTwo[72213:c07] the stories array is as follows: (
{
additionalDesc = "";
allday = "Yes.";
endtime = "11:55 PM";
isodate = "2013-05-23";
location = "";
starttime = "12:00 AM";
title = "CST Exam Makeups";
},
{
additionalDesc = "";
allday = "No.";
endtime = "12:00 PM";
isodate = "2013-06-01";
location = "";
starttime = "8:00 AM";
title = "SAT & Subject Tests";
},)
There is an example of two elements in the array, I understand I am going to have break down the stories array into smaller ones based on the month that the events occur but I don't know what that method would look like. Here's my attempt at it in the viewDidLoad.
- (void)viewDidLoad {
[super viewDidLoad];
self.sections = [NSMutableDictionary dictionary];
for (UIEvent *event in stories)
{
NSDate *dateRepresentingThisDay = tempDate;
// If we don't yet have an array to hold the events for this day, create one
NSMutableArray *eventsOnThisDay = [self.sections objectForKey:dateRepresentingThisDay];
if (eventsOnThisDay == nil) {
eventsOnThisDay = [NSMutableArray array];
}
// Add the event to the list for this day
[eventsOnThisDay addObject:event];
}
// Create a sorted list of days
NSArray *unsortedDays = [self.sections allKeys];
self.sortedDays = [unsortedDays sortedArrayUsingSelector:#selector(compare:)];
}
I know the line for (UIEvent *event in stories) must be incorrect but I'm sure what should replace it. tempDate is a formatted date I receive from an NSDateFormatter in another method, and event is meant to refer to an element in my array stories. If you could, point me in the right direction to accomplish my goal, it would be much appreciated. Thank You.
For this you have to use the following steps:
1. On the number of section method of table view write the array count.
2. you also have to modify your array by comparing the dates which are available on stack overflow and arrange in array according to your requirement.
3. Set title by giving title.text = [your array object at index[index path of section]]
4. Now you can give rows as you like in it.
This are just steps you need to follow..
Have you got what I mean to say?
Is it possible for you create a class for the above data and have NSDate property to represent your start date/time of the event? If so, you can make use of NSPredicate to sort the array based on your start date property. You can find details on NSPredicate on this link.

UILocalNotification with non default timezone

I have been trying to use UILocalNotification and setting timezone to some other timezone than my default.
The purpose is to do something like:
-User will schedule an alert with time and timezone
-Show alert when that timezone becomes specified time.
In more verbal description, let say I live in LosAngeles and my gf is in Chicago. I want to set up an alert when chicago is 7am, for weekdays.
For one alert, I can do without timezone, to do absolute time. But I want to allow users to set repeat flags, in which case I can't seem to do - I tried setting localnotification's timezone property to chicago as above example, however the local notification will not fire.
I logged the uinocalnotification during serialization process, and here's one output:
"<UIConcreteLocalNotification: 0xfb25a50>{fire date = 2011-02-06 06:02:00 -0800, time zone = America/Chicago (CST) offset -21600, repeat interval = 16, next fire date = 2011-02-06 08:02:00 -0800}"
I set firedate for 9:02am chicago absolute time, and timezone to cst - and the log does mention that the fire date is indeed 06:02 my local time. However, nothing happens. Also, next fire date is weird since it should be +1 day, not +2 hrs.
Here's how I set this up:
Class classUILocalNotification = NSClassFromString(#"UILocalNotification");
if (classUILocalNotification != nil) {
id note = [[classUILocalNotification alloc] init];
NSString *body = #"body message";
switch (repeatflag) {
case 1: [note setRepeatInterval:NSDayCalendarUnit]; break;
case 2: [note setRepeatInterval:NSWeekdayCalendarUnit]; break;
case 3: [note setRepeatInterval:NSWeekCalendarUnit]; break;
case 4: [note setRepeatInterval:NSMonthCalendarUnit]; break;
case 5: [note setRepeatInterval:NSYearCalendarUnit]; break;
default: break;
}
[note setFireDate:dt];
[note setAlertBody:body];
[note setTimeZone:timezone];
[note setHasAction:NO];
}
This seems like some unknown behavior. Anyone have a better way to achieve what I am trying to do?
Thanks!
There appears to be a bug with UILocalNotification when using non-local timezone. The first event fired is fired for the correct date and time and timezone. However, subsequent events fire for the given date and time in your local timezone instead of the one set in the setTimeZone method. [iOS 6.1]