Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Annotated Answers solution in Clear category for Date and Time Converter by BootzenKatzen
import time
def date_time(Time: str) -> str:
t = time.strptime(Time, "%d.%m.%Y %H:%M") #strptime is used to take your string and creat a datetime object
#based on the formating you provide (the %d stuff)
h = '' if t.tm_hour == 1 else 's' #this is to pluralize our hours/minutes when necessary
m = '' if t.tm_min == 1 else 's'
return time.strftime(f"{t.tm_mday} %B %Y year {t.tm_hour} hour{h} {t.tm_min} minute{m}", t)
# strftime turns the datetime object into a string based on your formatting
# the f at the beginning signifies "formatted string literals"
# it tells the program to look for the curly braces and replace them with the correct value
# the different "%_" stand for different formats
# for example %d is day of the month as a zero-padded decimal- 01, 02, etc.
# so {t.tm_mday} pulls the day from the datetime object without the extra 0 so in the example - 1
# %B is the full month name, %Y is the 4 digit year and the "year" following is just a part of the string
# that's being created, so it says "1 January 2000 year"
# {t.tm_hour} pulls the number of hours without the 0 padding then we add "hour" to the string
# and the {h} tells it to look at our h variable to see if hours should be plural
# tm_min does the same thing with the minutes
# the "t" at the end tells it to pull the %B and %Y from our datetime object instead of using today's date
from datetime import datetime
def checkio(time):
dt = datetime.strptime(time, '%d.%m.%Y %H:%M')
hour = 'hour' if dt.hour == 1 else 'hours'
minute = 'minute' if dt.minute == 1 else 'minutes'
return dt.strftime(f'%-d %B %Y year %-H {hour} %-M {minute}')
#This example just uses different "%_" formatting instead of using {t.tm_mday} to pull the value
# %-d pulls the day as an int without the 0 padding same for %-H and %-M
print("Example:")
print(date_time("01.01.2000 00:00"))
assert date_time("01.01.2000 00:00") == "1 January 2000 year 0 hours 0 minutes"
assert date_time("09.05.1945 06:07") == "9 May 1945 year 6 hours 7 minutes"
print("The mission is done! Click 'Check Solution' to earn rewards!")
April 5, 2023
Comments: