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.
Related
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.
I have a model in Swift with an NSDate field named expirationDate and I want to calculate the hours remaining before expiration based on the current date.
Is there an easy way to do this with existing NSDate functionality?
Can't check now, but should be something like
expirationDate.timeIntervalSinceNow / 3600
Just find the number of seconds (NSTimeInterval) between the two dates ('now' and your expiration date) and divide by 60*60 = 3600:
let secondsUntilExpiration = expirationDate.timeIntervalSinceDate(NSDate());
let hoursUntilExpiration = secondsUntilExpiration / 3600
For example:
7> let now = NSDate()
now: NSDate = 2016-02-01 03:44:06 UTC
8> let expirationDate = now.dateByAddingTimeInterval(60*60*10) // ten hours from now
expirationDate: NSDate = 2016-02-01 13:44:06 UTC
9> let secondsUntilExpiration = expirationDate.timeIntervalSinceDate(NSDate());
secondsUntilExpiration: NSTimeInterval = 35991.422316968441
10> let hoursUntilExpiration = secondsUntilExpiration / 3600
hoursUntilExpiration: Double = 9.9976173102690122
// Slightly less than the 10 hours above because of the time it took me to type.
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.
I'm trying to calculate time from distance and speed.
Ie 155 km traveled at 40 km/h. That makes 3.875 hours. But I need that displayed as HH:MM.
How do I convert 3.875 hours into HH and MM?
double kmNumber = [Input1.text doubleValue]; // 155
double kmHourNumber = [Input3.text doubleValue]; // 40
double hoursAndMinutes = number1 / number3; // 3,875
[display setText:#"If you travel %f at %f you would use %f.", kmNumber, kmHourNumber, hoursAndMinutes];
Any help would be appreciated!
int hours = (int)hoursAndMinutes; // the integer part is the hours
int minutes = (hoursAndMinutes-hours)*60; // the fractional part of an hour
NSLog(#"HH:MM = %02d:%02d", hours, minutes);
Use NSDateComponents. Use the following to get the components of the date:
NSDateComponents *components = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:inputDate];
And then just use components.hour and components.minute to get the respective hour and minute as integers. This is better than the other answer, as it will respond accordingly to foreign calendars where hours and minutes do not necessarily follow the same rules.
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)