2018-10-25 20:29:56 +02:00
---
id: bd7158d8c443edefaeb5bdef
title: Timestamp Microservice
challengeType: 4
2019-08-05 09:17:33 -07:00
forumTopicId: 301508
2021-01-13 03:31:00 +01:00
dashedName: timestamp-microservice
2018-10-25 20:29:56 +02:00
---
2020-11-27 19:02:05 +01:00
# --description--
2020-11-10 17:53:57 -06:00
2020-11-27 19:02:05 +01: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:
- Clone [this GitHub repo ](https://github.com/freeCodeCamp/boilerplate-project-timestamp/ ) and complete your project locally.
2021-04-29 06:13:38 -04:00
- Use [our Replit starter project ](https://replit.com/github/freeCodeCamp/boilerplate-project-timestamp ) to complete your project.
2020-11-27 19:02:05 +01:00
- Use a site builder of your choice to complete the project. Be sure to incorporate all the files from our GitHub repo.
2020-11-10 17:53:57 -06:00
2021-11-24 23:02:34 +05:30
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 project's source code in the `GitHub Link` field.
2018-10-25 20:29:56 +02:00
2021-12-01 20:17:18 +10:00
**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.
2020-11-27 19:02:05 +01:00
# --hints--
You should provide your own project, not the example URL.
```js
(getUserInput) => {
assert(
!/.*\/timestamp-microservice\.freecodecamp\.rocks/.test(getUserInput('url'))
);
};
```
2022-03-07 00:12:25 -05:00
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)
2020-11-27 19:02:05 +01:00
```js
(getUserInput) =>
2021-04-13 18:06:02 +09:00
$.get(getUserInput('url') + '/api/2016-12-25').then(
2020-11-27 19:02:05 +01:00
(data) => {
assert.equal(
data.unix,
1482624000000,
'Should be a valid unix timestamp'
);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
2018-10-25 20:29:56 +02:00
```
2021-04-13 18:06:02 +09:00
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`
2018-10-25 20:29:56 +02:00
2020-11-27 19:02:05 +01:00
```js
(getUserInput) =>
2021-04-13 18:06:02 +09:00
$.get(getUserInput('url') + '/api/2016-12-25').then(
2020-11-27 19:02:05 +01:00
(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);
}
);
```
2018-10-25 20:29:56 +02:00
2021-04-13 18:06:02 +09:00
A request to `/api/1451001600000` should return `{ unix: 1451001600000, utc: "Fri, 25 Dec 2015 00:00:00 GMT" }`
2020-11-27 19:02:05 +01:00
```js
(getUserInput) =>
2021-04-13 18:06:02 +09:00
$.get(getUserInput('url') + '/api/1451001600000').then(
2020-11-27 19:02:05 +01:00
(data) => {
assert(
data.unix === 1451001600000 & &
data.utc === 'Fri, 25 Dec 2015 00:00:00 GMT'
);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2018-10-25 20:29:56 +02:00
2020-11-27 19:02:05 +01:00
Your project can handle dates that can be successfully parsed by `new Date(date_string)`
```js
(getUserInput) =>
2021-12-01 20:17:18 +10:00
$.get(getUserInput('url') + '/api/05 October 2011, GMT').then(
2020-11-27 19:02:05 +01:00
(data) => {
assert(
data.unix === 1317772800000 & &
data.utc === 'Wed, 05 Oct 2011 00:00:00 GMT'
);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
If the input date string is invalid, the api returns an object having the structure `{ error : "Invalid Date" }`
```js
(getUserInput) =>
2021-04-13 18:06:02 +09:00
$.get(getUserInput('url') + '/api/this-is-not-a-date').then(
2020-11-27 19:02:05 +01:00
(data) => {
assert.equal(data.error.toLowerCase(), 'invalid date');
},
(xhr) => {
2021-06-05 03:13:18 -05:00
assert(xhr.responseJSON.error.toLowerCase() === 'invalid date');
2020-11-27 19:02:05 +01:00
}
);
```
An empty date parameter should return the current time in a JSON object with a `unix` key
```js
(getUserInput) =>
2021-04-13 18:06:02 +09:00
$.get(getUserInput('url') + '/api').then(
2020-11-27 19:02:05 +01:00
(data) => {
var now = Date.now();
assert.approximately(data.unix, now, 20000);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
An empty date parameter should return the current time in a JSON object with a `utc` key
```js
(getUserInput) =>
2021-04-13 18:06:02 +09:00
$.get(getUserInput('url') + '/api').then(
2020-11-27 19:02:05 +01:00
(data) => {
var now = Date.now();
var serverTime = new Date(data.utc).getTime();
assert.approximately(serverTime, now, 20000);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
# --solutions--
2018-10-25 20:29:56 +02:00
```js
2019-10-24 10:08:13 +05:30
/**
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.
*/
2018-10-25 20:29:56 +02:00
```