This question already has answers here:
How do I add 1 day to an NSDate?
(30 answers)
Closed 9 years ago.
I'm trying to use dateByAddingTimeInterval: to add 8 days to my current date but it gives me a weird result.
This is the code I'm using:
-(void)requestForGetEPGChannelsProgramsSucceed:(id)jsonResponse andEpgId:(NSString *)epgId forDate:(NSDate *)forDate dayOffset:(NSInteger)dayOffset
NSDate *dateWithOffset = [forDate dateByAddingTimeInterval:(dayOffset * 86400.0)];
forDate represents the date of today with hour 0 and minutes 0. For this example forDate is 30/09/2013 00:00
dayOffset is 8.
I would expect to get 8/10/2013 00:00 but the value I'm getting (not printing) is 7/10/2013 23:00.
Why is that? Does someone have a clue?
EDIT: I just noticed that the first dates that come out well are IDT and after a few days it uses IST. The difference is between "Israel day-light time" and "Israel standard time" which is 1 hour difference.
How do I get over this obstacle?
Since NSDates have no concept of timezones, and and using a fixed time interval to add 1 day could give you incorrect results, there are 2 two things you need to improve:
NSDate *dateToBeOffset = [NSDate date];
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
[dayComponent setDay:8]; //Days you'd like to offset
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDate *offsetDate = [theCalendar dateByAddingComponents:dayComponent toDate:dateToBeOffset options:0];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"EET"]; // Eastern_European_Time
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeZone:timeZone];
NSLog(#"%#", [dateFormatter stringFromDate:offsetDate]);
Related
This question already has answers here:
Objective C - get the following day from today (tomorrow)
(3 answers)
Closed 9 years ago.
I have an app in I need the date of the next day though I have looked around and found nothing on how to do this.
Edit:
Found the answer at last. Here is what I did:
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSDate *startOfToday;
NSDate *tomorrow;
NSTimeInterval interval;
[cal rangeOfUnit:NSDayCalendarUnit
startDate:&startOfToday
interval:&interval
forDate:now];
tomorrow = [startOfToday dateByAddingTimeInterval:interval];
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"MM-dd-yy"];
tommorowDate = [df stringFromDate:tomorrow];
Though this was all thanks to one of the answers
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSDate *startOfToday;
NSDate *tomorrow;
NSTimeInterval interval;
[cal rangeOfUnit:NSDayCalendarUnit
startDate:&startOfToday
interval:&interval // interval will hold the duration of the day. DST-aware.
forDate:now];
tomorrow = [startOfToday dateByAddingTimeInterval:interval];
tomorrow now holds an NSDate object representing 0:00 of the next day in the device's timezone.
NSDate is representing a moment in time. To display it without time portion, use a NSDateFormatter. There are plenty of examples around here.
I have a very interesting problem: When calculating the number of days between two dates, my calculator gives the wrong results for the month of March only. I have two text fields, one for each date. If I enter 3/7/12 in date1, and 3/13/12 in date2, the result is 7, which is correct (I am counting the first day as well). But when I enter date1 = 3/7/12 and date2 = 3/14/12, the result is still 7, but it should be 8. Likewise, if I enter date1 = 3/7/12 and date2 = 3/23/12, the result should be 17, but it is 16. If I change the month to April so that date1 = 4/7/12 and date2 = 4/23/12, the result is 17. Every month is working as intended, only the month of March is giving me wrong results. Does anyone have any idea what I am missing? Is this a timezone problem? How do I fix it? Here is my code:
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"MM/dd/yyyy"];
NSDate *startdate1 = [dateFormatter1 dateFromString: date1.text];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"MM/dd/yyyy"];
NSDate *enddate2 = [dateFormatter2 dateFromString: date2.text];
int start1 = [startdate1 timeIntervalSince1970];
int end2 = [enddate2 timeIntervalSince1970];
double difference12 = end2-start1;
int days12;
days12 =(int)((double)difference12/(3600.0*24.00)+1);
result12.text = [[NSString alloc] initWithFormat:#"%i", days12]
If you use NSCalendar, you won't need to deal with Daylight Savings Time calculation.
int days = [[[NSCalendar currentCalendar] components:NSDayCalendarUnit
fromDate:startDate1
toDate:endDate2
options:0] day] + 1;
Most like this is a daylight savings issue (March 11th, 2012). Set the date formatters' timezones to:
[NSTimeZone timeZoneForSecondsFromGMT:0];
This should fix the issue.
To add to others, please do not rely on number of seconds in a day being 86400 all the time, it won't. Be sure to watch Session 211 Performing Calendar Calculations from WWDC 2011.
You can think of NSDate as a point in time. If you want to calculate number of days between two dates, this is a calendar issue and there are more than one calendars (like the Mayan calendar).
What you need is NSDateComponents:
NSDate *dateToday = [NSDate date];
NSDateComponents *dateBComponent = [[NSDateComponents alloc] init];
[dateBComponent setDay:-10];
NSDate *date10DaysAgo = [[NSCalendar currentCalendar] dateByAddingComponents:dateBComponent
toDate:dateToday
options:0];
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components = [calendar components:NSDayCalendarUnit
fromDate:date10DaysAgo
toDate:dateToday
options:0];
NSLog(#"Difference: %d", components.day);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I get the day of the week in Objective-C?
I searched for many solutions on websites but i was unable to find it. For a particular problem I need to know what is the current day of week.In other words I need to know how to know whether it is sunday, monday, tuesday or so on.
NSCalendar* cal = [NSCalendar currentCalendar];
NSDateComponents* comp = [cal components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
NSLog(#"weekday - %d", [comp weekday]); // 1 = Sunday, 2 = Monday, etc.
Try this:
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEEE"];
NSLog(#"%#",[dateFormatter stringFromDate:now]);
See this link too
Day Name From NSDate?
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
NSDate not returning correct date
when I try to display current time like so:
NSDate *mydate = [NSDate date];
I get this result:
2012-11-24 09:27:13.194 myApp[5284:c07] mydate = 2012-11-24 07:27:13 +0000
the time is off by two hours although the simulator time and OS X time is different. How can I fix that? Thank you in advance..
You need to print out the date with an NSDateFormatter. NSDate doesn't have any timezone information attached to it. Make sure to attach a timezone to your date formatter.
NSDate* now = [NSDate date];
// GET A DATE FORMATTER AND SET THE TIMEZONE
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.timeZone = [NSTimeZone systemTimeZone];
// CHANGE FORMAT TO SUIT YOUR NEEDS
[df setDateStyle: NSDateFormatterFullStyle];
[df setTimeStyle: NSDateFormatterFullStyle];
// GET YOUR FORMATTED DATE STRING
NSString *dateString = [df stringFromDate: now];
Hi I'm very new to iOS programming and am playing around with dates (todays date and a date 1 year from now).
Here's the code i'm dabbling with.
NSCalendar * calendar = [NSCalendar currentCalendar];//Create a calendar
NSDate *todaysDate = [[NSDate alloc]init]; //get todays date
NSString *dateToday = [NSString stringWithFormat:#"%#",todaysDate];//convert it to a string
NSDateFormatter *df = [[NSDateFormatter alloc] init];// create a formatter
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ" ];//input how the date looks as a string
myDate = [df dateFromString: dateToday];// change it back to a proper NSDate
NSDateComponents *components = [[NSDateComponents alloc] init]; // create the components
[components setYear:1];//add 1 year
nextYear = [calendar dateByAddingComponents:components toDate:todaysDate options:0]; // build the year from component into a variable
dateNextYear = [NSString stringWithFormat:#"%#",nextYear];//convert it to a string
NSDateFormatter *yearFormat = [[NSDateFormatter alloc] init];// create a formatter
[yearFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ" ];//input how the date looks as a string
myDateInTheFuture = [yearFormat dateFromString: dateNextYear];// change it back to a proper NSDate
NSLog(#" next years date is %# ", myDateInTheFuture);
[yearFormat release];
[components release];
[todaysDate release];
I can get the current date and the future date 1 year from now, but i'm unsure how i would store the future date for comparison, i know i can use the "compare" item for NSDate to check, but when i set the future date to a variable every time it runs it stays relative 1 year apart from what i'm checking it against which is todays date.
Its 3am where i am and my brain is mush so apologises in advance if this is the simplest thing ever and i just can't see it.
Its my first post so go easy on me please.
Thanks
I am not entirely sure what you are trying to do, but this is what I gather:
You want to take the current date, add a year to it, and manipulate the resulting date.
Please notify me if this is not correct.
For this, try the following:
NSDate *todayDate = [NSDate date]; //Create a date that is set to today
NSDate *resultingDate = [calendar dateByAddingTimeInterval:31556926; //Take the current date and add the amount of seconds in a year to it
If you want to store this permanently, use the NSUserDefaults:
to set:
[userDefaults setObject:resultingDate forKey:#"storedDate"];
Hope this helps,
HBhargava
to get:
NSDate *returnedDate = [userDefaults dateForKey:#"storedDate"];