2

New to programming so really any help is good help haha. So my question is if I take in a number ex. 738218 (any number will do) How can I convert it to hours, minutes, seconds, (ex. 19 hours, 36 minutes, 26 seconds). I have tried some examples I have found but I keep getting errors. This is what I have right now.

from datetime import timedelta
import time
sec = input('Enter a number to covert to a formal timestap: ')
hours = sec/12 `enter code here`
minutes = sec/60
time.strftime('%H:%M:%S', time.gmtime(sec))
print(time.strtime)

2 Answers2

1

should be:

import time
from datetime import datetime
t = time.time()
d = datetime.fromtimestamp(t)
print d.hour, d.minute, d.second
ellipse42
  • 100
  • 3
1

Welcome to StackOverflow! I've updated your script and added some notes:

seconds = int(input('Enter a number to convert to a formal timestamp: '))
hours = seconds // 3600  # there are 3600 seconds per hour
seconds -= hours * 3600  # don't double count seconds counted by hours
minutes = seconds // 60  # there are 60 seconds per minute
seconds -= minutes * 60  # don't double count seconds counted by minutes
time_string = "{}:{}:{}".format(hours, minutes, seconds)
print(time_string)

going through some of the lines individually:

seconds = int(input('Enter a number to convert to a formal timestamp: '))

I assume here that you are working in Python 3. If so, input is exactly what we want. If you are in Python 2, however, input does something slightly related and what we really want is something else - raw_input. raw_input was renamed input when the move was made from 2 to 3. Either way, we pass whatever is input by the user to the int function and convert it to an integer value.

time_string = "{}:{}:{}".format(hours, minutes, seconds)

"{}:{}:{}" is a format string. You can read more about them in the Python docs. The pairs of curly brackets are each placeholders for the arguments passed to the format method; the first brackets get replaced with the first argument hours, the second pair gets replaced with the second argument minutes, and so on.

If you are in Python 2, the code won't quite work as specified. Luckily, most of them can be backported to Python 2 by importing from the __future__ module; the only manual change would be switching input to raw_input in this case. Here's the same code, in Python 2:

from __future__ import print_function, division

seconds = int(raw_input('Enter a number to convert to a formal timestamp: '))
hours = seconds // 3600  # there are 3600 seconds per hour
seconds -= hours * 3600  # don't double count seconds counted by hours
minutes = seconds // 60  # there are 60 seconds per minute
seconds -= minutes * 60  # don't double count seconds counted by minutes
time_string = "{}:{}:{}".format(hours, minutes, seconds)
print(time_string)
chucksmash
  • 5,777
  • 1
  • 32
  • 41
  • Hey thanks alot! This does clear it up for me! and yes to clear up I am using Python 3. Cheer mate! much appreciated! –  Sep 12 '15 at 17:00