Get Started

Recreating ISO format with strftime()

In the last chapter, you used strftime() to create strings from date objects. Now that you know about datetime objects, let's practice doing something similar.

Re-create the .isoformat() method, using .strftime(), and print the first trip start in our data set.

Reference
%Y4 digit year (0000-9999)
%m2 digit month (1-12)
%d2 digit day (1-31)
%H2 digit hour (0-23)
%M2 digit minute (0-59)
%S2 digit second (0-59)

This is a part of the course

“Working with Dates and Times in Python”

View Course

Exercise instructions

  • Complete fmt to match the format of ISO 8601.
  • Print first_start with both .isoformat() and .strftime(); they should match.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import datetime
from datetime import datetime

# Pull out the start of the first trip
first_start = onebike_datetimes[0]['start']

# Format to feed to strftime()
fmt = "____"

# Print out date with .isoformat(), then with .strftime() to compare
print(first_start.isoformat())
print(____)

This exercise is part of the course

Working with Dates and Times in Python

IntermediateSkill Level
4.4+
36 reviews

Learn how to work with dates and times in Python.

Bike sharing programs have swept through cities around the world -- and luckily for us, every trip gets recorded! Working with all of the comings and goings of one bike in Washington, D.C., you'll practice working with dates and times together. You'll parse dates and times from text, analyze peak trip times, calculate ride durations, and more.

Exercise 1: Dates and timesExercise 2: Creating datetimes by handExercise 3: Counting events before and after noonExercise 4: Printing and parsing datetimesExercise 5: Turning strings into datetimesExercise 6: Parsing pairs of strings as datetimesExercise 7: Recreating ISO format with strftime()
Exercise 8: Unix timestampsExercise 9: Working with durationsExercise 10: Turning pairs of datetimes into durationsExercise 11: Average trip timeExercise 12: The long and the short of why time is hard

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free