UTC()
Returns the milliseconds since 1st of January, 1970 till the date specified. This method
gets two required arguments of year and month and five other optional arguments: day, hour,
minute, second, and millisecond.
[Link]([Link](1980, 2)) // 320716800000
[Link]([Link](2050, 10, 20, 7, 23, 42, 762)) // 2552541822762
Setters
These methods are used for setting the date properties:
setFullYear()
Sets the year of a date object (negative numbers allowed):
[Link](2025)
[Link](d) // 2025-07-22T21:11:46.672Z
[Link](-135)
[Link](d) // -000135-07-22T22:42:16.055Z
This method also accepts two other arguments being month and day to be set. The month
argument accepts numbers from 0 to 11 representing January to December; other numbers will
also be accepted. Such as -1, which represents the last month of the previous year, and 12
represents the first month of the next year. Day argument accepts numbers from 1 to 31, and
respectively 0 represents the last day of the previous month while 32 is the first day of the next
month.
[Link](2025, 5)
[Link](d) // 2025-06-22T21:15:22.151Z
If we set the month to -2, it will show the November of 2024:
[Link](2025, -2)
[Link](d) // 2024-11-22T22:16:02.821Z
And here is an example of the day argument:
[Link](2025, 4, 5)
[Link](d) // 2025-05-04T21:16:35.262Z
And a negative number for the day argument:
166
[Link](2025, 4, -3)
[Link](d) // 2025-04-26T21:16:50.865Z
setMonth()
Sets the month of a date object just like the month argument of the previous method:
[Link](3)
[Link](d) // 2020-04-22T21:17:47.376Z
And a negative number as the month argument:
[Link](-5)
[Link](d) // 2019-08-22T21:18:08.217Z
This method accepts a second argument to set the day, just like the day argument of the previous
method:
[Link](2, -2)
[Link](d) // 2020-02-26T22:19:17.660Z
setDate()
Sets the day of a date object just like the day argument of two previous methods:
[Link](28)
[Link](d) // 2020-07-27T21:21:14.070Z
setHours()
Sets the hour of a date object from number 0 to 23. Other numbers are accepted, while -1
represents the last hour of the previous day and 24 representing the first hour of the next day.
[Link](5)
[Link](d) // 2020-07-23T01:23:09.752Z
And a negative number:
[Link](-5)
[Link](d) // 2020-07-22T15:23:31.361Z
This method supports three other arguments: minute, second, and millisecond. The minute
argument can be from 0 to 59, while -1 represents the last minute of the previous hour and 60 the
first minute of the next hour. The second and millisecond arguments can be from 0 to 59 and
from 0 to 999, respectively, while negative numbers and numbers beyond that range represent
previous and next units.
167
[Link](5, 3, 1, 8)
[Link](d) // 2020-07-23T00:33:01.008Z
[Link](5, 8, 90, 8)
[Link](d) // 2020-07-23T00:39:30.008Z
[Link](5, 8, 90, -12)
[Link](d) // 2020-07-23T00:39:29.988Z
setMinutes()
Sets the minute of a date object just like the minute argument of the previous method:
[Link](5)
[Link](d) // 2020-07-22T20:35:29.576Z
This method accepts two other arguments: second and millisecond, just like the previous method.
[Link](5, 4)
[Link](d) // 2020-07-22T21:35:04.679Z
[Link](5, 4, -7)
[Link](d) // 2020-07-22T21:35:03.993Z
setSeconds()
Sets the second of a date object just like the second argument of the previous method:
[Link](9)
[Link](d) // 2020-07-22T21:35:09.023Z
[Link](-20)
[Link](d) // 2020-07-22T21:34:40.348Z
This method accepts a second argument to set the millisecond of a date object just like the
millisecond date of the previous method:
[Link](30, -30)
[Link](d) // 2020-07-22T21:36:29.970Z
setMilliseconds()
Sets the millisecond of a date object:
[Link](570)
[Link](d) // 2020-07-22T21:36:56.570Z
[Link](-700)
[Link](d) // 2020-07-22T21:37:20.300Z
168
setTime()
Set a date to a specifc number of milliseconds after or before 1st of January, 1970:
[Link](0)
[Link](d) // 1970-01-01T00:00:00.000Z
[Link](81241278651)
[Link](d) // 1972-07-29T07:01:18.651Z
[Link](6247867815592)
[Link](d) // 2167-12-27T06:50:15.592Z
[Link](-1)
[Link](d) // 1969-12-31T23:59:59.999Z
[Link](-789129781252)
[Link](d) // 1944-12-29T13:16:58.748Z
setUTCFullYear()
Exactly like setFullYear(), but based on UTC.
[Link](2070)
[Link](d) // 2070-07-22T21:39:27.824Z
[Link](2070, 7, 8, 2)
[Link](d) // 2070-08-08T21:39:41.617Z
setUTCMonth()
Exactly like setMonth(), but based on UTC.
[Link](10)
[Link](d) // 2020-11-22T21:40:47.199Z
[Link](15, -4)
[Link](d) // 2021-03-27T21:41:08.630Z
setUTCDate()
Exactly like setDate(), but baed on UTC.
[Link](10)
[Link](d) // 2020-07-10T21:44:31.111Z
setUTCHours()
Exactly like setHours(), but based on UTC.
[Link](20)
[Link](d) // 2020-07-22T20:44:00.463Z
169
[Link](20, 8, 10, -5)
[Link](d) // 2020-07-22T20:08:09.995Z
setUTCMinutes()
Exactly like setMinutes(), but based on UTC.
[Link](40)
[Link](d) // 2020-07-22T21:40:17.271Z
[Link](40, 7, -500)
[Link](d) // 2020-07-22T21:40:06.500Z
setUTCSeconds()
Exactly like setSeconds(), but based on UTC.
[Link](30)
[Link](d) // 2020-07-22T21:46:30.983Z
[Link](30, 452)
[Link](d) // 2020-07-22T21:46:30.452Z
setUTCMilliseconds()
Exactly like setMilliseconds(), but based on UTC.
[Link](-230)
[Link](d) // 2020-07-22T21:47:34.770Z
Conversions
There are many ways to retrieve a date after setting their values or leaving the current
date to be:
toString():
[Link]([Link]()) // Thu Jul 23 2020 02:37:39 GMT+0430 (Iran Daylight Tim
e)
toDateString():
[Link]([Link]()) // Thu Jul 23 2020
toTimeString():
[Link]([Link]()) // 02:38:15 GMT+0430 (Iran Daylight Time)
toUTCString():
[Link]([Link]()) // Wed, 22 Jul 2020 22:08:37 GMT
170
toISOString()
[Link]([Link]()) // 2020-07-22T22:03:14.131Z
toJSON():
[Link]([Link]()) // 2020-07-22T22:04:14.477Z
toLocaleString():
[Link]([Link]()) // 7/23/2020, 2:37:10 AM
toLocaleDateString():
[Link]([Link]()) // 7/23/2020
toLocaleTimeString():
[Link]([Link]()) // 2:36:26 AM
171
Chapter 13: NPM
As mentioned before, programming sometimes requires you to use the codes written by
others. Reinventing the wheel is something that no one likes, especially programmers! As you’ve
read earlier about code splitting and exporting or importing modules, it is possible to use the
functionality of a different file inside another file, which gives you the power to organize your
codes. [Link] supports a way to handle packages that get installed using a special tool called
npm that stands for Node Package Manager. By using npm, you can define your project and
also its dependencies. In this chapter, we’ll look into some most useful commands this tool
provides us with.
To start a [Link] project, you need to run the command below in the root of your
project:
$ npm init
This command walks you through some steps in order to get your project read and creates a file
called [Link], which holds the information you provided. This file also registers the
dependencies needed for your project. A [Link] file looks like this (not exactly):
{
"type": "module",
"name": "nodejs-essentials",
"version": "1.0.0",
"description": "A good way to start learning Nodejs",
"main": "[Link]",
"author": "Adnan Babakan",
"license": "MIT"
}
As the file extension indicates, this is a JSON formatted file.
172
npm install
npm install is the command you are going to use the most. This command installs a
specific package from the npm repository. You can search for npm packages using Google or
by going to [Link]
Here is how the command looks like:
$ npm install {package_name}
There is a package called random available at [Link] which
helps us to create random numbers. To install this package, you simply need to run the command
below:
$ npm install random
There is also a short way to install packages:
$ npm i random
After the process is finished, you’ll see a new folder called node_modules in your project's root
directory. This directory is where your packages are. Now that you have installed this package,
you can use it by importing it. Importing a package requires its name only and not the path to its
file, like when it is needed for self-written files.
const random = require('random')
If you run the command below, along with installing the package, it gets added to your
[Link]:
$ npm i random --save
Tip: Anything prefixed with a dash (-) or double dash (--) is called a flag.
After running this command, your [Link] should look like this:
{
"type": "module",
"name": "nodejs-essentials",
"version": "1.0.0",
"description": "A good way to start learning Nodejs",
173
"main": "[Link]",
"author": "Adnan Babakan",
"license": "MIT",
"dependencies": {
"random": "^2.2.0"
}
}
If your package is not used in production is only meant for development you can install it as a
dev dependency using the --save-dev flag:
$ npm i random --save-dev
Which makes this package a dev dependency and registers it as so:
{
"type": "module",
"name": "nodejs-essentials",
"version": "1.0.0",
"description": "A good way to start learning Nodejs",
"main": "[Link]",
"author": "Adnan Babakan",
"license": "MIT",
"devDependencies": {
"random": "^2.2.0"
}
}
If you are asking what’s the reason to submit dependencies in [Link], here is the answer:
The node_modules folder can be hefty, and you should not ship it with your application (neither
on the server nor to another computer). Instead, you’ll leave your node_modules folder, and
when your application is in a new environment, what you need to do is running this command:
$ npm i
What this command does is pretty simple! It installs all the dependencies mentioned in the
[Link]. If you only want to install the production dependencies (exclude the dev
dependencies), you should use the --production flag:
$ npm i --production
If you only want to install dev dependencies, you can do so by the command below:
174
$ npm i --only=dev
Npm is not limited to its repository and can install packages from GitHub or even from a tarball
file (.tgz):
GitHub samples:
$ npm i user/repo
$ npm i user/repo#branch
You can install from GitLab as well:
$ npm i gitlab:user/repo#branch
Tarball file samples:
$ npm i ./[Link]
$ npm i [Link]
Versioning
Npm uses a specif way to version packages, and you can use it to install a specific
version of the package you need by indicating the version after the @ symbol.
This command installs the latest version:
$ npm i {package_name}@latest
A number can be specified as well:
$ npm i {package_name}@2.0.0
A version range is also supported:
$ npm i {package_name}@”2.3.0 – 2.3.4”
A more advanced range indicator is also possible to use:
$ npm i {package_name}@”>=2 <3”
175