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.
Related
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
// find next date where the minutes are zero
NSDate *nextHour = [calendar nextDateAfterDate:now matchingUnit:NSCalendarUnitMinute value:0 options:NSCalendarMatchNextTime];
// get the number of seconds between now and next hour
NSDateComponents *componentsToNextHour = [calendar components:NSCalendarUnitSecond fromDate:now toDate:nextHour options:0];
//NSLog(#"%ld", componentsToNextHour.second);
NSString *dec = [NSString stringWithFormat:#"%ld", (long)componentsToNextHour.second];
In the above code, I convert the current date and time into seconds, but I am unable to convert these seconds into unixtimestamp, any ideas on how to go about this will be highly appreciated.
I'm not sure I understand your question. If you are asking for the Unix timestamp at which the next hour starts, do this:
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
// find next date where the minutes are zero
NSDate *nextHour = [calendar nextDateAfterDate:now matchingUnit:NSCalendarUnitMinute value:0 options:NSCalendarMatchNextTime];
time_t unixTimestampAtWhichNextHourStarts = (time_t)nextHour.timeIntervalSince1970;
NSLog(#"ts=%ld", (long)unixTimestampAtWhichNextHourStarts);
Please help me with non-obvious code behavior
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
dateComponents.hour = 15;
dateComponents.minute = 20;
dateComponents.calendar = calendar;
NSDate * endDate = [calendar dateFromComponents:dateComponents];
EndDate is 0001-01-01 12:49:43 +0000. Why?
Hours value may be incorrect due to the difference in the time zones. Why such strange minutes and seconds value?
Regards.
If we are just considering the following code snippet
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setCalendar:calendar];
NSDate *date = [calendar dateFromComponents:components];
date = 0000-12-31 23:06:32 +0000, because:
Calendars encapsulate information about systems of reckoning time in
which the beginning, length, and divisions of a year are defined.
[...]
When there are insufficient components provided to completely specify an absolute time, a calendar uses default values of its choice.
Apple Documentation
and
An NSDateComponents object is meaningless in itself; you need to know what calendar it is interpreted against, and you need to know whether the values are absolute values of the units, or quantities of the units. Apple Documentation
Actually, everything is fine and exactly what the documentation says.
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]);
I need to filter search results based on values that were added yesterday. I have seen plenty on finding yesterday using:
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]];
[components setHour:-24];
[components setMinute:0];
[components setSecond:0];
NSDate *yesterday = [cal dateByAddingComponents:components toDate:[NSDate date] options:0];
predicate = [NSPredicate predicateWithFormat:#"created_at >= %#", yesterday];
But this finds 24 hours since this exact moment in time. I need to filter yesterday as 12:01am-12:00pm. So the actual 24 hour period that was yesterday.
I'm guessing that I need to do something along the lines of:
1. Take the current date
2. Find the time from the current date to 12:01am of the same day
3. Then subtract 24 hours from that date
I feel confident I can do #3 (and #1 of course), but I'm not sure how to go about #2. I maybe over thinking it but I can't seem to grasp how to say: "Ok, it's 8:03am, I need to remove 8 hours and 2 minutes which will put me at 12:01am".
Start with some date of today, for example "now":
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
Subtract one day to get some date of yesterday:
NSDateComponents *minusOneDay = [[NSDateComponents alloc] init];
[oneDay setDay:-1];
NSDate *nowMinusOneDay = [cal dateByAddingComponents:minusOneDay toDate:now options:0];
Compute start and end date of the "day calendar unit" that contains yesterday's date:
NSDate *startOfYesterday;
NSTimeInterval lengthOfYesterday;
[cal rangeOfUnit:NSDayCalendarUnit startDate:&startOfYesterday interval:&lengthOfYesterday forDate:nowMinusOneDay];
NSDate *endOfYesterday = [startOfYesterday dateByAddingTimeInterval:lengthOfYesterday];
This should work even if a daylight savings time transition occurs between today and yesterday.
Generally one should avoid to use explicit time intervals such as "24 hours", because not every day has that length.
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?