![]() I came up with the following: time rawinput(). Note that the total_seconds() method was introduced in Python 2.7, so if you want that on an earlier version, you'll need to calculate it yourself from. Without using any libraries, I'm trying to solve the Hackerrank problem 'Time Conversion', the problem statement of which is copied below. Now, to convert the seconds difference from a timedelta into hours, just divide by 3600: > hours_difference = abs(start_time - end_time).total_seconds() / 3600.0 ![]() Here's an example that outputs seconds obviously you can divide by 60 to get minutes. timestamp time.strftime('l:M:S') However, this prints the format in a 12-hour format. The simple solution is to write your own code to parse the string and perform the arithmetic. Currently I'm creating timestamps with the Python time module. Subtracting one datetime from another gives you a timedelta object, and you can use abs to ensure the time difference is positive: > start_time - end_time The strptime function (in its various incarnations) isn't designed to perform this sort of task, it's just for parsing valid time / date strings. Let's assume that you have your two dates and times as datetime objects (see datetime.datetime): > import datetime
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |