last modified July 7, 2020
The faker.js tutorial shows how to generate fake data in JavaScript with faker.js library.
The above method makes use of the JavaScript date object to change the date time to different formats. You can use a library like Moment.js to make date time format change and conversions easier jay. Useful, free online tool that generates random calendar dates. No ads, nonsense or garbage, just a random date generator.
faker.js
faker.js is a JavaScript library for generating fake data. Fake data is useful when building and testing our application. The faker.js can generate fake data for various areas, including address, commerce,company, date, finance, image, random, or name.
In this tutorial we work with faker.js in a Node application.
Setting up faker.js
First, we install faker.js.
![Random Random](https://flaviocopes.com/how-to-get-timestamp-javascript/banner.png)
We use Node version 11.5.0.
We initiate a new Node application.
We install faker.js module with
nmp i faker
.Faking names
In the first example, we fake data related to user names.
Random Date Scheduler
names.js
![Random Random](https://exceljet.net/sites/default/files/styles/function_screen/public/images/formulas/random date between two dates.png?itok=-p4ebjQw)
The example creates a random first name, last name, job title, name prefix and suffix, job area, and phone number.
We require the faker module.
We generate a fake first name with the
firstName()
function.The function is located in the name object.Random Date Selector
This is a sample output.
Faking dates
In the second example, we generate fake dates.
The example picks up a future and recent date and some weekday.
This is a sample output.
Faking random values
The faker allows to generate random values, such as integers, uuids, or words.
random_values.js
Random Date Jsu
The example generates random number, uuid, word, and a group of six words.
This is a sample output. Quickkey 2 1 – text expansion.
Faking locale data
The faker supports localized data to some extent. Note that the locales are finishedto various levels.
The example generates fake data in Russian language.
This is a sample output.
Serving fake data with JSON Server
In the next example, we generate JSON data and write it to file. The file is served by JSON Server. Clearview 1 7 3 – tabbed style ebook reader.
We install
json-server
Sandvox 2 9 5 download free. module.generate_users.js
The example generates one-hundred users and writes them to a
data.json
file.We start the JSON Server. The server serves data from the generated JSON file.
We retrieve the user with Id 3 with the
curl
tool.We show how to generate a HTTP GET request in JavaScript with the Node
request
module.We install the module.
The program fetches data from the JSON Server, beginning with index 4 (exclusive)and ending with index 8 (inclusive).
This is a sample output.
Faker CLI
The
faker-cli
is a Node module, which is a wrapper over the faker.js
.It allows to generate fake data from a command line tool.We install the
faker-cli
module. We generate some fake data with the
faker-cli
program.In this tutorial, we have used faker.js to generate fake data in JavaScript.
List all JavaScript tutorials.
JavaScript Code:
The above command will return current timestamp or epoch time or Unix timestamp in seconds (Total seconds since 1970/01/01).
- Read this =>JavaScript ForEach Method
More Examples:
Get Timestamp in Milliseconds in JavaScript
The following JavaScript code will return current timestamp in milliseconds (Total milliseconds since 1970/01/01) and store in timeStamp variable.Get Timestamp in Seconds in JavaScript
The following JavaScript code will return current timestamp in seconds (Total seconds since 1970/01/01) and store in timeStamp variable.Get Date/Time in UTC in JavaScript
The following JavaScript code will create an object of Date and then get current date/time in UTC in human readable for.var dt = new date();var utcdate = dt.toutcstring();//print resultsconsole.log(utcdate);24varutcdate=dt.toutcstring();//print resultsthe output will be as following: