Back End Development and APIs Projects - Timestamp Microservice

Tell us what’s happening:
Describe your issue in detail here.

I do not understand the reason this is not working correctly. I did the tests by my own and it worked properly.

Your project link(s)

solution: boilerplate-project-timestamp - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36

Challenge: Back End Development and APIs Projects - Timestamp Microservice

Link to the challenge:

// index.js
// where your node app starts

// init project
var express = require('express');
var app = express();
let bodyParser = require('body-parser');

// enable CORS (https://en.wikipedia.org/wiki/Cross-origin_resource_sharing)
// so that your API is remotely testable by FCC 
var cors = require('cors');
app.use(cors({ optionsSuccessStatus: 200 }));  // some legacy browsers choke on 204

// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));

//it is to read different kind of information
app.use(bodyParser.urlencoded({ extended: false }));

// http://expressjs.com/en/starter/basic-routing.html
app.get("/", function(req, res) {
 res.sendFile(__dirname + '/views/index.html');
});


// your first API endpoint... 
app.get("/api/hello", function(req, res) {
 res.json({ greeting: 'hello API' });
});



app.get("/api", function(req, res) {
 let utcCurrentTime = new Date().toUTCString();
 let unixCurrentTime = Date.now();
 res.json({ unix: utcCurrentTime, utc: unixCurrentTime });
 //res.json({unix:utcCurrentTime})
});

//fourth endpoint


app.get("/api/:fecha", function(req, res) {

 let fecha = req.params.fecha;
 console.log(fecha);
 let date = new Date(fecha);

 if (/^\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$/.test(fecha)) {
   console.log("Bonaparte");
   //console.log(date);
   let fecha = req.params.fecha;
   let date = new Date(fecha);
   let timestampDate = new Date(date * 1000);
   console.log(timestampDate)
   let formattedDate = timestampDate.toUTCString();
   let gmtDateTime = new Date(date).toUTCString();
   let yymmddDate = date.getTime();
   let unixTimestamp = Math.floor(date.getTime())
   console.log({ unix: unixTimestamp, utc: gmtDateTime });
   res.json({ unix: unixTimestamp, utc: gmtDateTime });

 } else if (/^[0-9]*$/.test(fecha)) {

   console.log("Napoleon");
   let fecha = req.params.fecha;
   let date2 = new Date(fecha * 1);
   console.log(date2);
   let utcDate = date2.toUTCString();

   console.log({ unix: fecha, utc: utcDate });
   res.json({ unix: fecha, utc: utcDate });

 } else {
   //console.log("Venice");
   res.json({ error: "Invalid Date" });

 }

});



// listen for requests :)
var listener = app.listen(process.env.PORT, function() {
 console.log('Your app is listening on port ' + listener.address().port);
});
  1. Your unix value is the wrong data type in the /api/1451001600000 route.
Request URL: https://boilerplate-project-timestamp.aaronargotte.repl.co/api/1451001600000
Response: {"unix":"1451001600000","utc":"Fri, 25 Dec 2015 00:00:00 GMT"}
  1. You are not handling the valid date string correctly.
Request URL: https://boilerplate-project-timestamp.aaronargotte.repl.co/api/05%20October%202011,%20GMT
Response: {"error":"Invalid Date"}
  1. You switched the date unit values in the default route response.
Request URL: https://boilerplate-project-timestamp.aaronargotte.repl.co/api
Response: {"unix":"Wed, 27 Sep 2023 01:19:34 GMT","utc":1695777574399}
1 Like

I can pass all of the tests thanks to your suggestion, but I still cannot pass the “Your project can handle dates that can be successfully parsed by new Date(date_string)

// index.js
// where your node app starts

// init project
var express = require('express');
var app = express();
let bodyParser = require('body-parser');

// enable CORS (https://en.wikipedia.org/wiki/Cross-origin_resource_sharing)
// so that your API is remotely testable by FCC 
var cors = require('cors');
app.use(cors({ optionsSuccessStatus: 200 }));  // some legacy browsers choke on 204

// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));

//it is to read different kind of information
app.use(bodyParser.urlencoded({ extended: false }));

// http://expressjs.com/en/starter/basic-routing.html
app.get("/", function(req, res) {
  res.sendFile(__dirname + '/views/index.html');
});


// your first API endpoint... 
app.get("/api/hello", function(req, res) {
  res.json({ greeting: 'hello API' });
});



app.get("/api", function(req, res) {
  let utcCurrentTime = new Date().toUTCString();
  let unixCurrentTime = Date.now();
  res.json({ unix: unixCurrentTime, utc: utcCurrentTime });
  //res.json({unix:utcCurrentTime})
});

//fourth endpoint


app.get("/api/:fecha", function(req, res) {

  let fecha = req.params.fecha;
  console.log(fecha);
  let date = new Date(fecha);

  if (/^\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$/.test(fecha)) {
    console.log("Bonaparte");
    //console.log(date);
    let fecha = req.params.fecha;
    let date = new Date(fecha);
    let timestampDate = new Date(date * 1000);
    console.log(timestampDate)
    let formattedDate = timestampDate.toUTCString();
    let gmtDateTime = new Date(date).toUTCString();
    let yymmddDate = date.getTime();
    let unixTimestamp = Math.floor(date.getTime())
    let unixNumberTimestamp = Number(unixTimestamp);
    console.log(unixNumberTimestamp);
    console.log({ unix: unixTimestamp, utc: gmtDateTime });
    res.json({ unix: unixNumberTimestamp, utc: gmtDateTime });

  } else if (/^[0-9]*$/.test(fecha)) {

    console.log("Napoleon");
    let fecha = req.params.fecha;
    let fechaNumber = Number(fecha);
    let date2 = new Date(fecha * 1);
    console.log(date2);
    let utcDate = date2.toUTCString();

    console.log({ unix: fecha, utc: utcDate });
    res.json({ unix: fechaNumber, utc: utcDate });

  } else {
    //console.log("Venice");
    res.json({ error: "Invalid Date" });

  }

});



// listen for requests :)
var listener = app.listen(process.env.PORT, function() {
  console.log('Your app is listening on port ' + listener.address().port);
});

Test your regex with the example date string.


You do not need a regex, you can use what the Date constructor returns to check if it is a valid date or not. You can also use the Number constructor to check the type of date string (i.e. if it can be converted to a number or not).

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.