2019-05-06 07:31:26 -04:00
---
id: bd7158d8c443edefaeb5bdef
2021-02-06 04:42:36 +00:00
title: Timestamp Microservice
2019-05-06 07:31:26 -04:00
challengeType: 4
2020-09-18 00:11:18 +08:00
forumTopicId: 301508
2021-01-13 03:31:00 +01:00
dashedName: timestamp-microservice
2019-05-06 07:31:26 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
Build a full stack JavaScript app that is functionally similar to this: < https: / / timestamp-microservice . freecodecamp . rocks / > . Working on this project will involve you writing your code using one of the following methods:
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
- Clone [this GitHub repo ](https://github.com/freeCodeCamp/boilerplate-project-timestamp/ ) and complete your project locally.
- Use [our repl.it starter project ](https://repl.it/github/freeCodeCamp/boilerplate-project-timestamp ) to complete your project.
- Use a site builder of your choice to complete the project. Be sure to incorporate all the files from our GitHub repo.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
When you are done, make sure a working demo of your project is hosted somewhere public. Then submit the URL to it in the `Solution Link` field. Optionally, also submit a link to your projects source code in the `GitHub Link` field.
2020-12-16 00:37:30 -07:00
# --hints--
2021-02-06 04:42:36 +00:00
You should provide your own project, not the example URL.
```js
(getUserInput) => {
assert(
!/.*\/timestamp-microservice\.freecodecamp\.rocks/.test(getUserInput('url'))
);
};
```
A request to `/api/timestamp/: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
2020-12-16 00:37:30 -07:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/api/timestamp/2016-12-25').then(
(data) => {
assert.equal(
data.unix,
1482624000000,
'Should be a valid unix timestamp'
);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2021-02-06 04:42:36 +00:00
A request to `/api/timestamp/: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`
2019-05-06 07:31:26 -04:00
2020-12-16 00:37:30 -07:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/api/timestamp/2016-12-25').then(
(data) => {
assert.equal(
data.utc,
'Sun, 25 Dec 2016 00:00:00 GMT',
'Should be a valid UTC date string'
);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
2019-05-06 07:31:26 -04:00
```
2021-02-06 04:42:36 +00:00
A request to `/api/timestamp/1451001600000` should return `{ unix: 1451001600000, utc: "Fri, 25 Dec 2015 00:00:00 GMT" }`
```js
(getUserInput) =>
$.get(getUserInput('url') + '/api/timestamp/1451001600000').then(
(data) => {
assert(
data.unix === 1451001600000 & &
data.utc === 'Fri, 25 Dec 2015 00:00:00 GMT'
);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
Your project can handle dates that can be successfully parsed by `new Date(date_string)`
2020-12-16 00:37:30 -07:00
```js
(getUserInput) =>
2021-02-06 04:42:36 +00:00
$.get(getUserInput('url') + '/api/timestamp/05 October 2011').then(
2020-12-16 00:37:30 -07:00
(data) => {
2021-02-06 04:42:36 +00:00
assert(
data.unix === 1317772800000 & &
data.utc === 'Wed, 05 Oct 2011 00:00:00 GMT'
);
2020-12-16 00:37:30 -07:00
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2019-05-06 07:31:26 -04:00
2021-02-06 04:42:36 +00:00
If the input date string is invalid, the api returns an object having the structure `{ error : "Invalid Date" }`
2019-05-06 07:31:26 -04:00
2020-12-16 00:37:30 -07:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/api/timestamp/this-is-not-a-date').then(
(data) => {
assert.equal(data.error.toLowerCase(), 'invalid date');
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2019-05-06 07:31:26 -04:00
2021-02-06 04:42:36 +00:00
An empty date parameter should return the current time in a JSON object with a `unix` key
2019-05-06 07:31:26 -04:00
```js
2020-12-16 00:37:30 -07:00
(getUserInput) =>
$.get(getUserInput('url') + '/api/timestamp').then(
(data) => {
var now = Date.now();
assert.approximately(data.unix, now, 20000);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
2019-05-06 07:31:26 -04:00
```
2020-08-13 17:24:35 +02:00
2021-02-06 04:42:36 +00:00
An empty date parameter should return the current time in a JSON object with a `utc` key
2020-12-16 00:37:30 -07:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/api/timestamp').then(
(data) => {
var now = Date.now();
var serverTime = new Date(data.utc).getTime();
assert.approximately(serverTime, now, 20000);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
# --solutions--
2021-01-13 03:31:00 +01:00
```js
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
```