curriculum/challenges/english/blocks/back-end-development-and-apis-projects/bd7158d8c443edefaeb5bdef.md
Build a full-stack JavaScript app that is functionally similar to this: <a href="https://timestamp-microservice.freecodecamp.rocks" target="_blank" rel="noopener noreferrer nofollow">https://timestamp-microservice.freecodecamp.rocks</a>. Working on this project will involve you writing your code using one of the following methods:
Note: Time zones conversion is not a purpose of this project, so assume all sent valid dates will be parsed with new Date() as GMT dates.
You should provide your own project, not the example URL.
assert(
!/.*\/timestamp-microservice\.freecodecamp\.rocks/.test(code)
);
A request to /api/:date? with a valid date should return a JSON object with a unix key that is a Unix timestamp of the input date in milliseconds (as type Number)
const response = await fetch(code + '/api/2016-12-25');
if (!response.ok) {
throw new Error(await response.text());
}
const data = await response.json();
assert.equal(
data.unix,
1482624000000,
'Should be a valid unix timestamp'
);
A request to /api/:date? with a valid date should return a JSON object with a utc key that is a string of the input date in the format: Thu, 01 Jan 1970 00:00:00 GMT
const response = await fetch(code + '/api/2016-12-25');
if (!response.ok) {
throw new Error(await response.text());
}
const data = await response.json();
assert.equal(
data.utc,
'Sun, 25 Dec 2016 00:00:00 GMT',
'Should be a valid UTC date string'
);
A request to /api/1451001600000 should return { unix: 1451001600000, utc: "Fri, 25 Dec 2015 00:00:00 GMT" }
const response = await fetch(code + '/api/1451001600000');
if (!response.ok) {
throw new Error(await response.text());
}
const data = await response.json();
assert(
data.unix === 1451001600000 &&
data.utc === 'Fri, 25 Dec 2015 00:00:00 GMT'
);
Your project can handle dates that can be successfully parsed by new Date(date_string)
const response = await fetch(code + '/api/05 October 2011, GMT');
if (!response.ok) {
throw new Error(await response.text());
}
const data = await response.json();
assert(
data.unix === 1317772800000 &&
data.utc === 'Wed, 05 Oct 2011 00:00:00 GMT'
);
If the input date string is invalid, the API returns an object having the structure { error : "Invalid Date" }
const response = await fetch(code + '/api/this-is-not-a-date');
if (response.ok) {
const data = await response.json();
assert.equal(data.error.toLowerCase(), 'invalid date');
} else {
const errorData = await response.json();
assert(errorData.error.toLowerCase() === 'invalid date');
}
An empty date parameter should return the current time in a JSON object with a unix key
const response = await fetch(code + '/api');
if (!response.ok) {
throw new Error(await response.text());
}
const data = await response.json();
var now = Date.now();
assert.approximately(data.unix, now, 20000);
An empty date parameter should return the current time in a JSON object with a utc key
const response = await fetch(code + '/api');
if (!response.ok) {
throw new Error(await response.text());
}
const data = await response.json();
var now = Date.now();
var serverTime = new Date(data.utc).getTime();
assert.approximately(serverTime, now, 20000);