Dates Module #27 Flashcards

1
Q

What type is a Date in JS

A

It is an object representing a single point in time. It also handles time. It is initialized by calling new Date( ).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When new Date( ) is called what does it reference?

A

Without any parameters it represents the current date and time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How is the timestamp returned when you call Date.parse( )

A

It returns the time in milliseconds rather than a date object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The date can be created in JS 4 Ways, what are they?

A
  1. passing no parameters, creates a Date object that represents “now”
  2. passing a number, which represents the milliseconds from 1 Jan 1970 00:00 GMT
  3. passing a string, which represents a date
  4. passing a set of parameters, which represent the different parts of a date
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Specify a timezone by adding it how?

A

+HOURS format wrapped in parenthesis. Example below:

new Date('July 22, 2018 07:22:13 +0700')
new Date('July 22, 2018 07:22:13 (CET)')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

For reference here are all the methods to format a particular date.

A

date.toString()
// “Sun Jul 22 2018 07:22:13 GMT+0200 (Central European Summer Time)”
date.toTimeString() //”07:22:13 GMT+0200 (Central European Summer Time)”
date.toUTCString() //”Sun, 22 Jul 2018 05:22:13 GMT”
date.toDateString() //”Sun Jul 22 2018”
date.toISOString() //”2018-07-22T05:22:13.000Z” (ISO 8601 format)
date.toLocaleString() //”22/07/2018, 07:22:13”
date.toLocaleTimeString() //”07:22:13”

You are not limited to those, of course - you can use more low level methods to get a value out of a date, and construct any kind of result you want:

date. getDate() //22
date. getDay() //0 (0 means sunday, 1 means monday..)
date. getFullYear() //2018
date. getMonth() //6 (starts from 0)
date. getHours() //7
date. getMinutes() //22
date. getSeconds() //13
date. getMilliseconds() //0 (not specified)
date. getTime() //1532236933000
date. getTimezoneOffset() //-120 (will vary depending on where you are and when you check - this is CET during the summer). Returns the timezone difference expressed in minutes

Those all depend on the current timezone of the computer. There are equivalent UTC versions of these methods, that return the UTC value rather than the values adapted to your current timezone:

date. getUTCDate() //22
date. getUTCDay() //0 (0 means sunday, 1 means monday..)
date. getUTCFullYear() //2018
date. getUTCMonth() //6 (starts from 0)
date. getUTCHours() //5 (not 7 like above)
date. getUTCMinutes() //22
date. getUTCSeconds() //13
date. getUTCMilliseconds() //0 (not specified)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

There are almost as many ways to edit a date

A

A Date object offers several methods to edit a date value:

const date = new Date(‘July 22, 2018 07:22:13’)

date. setDate(newValue)
date. setFullYear(newValue) //note: avoid setYear(), it’s deprecated
date. setMonth(newValue)
date. setHours(newValue)
date. setMinutes(newValue)
date. setSeconds(newValue)
date. setMilliseconds(newValue)
date. setTime(newValue)

setDate and setMonth start numbering from 0, so for example March is month 2.

Example:

const date = new Date(‘July 22, 2018 07:22:13’)

date.setDate(newValue) //July 23, 2018 07:22:13
date.setFullYear(newValue) //note: avoid setYear(), it’s deprecated
date.setMonth(newValue)
date.setHours(newValue)
date.setMinutes(newValue)
date.setSeconds(newValue)
date.setMilliseconds(newValue)
date.setTime(newValue)
Fun fact: those methods “overlap”, so if you, for example, set date.setHours(48), it will increment the day as well.

Good to know: you can add more than one parameter to setHours() to also set minutes, seconds and milliseconds: setHours(0, 0, 0, 0) - the same applies to setMinutes and setSeconds.

As for get, also set methods have an UTC equivalent:

const date = new Date(‘July 22, 2018 07:22:13’)

date. setUTCDate(newValue)
date. setUTCDay(newValue)
date. setUTCFullYear(newValue)
date. setUTCMonth(newValue)
date. setUTCHours(newValue)
date. setUTCMinutes(newValue)
date. setUTCSeconds(newValue)
date. setUTCMilliseconds(newValue)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly