NSTimeInterval 15mins before - iphone

I have one variable startDate which is in NSTimeInterval.
How can I get NSTimeInterval just 15mins before to actual startDate ?

NSTimeInterval reqStartDate = startDate - (15*60);

Related

Days Difference between current and an up coming Date in iOS

I have tried so many things through the help I got, but I still can't figure out how to do it properly. Here's what I did lastly.
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
[tempFormatter setDateFormat:#"dd-MM-yyy"];
NSDate *currentDate = [NSDate date];
NSDate *fromDate = [NSString stringWithFormat:#"%#",[tempFormatter stringFromDate:currentDate]];
NSLog(#"currentDate %#", fromDate);
NSDate *toDate = [NSString stringWithFormat:#"%#",[tempFormatter stringFromDate:datePicker.date]];
NSLog(#"toDate %#", toDate);
NSTimeInterval interval = [toDate timeIntervalSinceDate:fromDate];
double leftDays = interval/86400;
NSLog(#"Total interval Between::%g",leftDays);
Tell me what I did wrong. Is it the NSDate conversion, that I am not doing properly ??
Thanks.
Your code is all messed up -- both toDate and fromDate are strings not NSDates. Your from date should just be currentDate, and your toDate should just be datePicker.date. You don't need to do anything with converting to strings or using a date formatter to get the time interval.
This line is creating problem.
NSDate *toDate = [NSString stringWithFormat:#"%#",[tempFormatter stringFromDate:datePicker.date]];
It changes the type of toDate from NSDate to __NSCFString. The NSTimeInterval take both of its arguments of NSDate type, but in your case only fromDate is NSDate type.
Change your code with these lines
NSDate *currentDate = [NSDate date];
NSDate *toDate = datePicker.date;
NSTimeInterval interval = [toDate timeIntervalSinceDate:currentDate];
It will surely work (inshaAllah).
You're certainly on the right track; however, you seem to be calling "timeIntervalSinceDate" using two NSString's (even though you're specifying fromDate and toDate as NSDates, look right after that- you're setting those two variables to NSString objects).
To get the interval you're looking for, try:
[datePicker.date timeIntervalSinceDate:currentDate];
That should get you the right interval. In addition, you may want to change leftDays to equal
double leftDays = abs(round(interval/86400));
This will stop leftDays from being an awkward number like -1.00005.
`Passing NSString to NSDate! this code is wrong
try
NSDate *curDate = [NSDate Date];
NSDate *pickerDate = datepicker.date;
then compare both these dates using NSTimeInterval

Get actual date into seconds on iOS SDK

I'm making an app where I need to get the actual date ([NSDate date]) into seconds since 1970 (timeIntervalSince1970). But I don't manage to get it working, how should it be done?
NSTimeInterval seconds = [[NSDate date] timeIntervalSince1970]
Note that NSTimeInterval is actually a double value, not an object.
double intrval=[[NSDate date] timeIntervalSince1970];
-(NSDate*)dateFromDouble:(double) intrval{
return [NSDate dateWithTimeIntervalSince1970:intrval];
}

How to Get current NSDate same to simulator Date? iPhone Sdk

I am trying to get current date with adding device time zone but is show 1 hr late that original date. I thing , I am getting problem of DaylightSavingTime.
How to disable isDaylightSavingTime = FALSE .
here is the code, I have used..
NSDate *date = [NSDate Date];
NSTimeZone *currentTimeZon = [NSTimeZone localTimeZone];
if ([currentTimeZon isDaylightSavingTime])
{
NSLog(#"East coast is NOT on DT");
}
else
{
NSLog(#"East coast is on DT");
}
NSTimeInterval timeZoneOffset = [currentTimeZon secondsFromGMTForDate:currentDate];
NSTimeInterval gmtTimeInterval = [date timeIntervalSinceReferenceDate] - timeZoneOffset;
NSDate *gmtDate = [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval];
NSLog(#"### Announcement gmtDate = %#",gmtDate);
I am getting time with 1 Hour difference, date is perfact.
Use NSCalendar, it understands time zones, daylight savings, etc. As #albertamg says, NSDate is just the time since a reference at UTC (GMT), it has no other concept.

NSDateFormatter subtraction

I have a datepicker for the field "From" and "To" and I want the result of the subtraction.
for example: toValue-fromValue and the result would be in hours.
How to do that?
The difference of the two NSdate objects can be calculated using timeIntervalSinceDate:
NSTimeInterval diff = [toDate timeIntervalSinceDate:fromDate];
The result is given in seconds. Then, you calculate the hours like this:
NSInteger hours = diff / 3600;
If you have two NSDate objects you can compare them using the timeIntervalSinceDate method
NSDate* fromDate = //Get from date picker
NSDate* toDate = //Get from date picker
NSTimeInterval = [fromDate timeIntervalSinceDate:toDate];
NSInteger hours = timeInterval / 60*60; //60 seconds per minute, 60 minutes per hour

How to set Max date dynamically for date picker in iPhone?

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 )