JavaScript Object Notation (JSON), a JavaScript-like data syntax
used quite frequently, does not allow trailing commas.
Dates
Dates and times in JavaScript are represented by the built-in
Date
object.
Date
is one
of the more problematic aspects of the language. Originally a direct port from Java
(one of the few areas in which JavaScript actually has any direct relationship to Java),
the
Date
object can be difficult to work with, especially if you are dealing with dates
in different time zones.
To create a date that’s initialized to the current date and time, use
new Date()
:
const
now
=
new
Date
();
now
;
// example: Thu Aug 20 2015 18:31:26 GMT-0700 (Pacific Daylight Time)
To create a date that’s initialized to a specific date (at 12:00 a.m.):
const
halloween
=
new
Date
(
2016
,
9
,
31
);
// note that months are
// zero-based: 9=October
To create a date that’s initialized to a specific date and time:
const
halloweenParty
=
new
Date
(
2016
,
9
,
31
,
19
,
0
);
// 19:00 = 7:00 pm
Once you have a date object, you can retrieve its components:
halloweenParty
.
getFullYear
();
// 2016
halloweenParty
.
getMonth
();
// 9
halloweenParty
.
getDate
();
// 31
halloweenParty
.
getDay
();
// 1 (Mon; 0=Sun, 1=Mon,...)
halloweenParty
.
getHours
();
// 19
halloweenParty
.
getMinutes
();
// 0
halloweenParty
.
getSeconds
();
// 0
halloweenParty
.
getMilliseconds
();
// 0
We will cover dates in detail in
.
Regular Expressions
A regular expression (or regex or regexp) is something of a sublanguage of JavaScript.
It is a common language extension offered by many different programming lan‐
guages, and it represents a compact way to perform complex search and replace oper‐
ations on strings. Regular expressions will be covered in
. Regular
expressions in JavaScript are represented by the
RegExp
object, and they have a literal
syntax consisting of symbols between a pair of forward slashes. Here are some exam‐
ples (which will look like gibberish if you’ve never seen a regex before):
Dates | 51