Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

Random Dates

node v12.22.12
version: 1.0.0
endpointsharetweet
const START_DATE = new Date(2021, 01, 11); const END_DATE = new Date(); const MAX_DATES = 1500;
const dateFns = require('date-fns'); const randomNumber = (max) => Math.floor(Math.random() * max); const url = require('url'); const DAY_DIFF = dateFns.differenceInDays(END_DATE, START_DATE);
const getRandomDate = (dateFormat, startDate, dayDiff) => dateFns.format(dateFns.addDays(startDate, randomNumber(dayDiff)), dateFormat);
exports.endpoint = (req, res) => { const parsedUrl = url.parse(req.url, true); console.log(parsedUrl); const dateFormat = 'format' in parsedUrl.query ? parsedUrl.query.format : 'dd-MM-yyyy'; const list = Array.from({ length: MAX_DATES }, () => getRandomDate(dateFormat, START_DATE, DAY_DIFF)) res.end(list.join("\n")) }
Loading…

no comments

    sign in to comment