I'm trying to get current hour minutes and seconds. Here is the code I use
NSDate *now = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now];
[self.timerLabel setText:[NSString stringWithFormat:#"%# %# %#", [dateComponents hour], [dateComponents minute], [dateComponents second]]];
When I run the app crash on the line where self.timerLabel is.
What is wrong here ?
When i run the code it works fine here.
what's the following line in your code ? Are you sure that it crashes on the third line in the code you pasted ? maybe the code you pasted is not the same than the one in your xcode.
Edit : it's because you're using %# in your string which belongs to UTF-8 string. [dateComponents hour] etc. are returning NSInteger, so you should use %d instead.
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
NSInteger hour= [components hour];
NSInteger minute = [components minute];
NSInteger second = [components second];
Just doing
NSDate *datenow = [NSDate date];
you will have the current date and time, to generate string representations of a date, you should use an instance of NSDateFormatter.
Related
Im trying to figure out this timestamp format, but it looks weird to me.
There is a timestamp in that format: 184930.60
Explanation is: "This is UTC time since midnight in the form HH:MM:SS.SS."
It looks like number of seconds (based on change in file, for 20Hz GPS module), but it is 51h when you convert these seconds, and this can't be 51h after midnight.
Any idea how to convert that into nsdate?
Details about file format: https://racelogic.support/01VBOX_Automotive/01General_Information/Knowledge_Base/VBO_file_format
This is working example:
-(NSDate *)getDateFromDecimalTime:(double)decimalTime{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HHmmss.SS"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *date = [formatter dateFromString:[NSString stringWithFormat:#"%f", decimalTime]];
//gather current calendar
NSCalendar *calendar = [NSCalendar currentCalendar];
//gather date components from date
NSDateComponents *dateComponents = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
// Get current year/day/month
NSDate *now = [NSDate date];
// Specify which units we would like to use
unsigned units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSCalendar *calendar2 = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar2 components:units fromDate:now];
//set date components
[dateComponents setDay:[components day]];
[dateComponents setMonth:[components month]];
[dateComponents setYear:[components year]];
//save date relative from date
NSDate *completeDate = [calendar dateFromComponents:dateComponents];
return completeDate;
}
I am building an IOS application where I am doing following step.
Parsing the data.
Getting OpenTime and CloseTime.
Set the OpenTime and CloseTime in a NSString variable name as openTime and closeTime.
Now the problem is I want to calculate the different of this two time.
I am getting the value like,
OpenTime = "00:15:00" and CloseTime = "1:30:00"
The Time formate is - 24 hours.
Can someone help me in solving this problem I am new in IOS.
Let say you have two NSDate variables openTime and closeTime
int calendarUnit = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;
NSDateComponents *components = [[NSCalendar currentCalendar] components:calendarUnit fromDate: openTime toDate: closeTime options:0];
int months = [components month];
int days = [components day];
int hours = [components hour];
int minutes = [components minute];
You can use NSDateComponents
NSDate *date = [self getTimeInDateFormatFromString://your time string]; // your method return nsdate from string using nsformatter
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];
you can use comp.hour and minute to get time difference.
NSString *openTime = #"12:48:00";
NSString *closingTime = #"21:52:00";
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:#"hh:mm:ss"];
NSDate *openDate = [formatter dateFromString:openTime];
NSDate *closeDate = [formatter dateFromString:closingTime];
int calendarUnits = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *components = [[NSCalendar currentCalendar] components:calendarUnits fromDate: openDate toDate: closeDate options:0];
NSLog(#"The difference is: %ld hours , %ld minutes, %ld seconds", (long)components.hour, (long)components.minute, (long)components.second);
-(void)compareTwoDate:(NSDate *)start :(NSDate *)end
{
NSDate *end_date =end;
NSDate *startDate =start;
NSTimeInterval distanceBetweenDates = [startDate timeIntervalSinceDate:end_date];
double secondsInMinute = 60;
NSInteger secondsBetweenDates = distanceBetweenDates / secondsInMinute;
}
NSArray *star_array=[#"00:15:00" componentsSeparatedByString:#":"];
NSInteger start_time=[[star_array objectAtIndex:2] integerValue]*3600+[[star_array objectAtIndex:1] integerValue]*60+[[star_array objectAtIndex:0] integerValue];
NSArray *end_array=[#"1:30:00" componentsSeparatedByString:#":"];
NSInteger end_time=[[end_array objectAtIndex:2] integerValue]*3600+[[end_array objectAtIndex:1] integerValue]*60+[[end_array objectAtIndex:0] integerValue];
NSInteger diff_seconds=end_time-start_time;
I am using following code in Xcode:
(void)updateLabel
{
NSDate* now = [NSDate date];
int hour = 23 - [[now dateWithCalendarFormat:nil timeZone:nil] hourOfDay];
int min = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] minuteOfHour];
int sec = 59 - [[now dateWithCalendarFormat:nil timeZone:nil] secondOfMinute];
countdownLabel.text = [NSString stringWithFormat:#"%02d:%02d:%02d", hour, min,sec];
}
And am getting the following error. How can I fix this?
error: invalid operands to binary - (have 'int' and 'id')
You must be using this way :-
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date]];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
[gregorian release];
Hope it helps you
Use this way
NSDate* now = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now];
long hour =23-[dateComponents hour];
long min =59-[dateComponents minute];
long sec =59-[dateComponents second];
NSString *str = [NSString stringWithFormat:#"%02ld:%02ld:%02ld", hour, min,sec];
NSLog(#"--> %#",str);
I've been following the apple docs as well as some code on here and I can't seem to get NSDateComponents to work correctly.
NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:(NSHourCalendarUnit|NSMinuteCalendarUnit) fromDate:dateValue];
[calendar release];
dateValue is some date, I only care about the time of it, and the time is 8:00AM. When I check what components is after this, Hour and Minute are both nil.
I also tried inserting this, because I read that you needed to (although not every example has this):
[components setCalendar:calendar];
That code will work. Maybe it's because you're trying to access the hour and minute incorrectly. I did this and got the current hour and minute in the Console window:
NSDate *dateValue = [NSDate date];
NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:(NSHourCalendarUnit|NSMinuteCalendarUnit) fromDate:dateValue];
NSLog(#"hour: %d, minute: %d", [components hour], [components minute]);
[calendar release];
-hour and -minute return plain integers, not NSNumber objects.
From ios8 onwards above class constant is deprecated, use below code:
NSDate *dateValue = [NSDate date];
NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour|NSCalendarUnitMinute) fromDate:dateValue];
NSLog(#"hour: %ld, minute: %d", (long)[components hour], [components minute]);
can you help out in finding the iphone time through iphone application
NSDate *today = [NSDate date];
Gives you the current time and date.
NSDate *today = [NSDate date];
NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:today];
[calendar release];
NSInteger hour = [dateComponents hour];
NSInteger min = [dateComponents minute];
NSInteger sec = [dateComponents second];
This will actually get just the time from the date, not the whole date. It gets the time in separate integers, hour, min, sec. To put them all back together in a string you can do the following:
NSString *time = [NSString stringWithFormat:#"%i:%i:%i", hour, min, sec];