I can't seem to figure out why the day doesn't change when I get to the 6th of November, 2011. All I'm doing is iterating through the days. Any help would be appreciated. Here is my code:
NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents* comp = [[NSDateComponents alloc] init];
[comp setDay:2];
[comp setMonth:11];
[comp setYear:2011];
NSDate* date = [calendar dateFromComponents:comp];
for (int i = 0; i < 7; i++) {
NSDate* d = [date dateByAddingTimeInterval:((3600 * 24) * i)];
NSDateComponents* dComponents = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:d];
int day = [dComponents day];
NSLog(#"\nDate: %#\nDay: %i", [d description], day);
}
This is my output:
Date: 2011-11-02 06:00:00 +0000
Day: 2
Date: 2011-11-03 06:00:00 +0000
Day: 3
Date: 2011-11-04 06:00:00 +0000
Day: 4
Date: 2011-11-05 06:00:00 +0000
Day: 5
Date: 2011-11-06 06:00:00 +0000
Day: 6
Date: 2011-11-07 06:00:00 +0000
Day: 6
Date: 2011-11-08 06:00:00 +0000
Day: 7
Thanks!
Welcome to the wonderful world of date calculations!
#Vladimir is correct. November 6th happens to have ~90,000 seconds in it because that's the day (in the US on the Gregorian calendar) that Daylight Savings Time takes effect. Accept his answer; it's the right one.
This one is to educate you a bit more, because this is a hard topic.
An NSDate represents an absolute point in time. This point is immutable. It will exist regardless of whether humans and their time measurements exist.
A "day" is a relative value. It is relative to many things, such as:
The planet earth. A martian day is not the same thing as an earth day
The calendar of the person doing the calculation. Not all calendars measure time in the same way.
Thus, it is impossible to take a relative value and add it to an absolute value without more context. You have no idea what the relative value is relative to. You make the assumption that everyone is using the same point of reference as you, and that assumption is wrong. For example:
The current year is 2011. But is it? "2011" is relative to the Gregorian calendar. What if you're not using the Gregorian calendar?
The current year is 23. But that's only if you're using the Japanese calendar and specifically using the Japanese era names.
The current year is 5771. But that's only if you're using the Hebrew calendar. And the turnover for the next year (5772) is not the same turnover as the next Gregorian year.
The current year is (probably) 1432. If you're a Muslim.
The current year is 4344, if you're Korean.
And so on and so on.
As a general rule, most calendars are solar based, meaning that one orbit around the sun is equivalent to one "year" in that calendar. But this makes things REALLY complicated, because the number of rotations the Earth makes (a "day") in its orbit around the Sun is not an integral (whole) number. It's something like 365.24 rotations. Enter leap days! But only in certain calendars! In other calendars, we have whole leap months! Or leap seconds. Or leap hours. Or leap-whatever-time-unit-you-wants. And don't even get me started on time zones.
So, how do you deal with this?
The simple answer: YOU DON'T. People much smarter than you or I have already written the code to handle all of this for us.
BEHOLD:
NSCalendar
NSDateComponents
An NSCalendar encapsulates all of the rules and regulations for calculating time (on Earth) according to that system of measurement. NSDateComponents encapsulates the relative nature of measuring time.
So you have an absolute point in time (an NSDate), and you want to know what day of the week it corresponds to. Here's what you do:
Get the appropriate calendar object. For almost everything you'll ever do, this is probably [NSCalendar currentCalendar].
Using the calendar, you say "i want such-and-such 'date components' from this absolute point in time"
The calendar's going to churn and burn and give you back an NSDateComponents that signify whatever information you're looking for.
Or let's say you have an absolute point in time and you want to know "what's the absolute point in time 1 week later?". Who knows how long a week is? I don't. It's usually 7 days, but it doesn't have to be. That's just what I'm familiar with. But I'm sure there are calendars out there that are based on non-7-day weeks. So you make an NSDateComponents object, lash it up to mean "1 week", and say "calendar: I have this date and this relative amount. Add them and tell me the new date" and it does that.
<update>
However, adding date components to dates can also be problematic. For example, what if you want to find the last day of every month this year? (We'll assume the Gregorian calendar here) So you start with an NSDate that happens to fall on Jan 31 and you add "1 month" to it. What do you get? Feb 28! So then you add one month to that. What do you get? Mar 28! That's not the end of the month anymore!
The way to fix that is to always compute the new date from the original date. So instead of adding 1 month to each successive date, you add "n" months to the original date. And that will work correctly.
</update>
You can see how this is a difficult topic. In case you haven't, here's one last hint:
THIS IS A DIFFICULT TOPIC
The sooner you learn to do things correctly, the happier you and your code will be. :)
November, 6 is a day when time changed due to the Day Saving Time so that day actually lasts 25 hours and incorrect result is probably comes from that (in general adding time intervals to a date is unreliable because of calendar irregularities and bahaviour may depend on many parameters: current calendar, time zone, locale settings etc).
The more correct way to iterate through days (per wwdc11 video "Performing Calendar Calculations"(iTunes link)) is to add appropriate number of date components to a starting date on each iteration:
...
NSDateComponents *addComponents = [[NSDateComponents alloc] init];
for (int i = 0; i < 7; i++) {
[addComponents setDay: i];
NSDate* d = [calendar dateByAddingComponents:addComponents toDate:date options:0];
NSDateComponents* dComponents = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:d];
int day = [dComponents day];
NSLog(#"\nDate: %#\nDay: %i", [d description], day);
}
Related
I am developing one ios app now. I have feature in which i have to display a continuous days the app is used.
Means if i use app for 3 days constantly i have to display 3.
And if i have not used it for one day. The counter should reset.
I am using this function to calculate days.
- (int)daysBetween:(NSDate *)dt1 and:(NSDate *)dt2 {
NSUInteger unitFlags = NSDayCalendarUnit;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:unitFlags fromDate:dt1 toDate:dt2 options:0];
return [components day];
}
I am saving last used date. and pass two parameter last used date and current date in it.
But by this function if i used app at 11.30 PM and again used it at 1.00 AM it will not increment day.
You get this because you are not considering the time part of the dates, you need to use rangeOfUnit:.
I suggest to use the #Brain functions mentioned in this SO question:
Number of days between two NSDates
I have to convert a Double value into a Date value in Objective-C
I tried many ways but im not getting it.
date=var1/24;
Here var1 is a double value which should be divided by 24 and stored as a Date into the variable date. How can I do this using Objective-C?
I created the date variable like this:
NSDate *date=[[NSDate alloc]init];
date=(nsdate)var1/24;
How can I do this?
Its a Double Variable.. which will be containing values upto 24 it will be representing HOURS from TODAY..
OK, so you have a relative value that you want to add to an absolute value. Additional work is required.
First, you must decide "What is today?". Is it 12:01am? If it is, in which time zone? GMT? Something else? You have to know this, because 12:01am GMT is not the same thing as 12:01am EDT. So: what is today?
Once you've decided where you're going to be measuring time from, you have to construct an NSDate representing that point in time.
For example:
NSDate *rightNow = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSUIntegerMax fromDate:rightNow];
This will give you an NSDateComponents object, which is an object that represents a point in time relative to a calendar. In this case, we're using the "current calendar" (probably the Gregorian calendar, but not necessarily), and the default time zone is your current time zone. If you need to have it be relative to a different time zone, you can create a new NSTimeZone object and use -[NSCalendar setTimeZone:] to set the calendar's time zone (before asking for the date components).
Now that you've got the date components, we can "reset" things to the appropriate time:
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
Then we can turn it back into an NSDate to make it an absolute point in time:
NSDate *startingPoint = [calendar dateFromComponents:components];
NOW we have our starting point, and can deal with the "hours from today". First, we'll create a date components object to represent however many hours the difference is:
NSInteger hourDelta = var1 / 24;
NSDateComponents *delta = [[NSDateComponents alloc] init];
[delta setHour:hourDelta];
Now, we'll add this relative difference to our absolute starting date:
NSDate *finalDate = [calendar dateByAddingComponents:delta toDate:startingPoint options:0];
And because we're good citizens, we'll clean up our memory (unless you're using Garbage Collection or compiling with ARC):
[delta release];
Some important notes:
All of the manipulations are done via the NSCalendar object, because the NSCalendar is what defines what a "day" means and what an "hour" is. You think "there are 24 hours in a day and 60 minutes in an hour...", but that's not necessarily true. I can create my own calendar that has 10 hours in a day, and 10 minutes in an hour. If I want, I can define a "day" as something other than "one full rotation of the Earth". The NSCalendar define all these rules. Thus, all relative manipulations of dates (adding "hours" and "days" or whatever) must be done via the calendar.
There are methods on NSDate like -dateByAddingTimeInterval:, but this is for when you're dealing with absolute intervals and not relative amounts.
It depends of what you want to do exactly, but a direct cast as (nsdate)var1/24 will definitely NOT work!!!
Anway, what does your variable var1 represent exactly? Minutes ? Seconds ? Hours ? From which reference date? Today? The UNIX Epoch? Is it a UNIX timestamp (seconds since 01/01/1970)? Just asking to "convert a number to a date" means nothing on its own ;-)
Depending on the answer to those questions, you may use either NSDateComponents to produce a date giving the different date components (month, day, hours, minutes, seconds, …), or create a NSDate from a UNIX timestamp using dateWithTimeIntervalSince1970... or use any other method from NSDate or NSDateComponents depending on your needs.
Whatever your problem is, do read* the Date and TIme Programming Guide!! Everything is explained here about date manipulation; it will contain everything you need to answer your question.
I am creating a calendar based app.
I have two dates say present date, and future date(due date), now i need to show the difference between these two dates in terms of days, hours, minutes & seconds and i want to show a timer which will continues to decrement second by second as the time increases and ultimately reaches to zero when due date comes.Basically it will be showing that how much days, hrs and seconds are left for the event.
How can I do that, please help me.
It would be a great help if i can get a similar kind of sample code.
Many Thanks in advance.
iPhone Developer
Maybe It can be helpful to you - http://blog.webscale.co.in/?p=244
Basically you want to use NSTimeInterval and NSDate to calculate the difference in time. For example:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy"];
NSDate *now = [NSDate date];
NSDate *futureDate = [dateFormat dateFromString:#"07/15/2015"];
// Now we can calculate the time interval, which really is an integer
// This is the number of seconds between the 2 dates, so with math you can calculate
// what you need. Remember, when you do dateFromString, the time is midnight 00:00:00
// however, *now is set to the current time. You may want to work on that a bit
NSTimeInterval timeDifference = [futureDate timeIntervalSinceDate:now];
// Also note, if you swap the order and do now timeIntervalSinceDate:futureDate, you
// get a negative number.
I'm getting the current date/time using [NSDate date]. The value returned is an hour in the future. I've check my phones location & time settings and they are correct.
I can display the correct date and time as a string using the code below.
[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterShortStyle]
But I need it to return the correct date/time as a date object as I use it to calculate the estimated time of arrival using -
[date dateByAddingTimeInterval:interval]
I realise my question is similar to this one already asked but none of the answers suit my needs. Thanks in advance!
init] returning date an hour in the past?
Maybe you are confusing the point in time (ie the NSDate object) and the point in time at your location (ie your local time).
If you print a NSDate (like NSLog(#"%#", [NSDate date]); which invokes [date description]) the date representation that is printed is in UTC timezone (+0000) (at least it is on my computer).
So as long as you don't live in an area that uses UTC the date printed by [date description]; is always "wrong". But wrong only means that its representation is not the same representation as the clock in your office. The date (as in point in time) is still correct.
When you use localizedStringFromDate:dateStyle:timeStyle: you are printing the date in your local timezone.
NSDate *date = [NSDate date];
NSLog(#"%#", date);
NSLog(#"%#", [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle]);
at my computer this results in:
2011-02-12 08:32:10.658 x[75647:207] Date: 2011-02-12 07:32:10 +0000
2011-02-12 08:32:10.661 x[75647:207] Date: Saturday, February 12, 2011 8:32:10 AM Central European Time
the printed strings are different, but the NSDate object is still the same. That's why you have to use NSDateFormatters when you show a date to the user. Because the same point in time looks different on different places of the world.
But there are only three places where an UTC formatted date would be one hour in the future, so if you don't live in greenland, cape verde or on the azores I might be totally wrong and there is something wrong with your NSDate objects.
Edit: Out of curiosity I read the documentation about [date description] again. And it says
A string representation of the
receiver in the international format
YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM
represents the time zone offset in
hours and minutes from GMT (for
example, “2001-03-24 10:45:32 +0600”).
So I don't know why the date at my computer is printed in GMT timezone. It might be in another timezone at your computer.
But still, it's only the representation, the date is still the same.
I have an array of NSDates which I build from strings using [NSDate dateFromString]
In the xml I parsed to get the string there was also a timezone string. As far as I can see in the manual NSDate does not in it self deal with timezones. Do I need to always store this timezone value somewhere and pair it with the belonging NSDate each time I need it?
I also need to figure out that if an event starts in London at 10:00, but I am in Denmark having my iPhone set to danish time my "event started in London" should display at 09:00 o'clock.
Again if an event starts in London at 10:00 o'clock and ends in Denmark at 12:00 o'clock, If I were to compare start time and end time using an iPhone with danish settings I would get that the duration of the event was 02:00 event though 10:00 o'clock in UK and 12:00 o'clock in Denmark is only 1 hour apart.
NSdate works really well for these things in the scope of one timezone, but introducing the timezone part just made everything complicated to me. Is there a way to abstract/hide all these calculations, as I see potential for making a lot of mistakes.
I have been through the NSDateformatter and NSDate guides from Apple, but they are really vague and sports a substantial amount of deprecated code :/
Thanks for any help given.
You should take one standard timezone like UTC/GMT format for all calculation.
According to the NSDate reference, dateWithString: takes an offset to GMT as last component; while it is not a time zone, it is sufficient to perform computation or comparison).
Looking at the NSTimeZone reference, you can use the abbreviationForDate: and the timeZoneWithAbbreviation: to get a NSTimeZone object from a NSDate instance. Once you get the time zone, you have everything you need.
I convert the present date and the date I would like to know if is close, to GMT and then returning the difference. So I changed every thing to deal with differences instead of actual times and dates. A bit like a music score transposed to a different key:)
+ (NSInteger) minutesUntilDate:(NSDate*) date withTimezoneOffset:(NSInteger) GMTOffset
{
NSDate *now = [NSDate date];
NSTimeInterval localTimeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
now = [now addTimeInterval:(localTimeZoneOffset * -1)];
date = [date addTimeInterval:(GMTOffset * 60 * 60) * −1];
return ((NSInteger)[now timeIntervalSinceDate:date] / 60 ) * -1;
}
As soon as you have allocated an NSDate, these do not have timezone information any longer. NSDate is "timezone-less" and is always in GMT. You should make sure that NSDate understand your format correctly when allocating it.
Once you have an NSDate you can make normal calculations and ignore the timezones.
You only need to take care of timezones when reading strings into NSDates and when printing them out.