I have date picker and i want to set maximum date as TODAY date which will change daily.
For ex today's date is 28/6/2011 so maximum date is 28/6/2011 but when i use my app tomorrow it will be change to 29/6/2011.
How to set this?
Thanks
Set the maximumDate property in viewWillAppear: method like this,
datePicker.maximumDate = [NSDate date];
To deal with date change while the application is being used, get the time left till tomorrow and set an NSTimer to trigger off at date change and then update the UIDatePicker instance.
NSDate * tomorrow = [NSDate dateWithNaturalLanguageString:#"12 AM tomorrow"];
NSTimeInterval timeInterval = [tomorrow timeIntervalSinceNow];
/* Create an NSTimer to trigger a method to update the datePicker's maximumDate
after timeInterval */
If you look in the documentation you will see that there is a property of UIDatePicker called maximumDate. If you then look at the documentation for NSDate you will see that the class method date returns an NSDate with the current date and time, therefore:
`someDatePicker.maximumDate = [NSDate date];
will set the date picker to not allow any date (or time) later than that moment.
As [NSDate date] or Date() returns the current today's date.
In Objective-C
Set the date picker's minimumDate property:
datePicker.minimumDate = [NSDate date];
In Swift 4.x,
datePicker.minimumDate = Date()
(Where datePicker is a reference to your UIDatePicker )
Related
I have a strange problem with EKEventEditViewController when using it with custom timezones. It behaves differently in two situations:
Situation 1 - works fine:
Launch app
Create EKEventEditViewController to add new event with startDate = [NSDate date]
New event start is displayed correctly (current time)
Change default timezone with [NSTimeZone setDefaultTimeZone:otherTimeZone]
Create EKEventEditViewController to add new event with startDate = [NSDate date]
New event start is displayed correctly (current time adjusted to time zone)
Situation 2 - unexpected behavior:
Launch app
Change default timezone with [NSTimeZone setDefaultTimeZone:otherTimeZone]
Create EKEventEditViewController to add new event with startDate = [NSDate date]
New event start is displayed incorrectly (system timezone offset + default timezone offset)
Change default timezone back to system timezone [NSTimeZone setDefaultTimeZone:[NSTimeZone systemTimeZone]]
Create EKEventEditViewController to add new event with startDate = [NSDate date]
New event start is still displayed incorrectly (system timezone offset + default timezone offset)
My guess that on first display of EKEventEditViewController it somehow caches default timezone and then uses it as an offset.
Has anyone faced similar problem? Is this a bug or am I missing something?
I had exact same problem. I was storing all the dates in the database in GMT timeZone with offsets (separately). My app uses custom timeZone from the beginning it's being run (GMT). When I wanted to use those dates while exporting an events to the calendar, I was seeing wrong start and end dates. What helped me solving the problem was firstly convert the dates I had stored in the database to the system Time Zone using the following converter method (see below). Such converted date passed to the EKEventEditViewController was then showing the dates correctly. Hope it will solve your problem too.
+ (NSDate *) convertToSystemTimeZone:(NSDate*)sourceDate {
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];
return destinationDate; }
I want to present a date to the user of my app as "Today", "Yesterday" or as a formatted date (i.e. 27/05/2011). Is there a quick way to get "Today" or "Yesterday" based on a given NSDate? If not I can write the code myself, I am just curious if I am overlooking some simpler way than working out remaining hours manually.
If you just want to present date to your user, there is an option in NSDateFormatter right for that.
- (void)setDoesRelativeDateFormatting:(BOOL)b
Take a look at documentation for more information.
Check out this similar question: Compare NSDate for Today or Yesterday.
You make NSDate objects from today and yesterday, and then compare the first 10 characters of their description to the NSDate you're unsure of.
From the Date and Time Programming Guide:
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *today = [[NSDate alloc] init];
NSDate *tomorrow, *yesterday;
tomorrow = [today dateByAddingTimeInterval: secondsPerDay];
yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
[today release];
anyone know or can provide some example code relating to "timeIntervalSinceNow" method...
I need something like... time2(when app eneters foreground) - time1(when app enters background) = time3(the difference in times)... this is so i can use this number(pref in seconds) to calculate the time i have lost while the app has been in background !!
I am having trying trying to create the date objects, receive the object and display/use in a label....
Actually, to answer your original question, myles, you can use timeIntervalSinceNow.
In the statement below, inputDate has been initialized as an NSDate and set to some date (you could just try [NSDate *inputDate = [NSDate date]; to set the date at the current date and time.
NSTimeInterval timeToAlert =[inputDate timeIntervalSinceNow];
The next line is a way to put that NSTimeInterval into a string.
NSMutableString *timeinterval = [NSMutableString string];
[timeinterval appendFormat:#"%f",timeToAlert];
Finally, the app delegate class is typically where code can be written to handle coming in and out of background. Good luck!
timeIntervalSinceNow tells you the offset of an NSDate from the current time. You want timeIntervalSinceDate::
NSDate *appEnteredForeground = ...;
NSDate *appEnteredBackground = ...;
NSTimeInterval difference = [appEnteredBackground timeIntervalSinceDate: appEnteredForeground];
You can calculate the difference between two dates with the timeIntervalSinceDate: method:
//When app enters background:
self.backgroundDate = [NSDate date]; //this should be a property
//...
//When the app comes back to the foreground:
NSTimeInterval timeSpentInBackground = [[NSDate date] timeIntervalSinceDate:self.backgroundDate];
NSTimeInterval is simply a typedef for double, it's measured in seconds. [NSDate date] instantiates an NSDate object with the current date and time.
In my date picker i can select a date from the list.
I save it in my string pickUpDateTime
self.pickUpDateTime = [NSString stringWithString:[apiFormat stringFromDate:d]];
I'm having a problem where the user is able to pick a date in the past. Is there some way to get the current day and check that its in the future?
My string holds the date like this 2010-11-04.
The user shouldnt be able to select a day in the past or the current day.
Thanks
-Code
You can get the current date and time with:
NSDate *now = [NSDate date];
Compare the 2 dates with:
if ([d compare:now] == NSOrderedDescending) {
// d is later than now
} else {
// d is earlier than or equal to now
}
If you're using a UIDatePicker, just set
datePicker.minimumDate = [NSDate dateWithTimeIntervalSinceNow:24*3600];
e.g. to allow dates starting tomorrow. Working back from the date string "2010-11-04" to a NSDate object is possible but cumbersome. (but if you insist, have a look at NSDateFormatter)
Lots of answers above give good advice about working with NSDates in general; things are a tiny bit more complicated if you want to round to the start of the day to say e.g. 'at least tomorrow' rather than 'at least 24 hours away'.
In general terms:
get an instance of NSCalendar to represent the Gregorian calendar
use the NSCalendar to convert an NSDate into an NSDateComponents representing just the day, month and year
use the NSCalendar to convert the NSDateComponents back into an NSDate
use arithmetic as recommended elsewhere to increment the NSDate a day into the future, for example
I have to dash, but relevant methods are:
NSCalendar +currentCalendar or -initWithCalendarIdentifier:NSGregorianCalendar to be extra safe
NSCalendar -components:fromDate: (the first parameter is a flag field indicating which bits of the date you need to be filled in) and -dateFromComponents:
NSDate -timeIntervalSinceDate:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
NSString *tempStr=[NSString stringWithFormat:#"%#",[df stringFromDate:datePicker.date]];
txtBirth.text=tempStr;
NSString *birthDate = tempStr;
NSDate *todayDate = [NSDate date];
int time = [todayDate timeIntervalSinceDate:[df dateFromString:birthDate]];
int numberOfDays = time/86400;
hear number of data return data difference.....
All,
This seems like such a simple thing, but I cannot find* the right method to create a UIDatePicker, in time mode, and have it initialized to a specific time. I don't want date -- just time (think alarm clock). I have created a NSDate object:
NSDate * date = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate: (NSTimeInterval) delta];
pickerView = [[UIDatePicker alloc] init]; // which should be 'now' right?
pickerView.datePickerMode = UIDatePickerModeTime; // which creates just the clock
[pickerView setDate:date];
and in the first line, the interval (delta) is zero. It displays 7:00PM.
This has gotta be so simple that I'm missing it, but I can't find the right way -- anyone?
Thank you in advance!
:bp:
*yes, I have looked, but apparently not in the correct places :(
Take the time zone into account. You will get 0AM only if you are at UTC.
Thank you dkk -- I appreciate your help. The real answer was to not use a DatePicker, but to use a PickerView (w/o the date). That made things simpler and do-able.
Thanks again.
NSDate *date = [NSDate date];
date = [[NSCalendar autoupdatingCurrentCalendar] dateFromComponents:[[NSCalendar autoupdatingCurrentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date]];
date = [date dateByAddingTimeInterval:60 * 60 * 21];
pickerView.date = date;
The steps here with the date are
initialise date with the current date
get the date at midnight
add a time interval to the date (60 seconds * 60 minutes * hour). In this case 21 = 9pm
This works for UIDatePicker with datePickerMode of UIDatePickerModeTime & UIDatePickerModeCountDownTimer