Won't print calculations for hours and minutes to seconds - printing

I'm making a program that calculates minutes and hours into seconds but it won't print, would appreciate any help
enter_hours = int(input("Please enter number of hours: "))
enter_minutes = int(input("Please enter number of minutes: "))
def CalculateSeconds():
hours = enter_hours * 3600
minutes = enter_minutes * 60
return(hours, minutes)

Figured it out
def CalculateSeconds(enter_hours, enter_minutes):
hours = enter_hours * 3600
minutes = enter_minutes * 60
return(hours, minutes)
seconds = CalculateSeconds(enter_hours=int(input("Enter first number: ")),
enter_minutes=int(input("Enter second number: ")))
print(seconds)

Related

Swift - Hours/Minutes/Seconds to Integer conversion

I have Hours / Minutes / Seconds that I would like converted into an integer
7 Hours 30 Minutes 5 Seconds
You could use split():
import Foundation
func getSecondsFromString(timeString: String) -> (Int) {
let timeParts = timeString.replacingOccurrences(of: "[^0-9]", with: " ", options: [.regularExpression])
.split(separator: " ")
.map{Int($0)!}
return timeParts[0] * 3600 + timeParts[1] * 60 + timeParts[2]
}
print(getSecondsFromString(timeString: "7 Hours 30 Minutes 5 Seconds"))
Output:
27005
If you have the time difference between two dates in seconds using timeIntervalSince then you can directly convert this into hours as a double by doing
let hours: Double = elapsedTime / 3600 // 7.501388...
In your examples this is 7.5 which you can then multiply with the hourly rate. If you for some reason only want to use full hours you can either round to the nearest full hour
let fullHours = round(hours) // 8.0
or if you want to truncate minutes and keep the hour then you can do a integer division from the start
let hours: Int = elapsedTime / 3600 // 7

lua number format not working

I have the following function:
function timestamp(duration)
local hours = duration / 3600
local minutes = duration % 3600 / 60
local seconds = duration % 60
return string.format("%02d:%02d:%02.03f", hours, minutes, seconds)
end
when the duration is 4.404 sec it returns 00:00:4.404
what is am looking for is 00:00:04.404
It should be:
string.format("%02d:%02d:%06.3f", hours, minutes, seconds)
Field width contains all characters of the number, including point and fraction.

Convert milliseconds into day,hour,minute and second iOS? [duplicate]

This question already has answers here:
How to convert milliseconds into human readable form?
(22 answers)
Closed 6 years ago.
I need to convert an milliseconds into Days, Hours, Minutes Second.
ex: 5 Days, 4 hours, 13 minutes, 1 second.
Thanks
if you don't want to do the calculation by yourself, you could go for such a solution.
I, however, know that is a kinda costly solution, so you need to be aware of potential performance issues in runtime – depending on how frequently you intend to invoke this.
NSTimeInterval _timeInSeconds = 123456789.123; // or any other interval...;
NSCalendar *_calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
NSCalendarUnit _units = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *_components = [_calendar components:_units fromDate:[NSDate date] toDate:[NSDate dateWithTimeIntervalSinceNow:_timeInSeconds] options:kNilOptions];
NSLog(#"%ld Days, %ld Hours, %ld Minutes, %ld Seconds", _components.day, _components.hour, _components.minute, _components.second);
You can write your own function like this:
import UIKit
let miliseconds: Int = 24 * 3600 * 1000 + 3700 * 1000
// 1 day and 1 hour 1 minute 40 seconds
func convertTime(miliseconds: Int) -> String {
var seconds: Int = 0
var minutes: Int = 0
var hours: Int = 0
var days: Int = 0
var secondsTemp: Int = 0
var minutesTemp: Int = 0
var hoursTemp: Int = 0
if miliseconds < 1000 {
return ""
} else if miliseconds < 1000 * 60 {
seconds = miliseconds / 1000
return "\(seconds) seconds"
} else if miliseconds < 1000 * 60 * 60 {
secondsTemp = miliseconds / 1000
minutes = secondsTemp / 60
seconds = (miliseconds - minutes * 60 * 1000) / 1000
return "\(minutes) minutes, \(seconds) seconds"
} else if miliseconds < 1000 * 60 * 60 * 24 {
minutesTemp = miliseconds / 1000 / 60
hours = minutesTemp / 60
minutes = (miliseconds - hours * 60 * 60 * 1000) / 1000 / 60
seconds = (miliseconds - hours * 60 * 60 * 1000 - minutes * 60 * 1000) / 1000
return "\(hours) hours, \(minutes) minutes, \(seconds) seconds"
} else {
hoursTemp = miliseconds / 1000 / 60 / 60
days = hoursTemp / 24
hours = (miliseconds - days * 24 * 60 * 60 * 1000) / 1000 / 60 / 60
minutes = (miliseconds - days * 24 * 60 * 60 * 1000 - hours * 60 * 60 * 1000) / 1000 / 60
seconds = (miliseconds - days * 24 * 60 * 60 * 1000 - hours * 60 * 60 * 1000 - minutes * 60 * 1000) / 1000
return "\(days) days, \(hours) hours, \(minutes) minutes, \(seconds) seconds"
}
}
convertTime(miliseconds)
//result is "1 days, 1 hours, 1 minutes, 40 seconds"
try this
NSTimeInterval time = <timein ms>;
NSInteger days = time / (24 * 60 * 60);
NSInteger hours = (time / (60 * 60)) - (24 * days);
NSInteger minutes =(time / 60) - (24 * 60 * days) - (hours * 60);
NSInteger seconds = (lroundf(time) % 60);
I have write the easy code to do this in both Objective-c and Swift:
Swift
var milliseconds : double_t = 568569600;
milliseconds = floor(milliseconds/1000);
let seconds : double_t = fmod(milliseconds, 60);
let minutes : double_t = fmod((milliseconds / 60) , 60);
let hours : double_t = fmod((milliseconds / (60*60)), 60);
let days : double_t = fmod(milliseconds / ((60*60)*24), 24);
NSLog("seconds : %.f minutes : %.f hours : %.f days : %.f", seconds, minutes, hours, days);
Output - seconds : 9 minutes : 56 hours : 38 days : 7
Objective
double milliseconds = 568569600;
milliseconds = milliseconds/1000;
float seconds = fmod(milliseconds, 60);
float minutes = fmod((milliseconds / 60) , 60);
float hours = fmod((milliseconds / (60*60)), 60);
float days = fmod(milliseconds / ((60*60)*24), 24);
NSLog(#"seconds : %.f minutes : %.f hours : %.f days : %.f ", seconds, minutes, hours, days);
Output - seconds : 10 minutes : 56 hours : 38 days : 7

Ruby Program time conversion

The task is to Write a method that will take in a number of minutes, and returns a string that formats the number into hours:minutes.
here's what I have so far:
def time_conversion(minutes)
minutes = (minutes / 60) % 60
hours = minutes / (60 * 60)
format(" %02d:%02d ", hours, minutes)
return format
end
it's not working out for me
Try this
def time_conversion(time)
minutes = time % 60
hours = time / 60
minutes = (minutes < 10)? "0" + minutes.to_s : minutes.to_s
return hours.to_s + ":" + minutes
end
Using division in Ruby returns a whole number, lowered to the previous number. Using modulus returns the remainder after division.
Ruby's Numeric#divmod is exactly what you want here. It returns both the quotient and remainder of a division operation, so e.g. 66.divmod(60) returns [ 1, 6 ]. Combined with sprintf (or String#%, it makes for an extremely simple solution:
def time_conversion(minutes)
"%02d:%02d" % minutes.divmod(60)
end
puts time_conversion(192)
# => 03:12
Well try
h = minutes/60
M = minutes%60

How to customize my timer display to show only minutes and seconds?

My Timer is displaying Minutes and Hours, but once it gets to 60 minutes it restarts from 0 Minute.
Should I get rid of the modulo ( % 60 ) for minutes.
I would like my timer to display for ex: 80:45 ( basically not stopping at 60 min once it reaches 1 hour)
var min = 0
var sec = 0
func stringFromTimeInterval(interval: NSTimeInterval) -> String {
let interval = Int(interval)
let seconds = interval % 60
let minutes = (interval / 60) % 60
//let hours = (interval / 3600)// I don't need the hours
return String(format: "%02d:%02d",minutes, seconds)
}
% 60 means that it will spit out a minutes value that is the remainder when divided by 60(minutes). This is most probably because for time in the form hh:mm, you want it to go from 5:59 to 6:00, not 5:60. So changing the following line will give you what you seek.
let minutes = (interval / 60) % 60 -> let minutes = interval / 60

Resources