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);
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 have an NSDate object *myDate.I can get the week day from this date and I want to set the weekday to an integer c and get the new date object in that week only.
I searched for it and I found the answer,here is the code..
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:1];
NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:comps toDate:[NSDate date] options:0];
NSLog(#"new date==%#",newDate);
This will return date of the next day.
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"MMM dd, yyy"];
date = [formatter dateFromString:string];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:date];
NSInteger day = [components day];
NSInteger weekday = [components weekday]; // if necessary
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.
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];
I'm trying to figure out what day (i.e. Monday, Friday...) of any given date (i.e. Jun 27th, 2009)
Thank you.
I've been doing:
NSDateFormatter* theDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[theDateFormatter setDateFormat:#"EEEE"];
NSString *weekDay = [theDateFormatter stringFromDate:[NSDate date]];
This has the added bonus of letting you choose how you'd like the weekday to be returned (by changing the date format string in the setDateFormat: call
Lots more information at:
http://developer.apple.com/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369
You may have a look to the NSDate and NSCalendar classes.
For example, here and here
They provide the following code:
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =
[gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
NSInteger day = [weekdayComponents day];
NSInteger weekday = [weekdayComponents weekday];
Use NSCalendar and NSDateComponents. As shown in the NSDateComponents documentation:
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date];
int weekday = [weekdayComponents weekday];
Here's what I wound up with, which works exactly as intended. It takes a date, alters the date to be the first of the month, and gets the day of that date:
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
NSDateComponents *monthDateComponents = [[NSCalendar currentCalendar] components: NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear |
NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate: date];
// build date as start of month
monthDateComponents.year = components.year;
monthDateComponents.month = components.month;
monthDateComponents.day = 1;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
// [gregorian setFirstWeekday:1];
NSDate *builtDate =[gregorian dateFromComponents: monthDateComponents];
// NSDateComponents *weekdayComponents =[gregorian components: NSCalendarUnitWeekday fromDate: builtDate];
NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:#"E"];
NSString *firstDay = [df stringFromDate:builtDate];
if([firstDay isEqual:#"Sun"]) // do for the other 6 days as appropriate
return 7;