strftime( )
Now that you have created your dt
object, you may have noticed that this is a proprietary Datetime
object and, therefore, some methods may fail when applied to it. For that reason, datetime
provides some functions to transform the datetime
object into a string. This is particularly useful when you are in need of changing the formatting.
Tehtävä
Swipe to start coding
- Create a
Datetime
object with the current date and time; - Use the
strftime()
module to change the formatting in"%Y-%m-%d"
; - Print the type of the
dt
variable.
Ratkaisu
9
1
2
3
4
5
6
7
8
# Create a datetime object
dt = datetime.datetime.now()
# Use strftime to convert the datetime object to a string in the format "YYYY-MM-DD HH:MM:SS"
dt_string = dt.strftime("%Y-%m-%d")
print(dt_string)
print(type(dt_string))
Mark tasks as Completed
Oliko kaikki selvää?
Kiitos palautteestasi!
Osio 1. Luku 3
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme