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);
Related
I'm trying to put a time value on a time picker, the problem is that the time gets messed up because of time zone issue. Here's the code snippet I use:
int targetmillisondsFromMidnight = [self.schedule.targetHour intValue]; // 59580000
NSDate* todayMidnight = [NSCalendar.currentCalendar startOfDayForDate:[NSDate new]]; //2019-12-23 00:00:00 UTC
NSTimeZone* timezone = [NSTimeZone defaultTimeZone];
NSInteger seconds = [timezone secondsFromGMT];
todayMidnight = [todayMidnight dateByAddingTimeInterval:seconds];
NSDate* scheduleDate = [NSDate dateWithTimeInterval:targetmillisondsFromMidnight/1000 sinceDate:todayMidnight]; //2019-12-23 16:33:00 UTC
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone defaultTimeZone]];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:scheduleDate];
[self.datePicker setTimeZone:[NSTimeZone defaultTimeZone]];
[self.datePicker setDate:[calendar dateFromComponents:components] animated:YES];
When I do po [components hour] instead of getting 16 as in the scheduleDate I get 18.
How can I fix this?
I tried to change the time zones to localTimeZone and timeZoneWithAbbreviation:#"GMT" but nothing seems to work.
It looks like you do superfluous conversion
Let's consider the following code
NSDate *date = [NSDate new]; // << in UTC always
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone defaultTimeZone]]; // << in local
NSDateComponents *components = [calendar components:
(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date]; // << converted to local
debug output:
(lldb) po date
2019-12-24 09:53:12 +0000
(lldb) po components
<NSDateComponents: 0x600003c78710> {
Hour: 10
Minute: 53
I need to get an NSDate object for 00:00(beginning of the day) from [NSDate date], let's say if currently it is 11:30am(returned by [NSDate date]), on 01/06/2012, now I need to have an NSDate for 00:00am on 01/06/2012.
I haven't tried this, but what is in my mind is:
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
NSDate *morningStart = [calendar dateFromComponents:components];
So I first get current date(say it is 01/06/2012), and construct a NSDateComponent for the date, then I set hour/minute/second to 0 and the year/month/day should not be changed(ie. 01/06/2012) then I create an NSDate for this component setting and can I get a date of 00:00:00 01/06/2012?
What you doing is correct, but when you NSLog morningStart the date will be displayed in GMT time zone
If you wanna make sure that the date is correct, convert it to NSString
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MMM-dd HH:mm:ss"];
NSString *strFromDate = [formatter stringFromDate:morningStart]; // this will return 2012-Jun-21 00:00:00
Converting NSDate to NSString can be helpful but if you need to keep a NSDate object for further processing, here is your solution to have your real morningStart NSDate object set at 00:00:00 time, with care of the timezone as well... As you will see you were not so far from the solution :
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:now];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
int timeZoneOffset = [destinationTimeZone secondsFromGMTForDate:now] / 3600;
[components setHour:timeZoneOffset];
[components setMinute:0];
[components setSecond:0];
NSDate *morningStart = [calendar dateFromComponents:components];
It's very easy to do this in iOS8 using startOfDayForDate:
let date = NSDate()
let calendar = NSCalendar.currentCalendar(calendarIdentifier: NSGregorianCalendar)
let dateAtStartOfDay = calendar.startOfDayForDate(date)
OR you may do it in the traditional way in Swift as follows:
let date = NSDate()
let calendar = NSCalendar.currentCalendar(calendarIdentifier: NSGregorianCalendar)
// Use a mask to extract the required components from today's date
let components = calendar.components(.CalendarUnitYear | .CalendarUnitMonth | .CalendarUnitDay, fromDate: date)
let dateAtStartOfDay = calendar.dateFromComponents(components)!
print(dateAtStartOfDay)
(Note: NSDates are stored relative to GMT. So print will display the relative local time. A clear understanding of TimeZone's is essential to using NSDates properly.)
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 want to compare two NSDates with NOW ([NSDate date]).
NSDate *date1 = [NSDate dateWithString:#"1982-02-12 07:00:00 +0100"];
NSDate *now = [NSDate dateWithString:#"2012-01-25 10:19:00 +0100"]; //example
NSDate *date2 = [NSDate dateWithString:#"1989-02-12 15:00:00 +0100"];
I would like to check if now is between date1 and date2. In the example above this is the case. The date component should be completely ignored, so only the time component should be compared. How could I accomplish this?
Thanks in advance!
unsigned int flags = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:flags fromDate:date1];
NSDate* timeOnly = [calendar dateFromComponents:components];
This will give you a date object where everything but the hours/minutes/seconds have been reset to some common value. Then you can use the standard NSDate compare functions on them.
For reference, here is the opposite question to yours: Comparing two NSDates and ignoring the time component
You can create a date representing the start of today and add the time as components to it to get the boundary dates.
NSDate *now = [NSDate date];
NSDate *startOfToday;
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&startOfToday interval:NULL forDate:now];
NSDateComponents *startComps = [[NSDateComponents alloc] init];
startComps.hour = 7;
startComps.minute = 30;
NSDateComponents *endComps = [[NSDateComponents alloc] init];
endComps.hour = 20;
NSDate *startDate = [[NSCalendar currentCalendar] dateByAddingComponents:startComps toDate:startOfToday options:0];
NSDate *endDate = [[NSCalendar currentCalendar] dateByAddingComponents:endComps toDate:startOfToday options:0];
if ([startDate timeIntervalSince1970] < [now timeIntervalSince1970] && [now timeIntervalSince1970] < [endDate timeIntervalSince1970]) {
NSLog(#"good");
}
NSDateFormatter* formatterDate = [[[NSDateFormatter alloc] init] autorelease];
formatterDate.dateStyle = NSDateFormatterMediumStyle; // whatever format you like
NSDate *first_Date = [formatterDate dateFromString:#""];
NSDate *second_Date = [formatterDate dateFromString:#""];
NSDate *todaysDate = [NSDate date];
NSTimeInterval timeIntFormFirstDate = [todaysDate timeIntervalSinceDate:First_Date];
NSTimeInterval timeIntFronLastDate = [second_Date timeIntervalSinceDate:todaysDate];
int interval1 = timeIntFormFirstDate/60;
int interval2 = timeIntFronLastDate/60;
if (interval1 >0 && interval2 >0)
{
NSLog(#"Today's date is between first and second date");
}
I need to change a NSDate object. What I am basically doing is changing the year value.
for example:
NSString *someYear = #"2093";
NSDate *date = [NSDate date]; // Gets the current date.
... Create a new date based upon 'date' but with specified year value.
So with 'date' returning 2011-03-06 22:17:50 +0000 from init, I would like to create a date with 2093-03-06 22:17:50 +0000.
However I would like this to be as culturally neutral as possible, so it will work whatever the timezone.
Thanks.
Here's my code for setting the UIDatePicker limits for a Date Of Birth selection. Max age allowed is 100yrs
_dateOfBirth.maximumDate = [NSDate date];
//To limit the datepicker year to current year -100
NSDate *currentDate = [NSDate date];
NSUInteger componentFlags = NSYearCalendarUnit;
NSDateComponents *components = [[NSCalendar currentCalendar] components:componentFlags fromDate:currentDate];
NSInteger year = [components year];
NSLog(#"year = %d",year);
[components setYear:-100];
NSDate *minDate = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:currentDate options:0];
_dateOfBirth.minimumDate = minDate;
Take a look at NSCalendar, especially components:fromDate: and dateFromComponents: methods.
I managed to figure the answer with the pointer Hoha gave me.
NSNumber *newYear = [[NSNumber alloc] initWithInt:[message intValue]];
NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int unitFlags = NSYearCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;
NSDateComponents* dateComponents = [gregorian components:unitFlags fromDate:[NSDate date]];
[dateComponents setYear:[newYear intValue]];
NSDate *newDate = [gregorian dateFromComponents:dateComponents];
[newYear release];
Starting in iOS 8 you can set an specific date component. For example:
date = [calendar dateBySettingUnit:NSCalendarUnitYear value:year ofDate:date options:0];