Try using NSDate for time only

I am struggling to find a way to use NSDate for time purposes only. I tried to make the following code:

- (void)awakeFromInsert { NSDateComponents *comps = [[NSDateComponents alloc] init]; comps.minute = 45; comps.second = 0; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; self.periodDuration = [calendar dateFromComponents:comps]; NSLog(@"periodDuration: %@",self.periodDuration); comps = [[NSDateComponents alloc] init]; comps.hour = 8; comps.minute = 0; self.firstPeriodStartTime = [calendar dateFromComponents:comps]; NSLog(@"firstPeriodTime: %@",self.periodDuration); } 

But I get the result:

 periodDuration: 0001-12-31 22:24:04 +0000 firstPeriodTime: 0001-12-31 22:24:04 +0000 

The result I was expecting:

 periodDuration: 45:00 firstPeriodTime: 08:00 

What am I doing wrong? How can i fix this? Thanks.

+4
source share
3 answers

The date log is misleading if you create a date with only a few components, you will not receive a corresponding copy of the date that uniquely identifies the date and time.

If you try to compile this code, you may find that if you simply convert dates to strings, this is exactly the same as you expect

 NSDateComponents *comps = [[NSDateComponents alloc] init]; comps.minute = 45; comps.second = 0; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDate *periodDate = [calendar dateFromComponents:comps]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"mm:ss"]; NSLog(@"periodDuration: %@",[dateFormatter stringFromDate:periodDate]); comps = [[NSDateComponents alloc] init]; comps.hour = 8; comps.minute = 0; NSDate *firstPeriodDate = [calendar dateFromComponents:comps]; [dateFormatter setDateFormat:@"HH:mm"]; NSLog(@"firstPeriodTime: %@",[dateFormatter stringFromDate:firstPeriodDate]); 

periodDuration: 45:00

firstPeriodTime: 08:00

+12
source

You can use NSDateFormatter

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"HH:mm"]; NSLog(@"%@",[dateFormatter stringFromDate:[NSDate date]]); 
+3
source

Use NSTimeInterval (this is basically a double containing the number of seconds). To save it to CoreData :

 [NSNumber numberWithDouble:myTimeInterval]; 

Regarding formatting: if you want to display more than 24 hours, use this:

 NSUInteger seconds = (NSUInteger)round(myTimeInterval); NSString *formattedDate = [NSString stringWithFormat:@"%02u:%02u:%02u", seconds / 3600, (seconds / 60) % 60, seconds % 60]; NSLog(@"%@", formattedDate); 

If you don't want more than 24 hours to go, you can use NSDate and NSDateFormatter :

 NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:myTimeInterval]; NSDateFormatter *dateFormatter = [NSDateFormatter new]; [dateFormatter setDateFormat:@"HH:mm:ss"]; [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]]; NSString *formattedDate = [dateFormatter stringFromDate:date]; NSLog(@"%@", formattedDate); 
+2
source

Source: https://habr.com/ru/post/1479112/


All Articles