How can I convert an integer such as 115900 to a time? I'd like to do arithmetic operations on times so that something like: 115900 + 100 will equal 120000, rather than 11600.
Your big problem is that an integer number does not behave like a date/time. Since you are using Objective-C, you really should be using the NSDate class and the associated classes for formatting dates and times and managing calendars.
Start by reading the Date and Time Programming Guide. That will be better than me writing it all out again.
int seconds = 115900 % 60;
int minutes = (115900 / 60) % 60;
int hours = 115900/ 3600;
return [NSString stringWithFormat:#"%02i:%02i:%02i",hours, minutes, seconds];
//output like is HH:MM:SS
Related
Is there a method in matlab to convert seconds from a known date to a standard date time format?
For example, if I have a vector of values shown as seconds from 1901/01/01, how would I convert them to a dateTime? In this case a value of 28125 would correspond to 1981/01/01. Is there an efficient method for doing this?
The numbers in your example do not make sense so it is not clear if your time is in seconds or days but since you asked for seconds I will use this.
What you want to achieve can be done using datenum function. This function returns the number of (fractional) days from 1/1/0000. So first you need to find your offset, e.g.:
offsetInDays = datenum(1901,1,1);
Next, you convert the date from seconds to days:
dateInDays = YourRequiredDateInSec * 3600 * 24;
Finally, you date is given by
RequiredDate = datestr(offsetInDays + dateInDays);
I have a timer in my app. When I click on exit buton then timer gets stop and stores value into the string in format of 01:15:55 . I have an array to store this string object.
What I want is , now I want to display these values by comparing to each other. So I think first I have to convert the string into the NSDate but I am having only time format and do not want to store date.
How can I accomplish this task ? any suggestion ?
EDITED : code
NSInteger secondsSinceStart = (NSInteger)[[NSDate date] timeIntervalSinceDate:sDate]; // sDate = when app get started
myAppDelegate.seconds = secondsSinceStart % 60;
myAppDelegate.minutes = (secondsSinceStart / 60) % 60;
myAppDelegate.hours = secondsSinceStart / (60 * 60);
NSString *result = nil;
if (myAppDelegate.hours > 0)
{
result = [NSString stringWithFormat:#"%02d:%02d:%02d", myAppDelegate.hours, myAppDelegate.minutes, myAppDelegate.seconds];
}
else
{
result = [NSString stringWithFormat:#"%02d:%02d", myAppDelegate.minutes, myAppDelegate.seconds];
}
NSString *tempDateString = [NSString stringWithFormat:#"%d:%d:%d",[myAppDelegate hours],[myAppDelegate minutes],[mogsApp seconds]];
Now I want to convert tempDateString into the NSDate so I can compare with similar objects. Is it possible ?
Thanks...
Sounds like an NSTimeInterval might be more appropriate. This is just a floating-point value indicating a number of seconds (including fractional seconds). You can manually format a value like this into whatever string format you want with some simple division and remainder math. (NSDate will give you time intervals since a reference date or other dates if you want to use those to get the values.) You can store NSTimeIntervals as strings if necessary.
NSDateComponents is always a good choice when storing only parts of a date/time (or a timespan).
It also gives you easy access to time management methods via NSCalendar. Then (unlike using NSTimeInterval), you don't have to set up any of the math yourself, and it will all automagically localize.
I did not really know on how to title this question so hopefully you've find the way in :)
My Problem is:
I wanted to set a clock time for a label with a UISlider.
So basically my slider min value is 0000 and the max value is 2400. (24 hour format)
So how do I achieve a properly formatted clock?
For example if my slider's value is at (1161)11:61 it should be (1201)12:01 and so on.
Any tipps for that :)
Would be great to get some help here.
Thanks to all who participate.
why don't you start from 0 to 1440. (24 hours = 1440 minutes) and do something like below.
int hours = slider.value / 60; -> no of hours;
int minutes = slider.value %60; -> no of minutes;
NSString *clock = [NSString stringWithFormat:#"%d : %d", hours, minutes];
You could do this with an NSDateComponents object. Create one, then break up your slider value into two parts: the thousands and hundred digits become the hour, and the tens and ones digits become the minute. You can feed this object to an NSCalendar to transform it into an actual NSDate (if that's what you want).
I am developing Alarm Clock.
I want to compare a time now and setTime. Is it possible to compare in minutes only.
My Problem is NSDate will compare in seconds, for example 9:38:50 is not equal to 9:38:00.
how can I compare in minutes?
First, convert them to UNIX timestamp ignoring milliseconds.
NSUInteger time1 = (NSUInteger)[date1 timeIntervalSince1970];
NSUInteger time2 = (NSUInteger)[date2 timeIntervalSince1970];
Ignore seconds.
time1 = time1 - (time1 % 60);
time2 = time2 - (time2 % 60);
Now, you can safely compare them.
if (time1 == time2) {
NSLog(#"Bingo");
}
Use NSCalendar to get the NSDateComponents you're interested in. Then it's easy to compare those.
Though it might be sufficient to check whether the current date is later than the alarm date. If it is and the alarm wasn't stopped, you should probably go off even if the computer momentarily lost power or something.
I'm programming against a Webservice that requires the amount of ticks (for the current time) (A single tick represents one hundred nanoseconds or one ten-millionth of a second) since 1.1.0001 (midnight).
what is the easiest way to get the amount of ticks from an NSDate Object?
thanks for your help
Just make 1.1.0001 into an NSDate and then use the NSDate method timeIntervalSinceNow and multiply by 10 million.
This code will give you GMT time.
const long long UNIX_EPOCH_IN_CLR_TICKS = 621355968000000000 ;
long long time = UNIX_EPOCH_IN_CLR_TICKS + [[NSDate date] timeIntervalSince1970] * pow(10, 7);