Create a countdown timer in iPhone App - iphone

I want to use a countdown timer in my app.
I have this date
03-09-2011T20:54:18Z
Now I want to use countdown timer for this date. Can you please suggest if there is any method regarding this. I f I am not clear at any point please let me know. Thanks in advance.

Turn that timestamp into an NSDate* (see here)
Then
NSTimeInterval interval = [timestamp timeIntervalSinceNow];
will always give you the number of seconds between now and the date you care about. Then you can do something like:
#define SECONDS_IN_MINUTE 60
#define SECONDS_IN_HOUR (SECONDS_IN_MINUTE * 60)
#define SECONDS_IN_DAY (SECONDS_IN_HOUR * 24)
NSInteger days, hours, minutes, seconds;
days = interval / SECONDS_IN_DAY;
interval %= SECONDS_IN_DAY;
hours = interval / SECONDS_IN_HOUR;
interval %= SECONDS_IN_HOUR;
minutes = interval / SECONDS_IN_MINUTE;
seconds %= SECONDS_IN_MINUTE;
If you want it to update "live" every second then use performSelector:withObject:afterDelay: with a delay of whatever period you want to update the display (or just 0.0 to do it every run loop)

Related

Coldfusion calculate seconds in days, hours, min

I want to convert seconds to days, hours and minutes
Currently, it works just for hours and minutes but not for days. Can you please support me tell me what I did wrong:
<cfscript>
seconds = '87400';
midnight = CreateTime(0,0,0);
time = DateAdd("s", seconds, variables.midnight);
date= xxxxxxxxxxxxxxxxxxxxx???
</cfscript>
<cfoutput>
#DateFormat(variables.date, 'd')# not working
#TimeFormat(variables.time, 'HH:mm')#
</cfoutput>
For the value 87400 the expected result is
1 Days, 0 hours, 16 minutes
If I take 94152 seconds it will be:
1 days, 3 hours, 22 minutes
The only issue i have is to get the correct days ... hours and minutes are diplayed but not the correct days
thank you for all the support
A simple way to calculate the intervals is by taking advantage of the modulus operator:
totalSeconds = 94152;
days = int(totalSeconds / 86400);
hours = totalSeconds / 3600 % 24;
minutes = totalSeconds / 60 % 60;
seconds = totalSeconds % 60;
For 94152 seconds, the results would be:
Interval
Value
DAYS
1
HOURS
2
MINUTES
9
SECONDS
12
TOTALSECONDS
94152
demo trycf.com
I understand from your question that you don't need to get a certain date and time along a timeline, but convert a total amount of seconds in days, hours and minutes. To do that you don't necessary need to use cfml time and date functions like CreateTime() or DateAdd(). You just may need these in order to get a reference point of time or date along a timeline, which doesn't seem to be the case, otherwise you would know the value of your starting date variable. Thus, you can solve this with plain rule of three. There may be simpler methods, so I'm posting an alternative only.
We know that:
60 seconds is equivalent to 1 minute
60 minutes is equivalent to 1 hour
24 hours is equivalent to 1 day
Thus, your calcualtion within cfml could be like so:
<cfscript>
//Constants for calculation
secondsPerDay= 60*60*24;
secondsPerHour= 60*60;
secondsPerMinute= 60;
//Seconds to convert
secondsTotal=87400;
// temp variable
secondsRemain= secondsTotal;
days= int( secondsRemain / secondsPerDay);
secondsRemain= secondsRemain - days * secondsPerDay;
hours= int( secondsRemain / secondsPerHour);
secondsRemain= secondsRemain - hours * secondsPerHour;
minutes= int( secondsRemain / secondsPerMinute);
secondsRemain= secondsRemain - minutes * secondsPerMinute;
writeoutput( "#secondsTotal# seconds are: #days# days, #hours# hours, #minutes# minutes and #secondsRemain# seconds." );
</cfscript>
That outputs:
87400 seconds are: 1 days, 0 hours, 16 minutes and 40 seconds.

Formatting a Countdown Timer When Timer Reaches Negative Numbers

I have an Int, this Int relates to number of minutes. I convert this Int to hours, minutes and seconds using following code and all works great until the timer is below 0.
let hours = Int(timeRemaining) / 3600
let minutes = Int(timeRemaining) / 60 % 60
let seconds = Int(timeRemaining) % 60
timeLeftLabel.text = String(format:"%02i:%02i:%02i", hours, minutes, seconds)
The output from the above code looks something like this and it counts down perfectly:
02:56:07
However, as soon as the time reaches 0 the format starts looking like this:
-2:-56:-7 (this is obviously after minus 2 hours, it doesn't immediately jump to minus 2 hours, I'm just trying to highlight the point)
As you can see, there are numerous issues here, firstly the minus symbol, I simply want there to be one minus symbol at the start. Then the 0's are missing from hours and seconds, it should simply read:
-02:56:07
The only way I can think to try and resolve this is to use: replacingOccurrences(of) but was wondering if there was a better option?
Any advice much appreciated.
A much better solution is to use a DateComponentsFormatter
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour, .minute, .second]
formatter.zeroFormattingBehavior = .pad
Example
let timeRemaining: TimeInterval = 2 * 60 * 60 + 56 * 60 + 7
let negative = -1 * timeRemaining
print(formatter.string(from: timeRemaining)!)
print(formatter.string(from: negative)!)
02:56:07
-02:56:07
Since you just want a single negative sign in front of the whole string, start with the absolute value of your interval and do your arithmetic, and then prepend the minus sign if the original was negative.
(But I agree that it is better to use a formatter and no arithmetic.)

Why is time frozen here? Can I get seconds since 1970 freshly called each time?

In code that is run repeatedly on my iOS app, I have:
// int timeStampMilliseconds = (ceil([[NSDate date] timeIntervalSince1970] * 1000.0));
// double timeStampMilliseconds = [[NSDate date] timeIntervalSince1970] * 1000.0;
// double timeStampMilliseconds = CACurrentMediaTime() * 1000;
double timeStampMilliseconds = CFAbsoluteTimeGetCurrent() * 1000.0;
double hours = fmodf(timeStampMilliseconds, 86400000) / 3600000.0;
double minutes = fmodf(timeStampMilliseconds, 3600000) / 60000.0;
double seconds = fmodf(timeStampMilliseconds, 60000) / 1000.0;
NSLog(#"Milliseconds: %lf, Hours: %lf, minutes: %lf, seconds: %lf", timeStampMilliseconds, hours, minutes, seconds);
The three ways that I am aware of to get a time are [NSDate date] timeIntervalSince1970, CACurrentMediaTime(), and CFAbsoluteTimeGetCurrent()
When I run them, timeIntervalSince1970 and CFAbsoluteTimeGetCurrent get the UTC time to within seconds... but strangely, when the log statement is reached, the milleseconds go up at an appropriate rate, but the hours, minutes, and seconds are frozen at the time they were started. The clock hands don't move.
CACurrentMediaTime displays more of what one would expect; it accurately tells how much time has elapsed since some time marker, meaning that seconds, minutes, and hours all go up.
I think I could make a workaround behavior to have the initializer initialize a constant to TimeIntervalSince1970 minus CACurrentMediaTime, and then in the quoted code work add the saved constant and CACurrentMediaTime. But I would like to know what is going on and why, for two out of the three timekeeping methods, the raw number of milliseconds feeding into hours, minutes, and seconds updates, but the hours, minutes, and seconds are frozen.
Thanks,
For a double use fmod() not fmodf().
The format probably wants to be: %0.f not %lf.
The fact that the Milliseconds display is correct indicated the problem must be in the math for the other units, not the time methods.
Simpler code:
double timeStampSeconds = [[NSDate date] timeIntervalSince1970];
double hours = fmod(timeStampSeconds / 86400, 24);
double minutes = fmod(timeStampSeconds / 3600, 60);
double seconds = fmod(timeStampSeconds, 60);
NSLog(#"Milliseconds: %lf, Hours: %.0f, minutes: %.0f, seconds: %.0f", timeStampSeconds * 1000.0, hours, minutes, seconds);
But all in all use the Cocoa methods such as NSDateComponents and NSDateFormatter.

VBSCRIPT - Have Current Time and a Duration and want to calculate start time based on these values

Example:
Time = 09:41:46
Duration = 0:00:17 (IE 17 seconds)
Start Time = Time - Duration
Clearly I can't just break this up into hours minutes and seconds and do a basic minus operation given the 60 minute hour and 60 second minute etc.
Can't seem to get my head around how to calculate this and hoping someone has come across this before :).
You can use the DateAdd function.
For example, this will subtract 17 seconds from the specified date/time.
DateAdd("s", -17, "1/1/2013 09:41:46")
Try this
Time = 09:41:46
Duration = 0:00:17
Start_Time = FormatDateTime(Time - Duration, 3)
wscript.echo hour(Start_Time)
wscript.echo minute(Start_Time)
wscript.echo second(Start_Time)
References:
https://www.w3schools.com/asp/asp_ref_vbscript_functions.asp#date

UIDatePicker - Difference between 2 dates in hours:minutes

I have this
NSDate *date1Val = date1.date;
NSDate *date2Val = date2.date;
NSTimeInterval interval = [date2Val timeIntervalSinceDate:date1Val];
int hours = (int)interval / 3600; // integer division to get the hours part
int minutes = (interval - (hours*3600)) / 60; // interval minus hours part (in seconds) divided by 60 yields minutes
NSString *timeDiff = [NSString stringWithFormat:#"%d:%d", hours, minutes];
but it doesnt show me correct date - for 24 hours it says 23:57 no idea why
When selecting a date using a UIDatePicker in date only mode the time that was set in date + time mode is also present in the supplied date. You can go into IB, switch the UIDatePickers to Date & Time mode and make sure that the hour and minute are the same.