Trouble with total time - objective-c

Need help with this.
I have seen other answers but didn't understand them that well. How can I print the time difference between the start and stop time in hour and minutes?
NSDateFormatter *Formatter = [[NSDateFormatter alloc] init];
[Formatter setDateFormat:#"HH:mm"];
if (count == 0) {
_startTime.text = [Formatter stringFromDate:[NSDate date]];
[sender setTitle:#"Stop" forState:UIControlStateNormal];
}else {
_stopTime.text = [Formatter stringFromDate:[NSDate date]];
_startStopLabel.hidden = YES;
//write to UILabel "totalTime" the difference between the stop and start times in hours, minutes.
}
If anything is needed please just ask :) (first post)
thanks all :)

You need to subtract the dates and then convert the resulting time interval:
NSDateFormatter *Formatter = [[NSDateFormatter alloc] init];
[Formatter setDateFormat:#"HH:mm"];
NSDate date1 = nil;
if (count == 0) {
date1 = [NSDate date];
_startTime.text = [Formatter stringFromDate:date1];
[sender setTitle:#"Stop" forState:UIControlStateNormal];
}else {
NSDate date2 = [NSDate date];
_stopTime.text = [Formatter stringFromDate:date2];
_startStopLabel.hidden = YES;
long diff = (long)[date2 timeIntervalSinceDate:date1];
diff /= 60; //Convert to minutes
//write to UILabel "totalTime" the difference between the stop and start times in hours, minutes.
_totalTime.text = [NSString stringWithFormat:#"%d hours, %d minutes", diff/60, diff%60];
}

Related

set UIDatePicker 1 hour later as current time

I think the title says it all.
I can get the current time, but I need the current time + 1 hour too.
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd MMM YYYY hh:mm"];
NSString *dateString = [dateFormat stringFromDate:today];
myUILabel.text = dateString;
How do I format the date so it will add 1 hour to the current time?
NSDate *dateAfter_1hour = [NSDate dateWithTimeInterval:60*60 sinceDate:[NSDate date]];
UIDatePicker *datepicker = [[UIDatePicker alloc]init];
[datepicker setDate:dateAfter_1hour animated:YES];
In Swift:
theDateAnHourFromNow = NSDate().dateByAddingTimeInterval(3600)

How to use the current time in Objective-C

How should I import the current time into my app and use it to do some calculation?
What my app do is that it use the current time and do some calculation and then give me some percentage.
How are you wanting to get the time (in milliseconds?)
There's a few questions on here already - like this one, which should help point you in the right direction.
From the Apple Docs for NSDate
Creates and returns an NSDate object set to the given number of
seconds from the first instant of 1 January 1970, GMT.
[[NSDate date] timeIntervalSince1970];
// use this method to get current time as per your iPhone's time format
+(NSString *)getCurrTime
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];
BOOL is24h = (amRange.location == NSNotFound && pmRange.location == NSNotFound);
if (is24h == YES) {
[formatter setDateFormat:#"HH:mm"];
}
else
{
[formatter setDateFormat:#"hh:mm a"];
}
dateString =[formatter stringFromDate:[NSDate date]];
NSLog(#"%#\n",(is24h ? #"YES" : #"NO"));
return dateString;
}

Re-formatting string

i need help., I have this date in a UIlabel in this format 04/26/2013 . How can I re format it into 04.26.2013 ? I already have below codes:
UILabel *timelabel = [[UILabel alloc] initWithFrame:CGRectMake(xx, xx, xx, xx)];
[timelabel setText:dateToday];
dateToday value is 04/26/2013.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM.dd.yyyy"];
NSString *stringFromDate = [formatter stringFromDate:[NSDate date]];
NSLog(#"today : %#", stringFromDate);
timelabel.text = [dateToday stringByReplacingOccurrencesOfString:#"/" withString:#"."];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM.dd.yyyy"];
NSDate *date = [dateFormatter dateFromString:dateToday];
if(date != nil) {
NSString *formattedDateString = [dateFormatter stringFromDate:date];
[timelabel setText:formattedDateString];
} else {
[timelabel setText:#"Invalid Date"];
}
Added error checking. Always check if the input string is a valid date string or not.
Reference: Date Formatting Guide (though it is for Mac; iOS applies the same)

Comparing NSDates to check if today falls in between

thanks to you guys I have successfully converted my array of strings to NSDates. Now I'm trying to check if today's date falls in between my 2 dates (namely fromDate and toDate). I've seen the following questions but failed to implement them in my code. Be great I can have any other method or help on using the solutions given by the users.
NSDate between two given NSDates
How to Check if an NSDate occurs between two other NSDates
How can I check if an NSDate falls in between two other NSDates in an NSMutableArray
This is what I currently have:
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"yyyy-MM-dd"];
NSTimeZone *tz = [NSTimeZone localTimeZone];
[format setTimeZone:tz];
NSDate *now = [[NSDate alloc] init];
NSString *todaysDate = [format stringFromDate:now];
NSLog(#"todaysDate: %#", todaysDate);
//converting string - date
int size = [appDelegate.ALLevents count];
for (NSUInteger i = 0; i < size; i++) {
Event *aEvent = [appDelegate.ALLevents objectAtIndex:i];
//Convert fromDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *fromDate = [dateFormatter dateFromString:aEvent.fromDate];
[dateFormatter setTimeZone:tz];
NSLog(#"fromDate: %#", fromDate);
//Convert toDate
NSDate *toDate = [dateFormatter dateFromString:aEvent.toDate];
NSLog(#"toDate: %#", toDate);
[dateFormatter release];
//Compare if today falls in between
//codes here
}
Do the following,
NSTimeInterval fromTime = [fromDate timeIntervalSinceReferenceDate];
NSTimeInterval toTime = [toDate timeIntervalSinceReferenceDate];
NSTimeInterval currTime = [[NSDate date] timeIntervalSinceReferenceDate];
NSTimeInterval is basically a double type so you can compare that if currTime is greater than one and less than the other than the date falls between those two NSDates.

Converting a string to an NSDate

How is it possible to convert a string to an NSDate on iOS?
NSString *dateStr = #"20100223";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMdd"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];
[dateFormat release];
Hope this will help you.
You'll want to take a look at NSDateFormatter. Determine the format of the date string and then use dateFromString: to convert the string to an NSDate object.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/YYYY"];
NSDate *date = [dateFormat dateFromString:dateStr];
list = [self getYear:date];
- (NSMutableArray *)getYear:(NSDate*)date
{
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
int year = [components year];
int month = [components month];
int day = [components day];
NSLog(#"%d",year);
NSMutableDictionary *dateDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:#"%d", day], #"day", [NSString stringWithFormat:#"%d", month], #"month", [NSString stringWithFormat:#"%d", year], #"year", nil];
return dateDict;
}
My working version in Swift
func dateFor(timeStamp: String) -> NSDate
{
let formater = NSDateFormatter()
formater.dateFormat = "HH:mm:ss:SSS - MMM dd, yyyy"
return formater.dateFromString(timeStamp)!
}
func timeStampFor(date: NSDate) -> String
{
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH:mm:ss:SSS - MMM dd, yyyy"
return dateFormatter.stringFromDate(date)
}
A simple approach even if you have sqlite DB:
// NSTimeInterval is basically typedef of double so you can receive it as double also.
NSTimeInterval *current = [[NSDate date] timeIntervalSince1970];
[DB addToDB:current];
//retrieve the date from DB
NSDate *retrievedDateItem = [[NSDate alloc] initWithTimeIntervalSince1970:[getDBValueAsDouble]]; //convert it from double (NSTimeInterval) to NSDate
// Set the style
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
// Converted to string
NSString *convertedDate = [dateFormatter stringFromDate:retrievedDateItem];
The output for this example:
Aug 3, 2015, 12:27:50 PM
+(NSDate*)str2date:(NSString*)dateStr{
if ([dateStr isKindOfClass:[NSDate class]]) {
return (NSDate*)dateStr;
}
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormat dateFromString:dateStr];
return date;
}