For this project, you will build a small API that returns data about dates. Familiarity working with dates, Date
objects in JavaScript, and with date formatting libraries like Day.js is essential for a developer.
You'll need to create a new Express app from scratch for this project, beginning with installing express
as a dependency. You'll also need to install dayjs
as a dependency.
You'll need to read the Day.js documentation to learn how to use the library to make these endpoints work.
Your API should have the following endpoints:
GET /api/dates/today
: Returns the current date in the formatTuesday Jan 16, 2024
.GET /api/dates/tomorrow
: Returns the date of the next day in the formatWednesday Jan 17, 2024
.GET /api/dates/yesterday
: Returns the date of the previous day in the formatMonday Jan 15, 2024
.GET /api/day-of-week/:year/:month/:day
: Returns the day of the week for the date provided in the URL as a parameter, e.g./api/day-of-week/2024/1/16
.GET /api/current-time
: Returns the current time in the format19:20:30
. This endpoint should accept a query parameter calledformat
that can be used to change the format of the time. For example,/api/current-time?format=12
should return the time in the format7:20:30 PM
. The default format should be24
.GET /api/timestamp
: Returns the current timestamp in milliseconds. This endpoint should accept a query parameter calledformat
that can be used to change the format of the timestamp. For example,/api/timestamp?format=seconds
should return the timestamp in seconds. The default format should bemilliseconds
.
Each endpoint should return JSON data in the following format:
{
"date": "Tuesday Jan 16, 2024"
}
Successful responses should be sent with a 200
status code.
If a user attempts to access an endpoint that does not exist, your API should return a JSON response in the following format:
{
"error": "Not found"
}
The response should also have a status code of 404
.
- Return the current date in the format
2024-01-16
for thetoday
,tomorrow
, andyesterday
endpoints when a query pa. - Add an endpoint that returns the current date in the format
2024-01-16T19:20:30.000Z
. This is the format used by the JavaScriptDate
object and is called ISO 8601. - Add the ability to accept a query parameter called
timezone
to thecurrent-time
endpoint. Return the current date and time in the timezone provided. For example,/api/current-time?timezone=America/New_York
should return the current time in New York. You can use the Day.js timezone plugin to help with this. - Look through the Day.js library. What other features does it have? Add a new endpoint that uses one of these features.
- Day.js documentation If you open the console on this page, you can run the methods in the documentation to see what they return.
- Express documentation
- MDN JS Date object
- REST Client extension for VS Codium This makes it super easy to test your endpoints right in VS Codium.