Addition of two NSDate in Objective-C - ios

I have two NSDates with this format : "H.m.s" and I want to do an addition of this two dates to compare the result with my current time on my phone.
I have all the information as you can see bellow :
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"H:m:s"];
NSDate *start = [dateFormat dateFromString:NextTVS.startTime];
NSDate *duration = [dateFormat dateFromString:NextTVS.duration];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *date = [NSDate date];
[calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
However I don't know how to add start and duration.
Thanks for your help

Use the dateByAddingComponents method of the NSCalendar class like so:
NSDateComponents *comps = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
NSDate *newDate = [calendar dateByAddingComponents: comps toDate: date options:0];

Related

Issue with NSDate

I am having a NSDate object in format as 2018-01-19 18:30:00 +0000. Now i want to change only the time components. After changing the time components the NSDate Object would be as 2018-01-19 10:00:00. I did tried various solution but nothing worked. If any one have any solution regarding this issue help out. Thank you in advance
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components: NSCalendarUnitYear| NSCalendarUnitMonth| NSCalendarUnitDay fromDate:date];
[components setHour:hour];
[components setMinute:minute];
[components setSecond:second];
NSDate *newDate = [calendar dateFromComponents:components];
another solution i tried is
NSDate *oldDate = date;
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components:unitFlags fromDate:oldDate];
comps.hour = 10;
comps.minute = 00;
comps.second = 00;
NSDate *newDate = [calendar dateFromComponents:comps];
Every time i get output is next date of input date
You can change the time components when you create a new NSDate object with:
NSDate *date = [calendar dateBySettingHour:10 minute:0 second:0 ofDate:[NSDate date] options:0];
Answer from: https://stackoverflow.com/a/5611700/4535184
Here is your code just add dateformater to formate the date
NSDate *oldDate = [[NSDate alloc]init];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components:unitFlags fromDate:oldDate];
comps.hour = 10;
comps.minute = 00;
comps.second = 00;
NSDate *newDate = [calendar dateFromComponents:comps];
NSDateFormatter *formater = [[NSDateFormatter alloc]init];
formater.dateFormat = #"YYYY-MM-dd hh:mm:ss";
NSLog(#"New Date : %#",[formater stringFromDate:newDate]);
Try this
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss zzzz"];
NSDate *date = [formatter dateFromString:#"2018-01-19 18:30:00 +0000"];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components: NSCalendarUnitDay| NSCalendarUnitHour| NSCalendarUnitMinute|NSCalendarUnitDay|NSCalendarUnitMonth|NSCalendarUnitYear fromDate:date];
[components setHour:10];
[components setMinute:0];
[components setSecond:0];
NSDate *newDate = [calendar dateFromComponents:components];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSLog(#"%#",[formatter stringFromDate:newDate]);

iOS set date to MM//DD/YY 00:00:00

I know there's a few questions already on this but I keep running into the same error regardless of what I do to my code. I am working in the simulator, I have location set to my long/lat. When I try to convert the current date to zero hours, minutes, seconds I always get this result:
2015-07-10 08:53:19.868 xxxxxxxxxxxxxxxxx[2184:7462272] Today at start of day:2015-07-10 04:00:00 +0000
Here's my code:
NSDate* today = [[NSDate alloc] init];
NSCalendar* currentCalendar = [NSCalendar currentCalendar];
NSDateComponents* dateComponents = [currentCalendar components:NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitYear fromDate:today];
NSInteger thisMonth = [dateComponents month];
NSInteger thisDay = [dateComponents day];
NSInteger thisYear = [dateComponents year];
NSDateComponents* dayComponents = [[NSDateComponents alloc] init];
[dayComponents setDay:thisDay];
[dayComponents setMonth:thisMonth];
[dayComponents setYear:thisYear];
NSDate* todayAtMidnight = [currentCalendar dateFromComponents:dayComponents];
NSLog(#"Today at start of day:%#", todayAtMidnight);
Even if add
[dayComponents setHour:0]; the hour still comes out as 4.
What am I doing wrong?
To get the beginning of today you can use
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
fromDate:[NSDate date]];
[calendar setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSLog(#"date %#", [calendar dateFromComponents:components]);
Make sure you have the correct timezone
2015-07-10 04:00:00 +0000 is UTC, not your local timezone.
To display it in your local timezone, use an NSDateFormatter:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSLog([dateFormatter stringFromDate:todayAtMidnight]);
Use NSDateFormatter as follow because system timezone is UTC:
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate * curDate = [formatter dateFromString:[NSDate date]];
Then you'll get 2015-07-10 00:00:00.

dateFromComponents returning wrong value

I'm trying to set the month and year of [NSDate date] but the function dateFromComponents return the wrong value, this is my code:
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian]; // Setup an NSCalendar
NSDateComponents *components = [gregorianCalendar components: NSUIntegerMax fromDate: [NSDate date]]; // Setup NSDateComponents
[components setTimeZone:[NSTimeZone defaultTimeZone]];
[components setMonth:monthInt];
[components setYear:yearInt];
// Setup the new date
NSDate *dateToCompare = [gregorianCalendar dateFromComponents: components];
yearInt is 2014 and monthInt is 12, but it reports back 12-2015 and not 2014, why is this?
Thanks!
you need to put only the bit operator you need, try this:
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian]; // Setup an NSCalendar
NSDateComponents *components = [gregorianCalendar components: NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear |NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitTimeZone fromDate: [NSDate date]]; // Setup NSDateComponents
[components setTimeZone:[NSTimeZone defaultTimeZone]];
[components setMonth:12];
[components setYear:2014];
// Setup the new date
NSDate *dateToCompare = [gregorianCalendar dateFromComponents: components];
NSLog(#"Ver %#",dateToCompare);
NSUIntegerMax = 18446744073709551615
In bit representation: 10000000000000000000000000000000000000000000000000000000000000000
To many switch off.

Constructing an NSDate from today's date and a string with the time

If I have a string representing a time, say "10:45 am", and do the following to parse the string:
NSDateFormatter *dateFormat;
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"h:mm a"];
NSLog(#"%#", [dateFormat dateFromString:#"10:45 AM"]);
I would get this logged:
2013-09-09 17:52:30.416 TimeTest[49491:a0b] 2000-01-01 15:45:00 +0000
How can I create an NSDate for the current day at the given time? I tried this
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"h:mm a"];
NSDate *time = [formatter dateFromString:#"10:45 AM"];
NSDateComponents *timeComponents = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit ) fromDate:time];
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];
NSDateComponents *newComponents = [[NSDateComponents alloc]init];
newComponents.timeZone = [NSTimeZone systemTimeZone];
[newComponents setDay:[dateComponents year]];
[newComponents setMonth:[dateComponents month]];
[newComponents setYear:[dateComponents year]];
[newComponents setHour:[timeComponents hour]];
[newComponents setMinute:[timeComponents minute]];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *combinedDate = [gregorianCalendar dateFromComponents:newComponents];
NSLog(#"%#", combinedDate);
with the result
2013-09-09 19:57:14.506 TimeTest[49712:a0b] 2019-03-06 15:45:00 +0000
How should I go about this?
I'm not exactly sure what you are looking for. For what I understand, you want to build a date with the current year, month and day, but with your supplied time by parsing it from a string.
If that is the case, as others have pointed out, you need to play with NSDateComponents.
Based on your code I wrote these lines. They should build a date by merging two dates. The current one and the one you parsed.
// Get the full current date
NSDate *date = [NSDate date];
// Get the current calendar
NSCalendar *calendar = [NSCalendar currentCalendar];
// Split the date into components but only take the year, month and day and leave the rest behind
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];
// Build the date formatter
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"h:mm a"];
// Convert the string time into an NSDate
NSDate *time = [formatter dateFromString:#"10:45 AM"];
// Split this one in components as well but take the time part this time
NSDateComponents *timeComponents = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit ) fromDate:time];
// Do some merging between the two date components
dateComponents.hour = timeComponents.hour;
dateComponents.minute = timeComponents.minute;
// Extract the NSDate object again
NSDate *result = [calendar dateFromComponents:dateComponents];
// Check if this was what you where looking for
NSLog(#"%#",result);
Please be aware that this sample code is by far non-optimized. There are more crisp ways to obtain what you are looking for by using time intervals, but I felt like you wanted a dirty simple example on how to do components copy and paste and then extracting dates.
This will create a date for the beginning of the day in the current time zone.
NSDate *today = [NSDate date];
NSTimeInterval interval;
NSCalendar *cal = [NSCalendar currentCalendar];
[cal rangeOfUnit:NSDayCalendarUnit
startDate:&today
interval:&interval
forDate:today];
Now we add the time:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// I have to set the locale to posix_en_us, as my system is using 24hour style as default
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setDateFormat:#"hh:mm a"];
NSDate *time = [dateFormatter dateFromString:#"10:45 AM"];
NSDateComponents *comps = [cal components:(NSHourCalendarUnit | NSMinuteCalendarUnit)
fromDate:time];
NSDate *dateAndTime = [cal dateByAddingComponents:comps
toDate:today
options:0];
dateAndTime will now be todays date with 10:45 am in the local timezone.
controlling in the debugger:
po dateAndTime
$0 = 0x41b7df138c00000d 2013-09-10 08:45:00 +0000
This is correct, as my timezone is 2 hours ahead to GMT, as we still have summer time.

ios get 12AM for today

NSDate *today = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setLocale:[NSLocale currentLocale]];
[calendar setTimeZone:[NSTimeZone timeZoneWithName:#"US/Central"]];
NSDateComponents *nowComponents = [calendar components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];
[nowComponents setHour:hour];
[nowComponents setMinute:0];
[nowComponents setSecond:0];
NSLog(#"%#", [calendar dateFromComponents:nowComponents]);
logs 2012-01-01 06:00:00 +0000. The time is correct based on the timezone setting, but today is July 20th... What am I doing wrong?
You need to extract the year, month, and day components, and construct a new NSDate, like this:
NSDate *today = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setLocale:[NSLocale currentLocale]];
[calendar setTimeZone:[NSTimeZone timeZoneWithName:#"US/Central"]];
NSDateComponents *nowComponents = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
today = [calendar dateFromComponents:nowComponents];
You do not need to get the time components and then set them back to zero: you can keep them at their defaults by not asking to extract them in the first place.

Resources