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];
Related
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 want to test if two NSDate objects have the same year/month/day but different times of day. Here is my code:
NSDate *date1 = [dataDictionary1 valueForKey:#"date"];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
calendar.timeZone = [NSTimeZone systemTimeZone];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date1];
NSDate *newDate1 = [calendar dateFromComponents:components];
NSDate *date2 = [dataDictionary2 valueForKey:#"date"];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
calendar.timeZone = [NSTimeZone systemTimeZone];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:date2];
NSDate *newDate2 = [calendar dateFromComponents:components];
However, newDate1 is returning with 2012-05-01 05:00:00 +0000 and newDate2 with 2012-05-01 04:00:00 +0000.
The hours are not zero because my time zone is not GMT, but why are the two hours not equal? Do you know a better way to test if dates with differing times are equal? (That is, more efficient than getting date components for each and testing equality for each day/month/year?)
Try this:
NSDate *date1 = [dataDictionary1 valueForKey:#"date"];
long noTime1 = [date1 timeIntervalSinceReferenceDate] / (60*60*24);
NSDate *date2 = [dataDictionary2 valueForKey:#"date"];
long noTime2 = [date2 timeIntervalSinceReferenceDate] / (60*60*24);
if (noTime1 == noTime2) {
// same date
}
Of course this only works if you simply want to compare the date portion and don't care about the actual day, month, or year values.
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.
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]);