Dates Flashcards

1
Q

Return today’s date and time in ms since 1970

A

Date.now()
or from a date object
Date.prototype.getTime()

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

Return any date and time in ms since 1970

A

Date.parse(‘2021, oct 11’)

or

new Date(‘2021, oct 11’).getTime()

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

Return a date object with today’s date/time OR any date/time

A

For now:
new Date()
For any date:
new Date(‘December 25, 2012’)

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

Return a date object with today’s date

A

new Date()

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

Return today’s date/time/etc as a string

A

Date()

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

Return a number representing the year minus 1900

A

Get date object
Do getyear method
Date.prototype.getYear()

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

Return the year as a number

A

Date.prototype.getFullYear()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
Return the day of the week as a number
Sunday=0
Monday= 1
...
Saturday = 6
A

Date.prototype.getDate()

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

Create a function that returns the day of the week from an given date

A
Return the date of the week as a string:
Get day of week:
> let date = new Date('December 25, 2012')
> date.getDay()
= 2    // a tuesday

let dayOfWeek = [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday]

Use these numbers as indices on an array with words for the days of the week.

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

What is returned from each of these
Date()

Date.now()
Date.parse(‘2021, oct 11’)

new Date()
new Date('December 25, 2012')
new Date().getYear()
new Date().getFullYear()
new Date().getDate()
A

Date() - A string with date, time, and time zone info for right now:
Mon Jan 17 2022 20:32:57 GMT-0500 (Eastern Standard Time)

Date.now() - date and time in ms fromJanuary 1, 1970 till right now
Date.parse(‘2021, oct 11’) - date and time in ms fromJanuary 1, 1970 till the entered time

new Date() - creates a date object (purple text) of the date and time right now
2022-01-18T01:35:26.880Z
new Date('December 25, 2012') - creates a date object (purple text) of the date and time of the given date
new Date().getYear() - from a date object, return the year as a number minus 1900
new Date().getFullYear() - from a date object, return the year as a number (in years since year 0)
new Date().getDate() - from a date object, return the day of the month as a number. Returns NaN when the day is 0 or greater than the number of days the month should have
new Date().getDay() - from a date object, return the day of the week as a number.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does
Date()
do?

A

Date() - A string with date, time, and time zone info for right now:
Mon Jan 17 2022 20:32:57 GMT-0500 (Eastern Standard Time)

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

What does
Date.now()
do or return?

A

Date.now() - returns date and time in ms fromJanuary 1, 1970 till right now

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

What does
Date.parse(‘2021, oct 11’)
do?

A

Date.parse(‘2021, oct 11’) - date and time in ms fromJanuary 1, 1970 till the entered time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
What does 
new Date()
do?
A
new Date() - creates a date object (purple text) of the date and time (date seems to be wrong) right now
2022-01-18T01:35:26.880Z
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
What does 
new Date('December 25, 2012')
do?
A

new Date(‘December 25, 2012’) - creates a date object (purple text) of the date and time (date seems to be wrong) of the given date

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
What does 
new Date().getYear()
do?
A

Date.prototype.getYear() - from a date object, return the year as a number minus 1900

Don’t use it, it’s old and silly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
What does 
new Date().getFullYear()
do?
A

Date.prototype.getFullYear() - from a date object, return the year as a number (in years since year 0)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
What does 
new Date().getDate()
do?
A

Date.prototype.getDate() - from a date object, return the day of the month as a number. Returns NaN when the day is 0 or greater than the number of days the month should have

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

Use the time object to measure the time of a process

A
let start = Date.now();
  ANY CODE
  return Date.now() - start;
20
Q

what do millliseconds in the date object creation argument do?

let date1 = new Date(0);
alert(date1 );
let date1 = new Date(123434534);
alert(date1 );
A

Return a date object in milliseconds since Jan01_1970

let date2 = new Date(24 * 3600 * 1000);
alert( date2 );
Date object Jan02_1970 (24 hours per day, 3600 seconds per hour, 1000 milliseconds per second)

21
Q

What is the order of day month year in a string argument for a date object?

A

“year-month-day”

22
Q

Input year, month, day, hour etc as numbers into a date object.

A

new Date(2011, 0, 1, 0, 0, 0, 0);

23
Q

What numbers correspond to what month to date objects?

A

The month count starts with 0 (Jan), up to 11 (Dec)

24
Q

If the date (day of the month) parameter is absent, what is it assumed to be?

A

1

25
Q

If hours/minutes/seconds/ms is absent, what are they assumed to be?

A

they are assumed to be equal 0.

26
Q

return the month as a number

Get the month, from 0 to 11.

A

Date.prototype.getMonth()

Get the month, from 0 to 11.

27
Q

Return hours, minutes etc from a date object as numbers

A

Date.prototype.getHours()
Date.prototype.getMinutes()
Date.prototype.getSeconds()
Date.prototype.getMilliseconds()

28
Q

How do you return hours, minutes, day, month, etc in the UTC time zone?

A
Date.prototype.getUTCMonth()
Date.prototype.getUTCFullYear()
Date.prototype.getUTCDay()
Date.prototype.getUTCHours()
Date.prototype.getUTCMilliseconds()
29
Q

Return a date object ms since jan 1970

A

Date.prototype.getTime()

30
Q

Return the difference between UTC and the local time zone, in minutes:

A

Date.prototype.getTimezoneOffset()

31
Q

Set a date object’s hours, day, month etc. numbers to specific numbers

A

Date.prototype.setFullYear(year, [month], [date])
Date.prototype.setMonth(month, [date])
Date.prototype.setDate(date)
Date.prototype.setHours(hour, [min], [sec], [ms])
Date.prototype.setMinutes(min, [sec], [ms])
Date.prototype.setSeconds(sec, [ms])
Date.prototype.setMilliseconds(ms)

Date.prototype.setTime(milliseconds) (sets the whole date by milliseconds since 01.01.1970 UTC)

32
Q

Set a date object’s hours, day, month etc. numbers to specific numbers UTC

A

Date.prototype.setUTCFullYear(year, [month], [date])
Date.prototype.setUTCMonth(month, [date])
Date.prototype.setUTCDate(date)
Date.prototype.setUTCHours(hour, [min], [sec], [ms])
Date.prototype.setUTCMinutes(min, [sec], [ms])
Date.prototype.setUTCSeconds(sec, [ms])
Date.prototype.setUTCMilliseconds(ms)

33
Q
What is returned from this date?
let date = new Date(2013, 0, 32); // 32 Jan 2013 ?!?
alert(date); //
A

…is 1st Feb 2013!

it autocorrects

34
Q

Add 70 seconds to a date object

A
let date = new Date();
date.setSeconds(date.getSeconds() + 70);
35
Q

First day of the month is 1, what happens:
date.setDate(0);
alert( date );

A
// min day is 1, so the last day of the previous month is assumed
// 31 Dec 2015
36
Q

What happens when you convert a date to a number?

What is logged to console from:
let newDate = new Date();
let newDate2 = Date();
console.log(+newDate, +newDate2);

A

Converts to ms since 1970

145633456457
NaN (this was a string)

37
Q
Date.now() 
and 
new Date().getTime()
return the same thing 
t or f
A

t

38
Q
Date.now() is faster than new Date().getTime()
t or f
A

t

it doesn’t have to create an object first

39
Q

run a benchmark multiple times to get an accurate result (not effected by other processes)

A
function diffSubtract(date1, date2) {
  return date2 - date1;
}
function diffGetTime(date1, date2) {
  return date2.getTime() - date1.getTime();
}
function bench(f) {
  let date1 = new Date(0);
  let date2 = new Date();
  let start = Date.now();
  for (let i = 0; i < 100000; i++) f(date1, date2);
  return Date.now() - start;
}
let time1 = 0;
let time2 = 0;
// run bench(diffSubtract) and bench(diffGetTime) each 10 times alternating
for (let i = 0; i < 10; i++) {
  time1 += bench(diffSubtract);
  time2 += bench(diffGetTime);
}

alert( ‘Total time for diffSubtract: ‘ + time1 );
alert( ‘Total time for diffGetTime: ‘ + time2 );

40
Q

What is the purpose of the function calls outside the loop?

bench(diffSubtract);
bench(diffGetTime);

for (let i = 0; i < 10; i++) {
time1 += bench(diffSubtract);
time2 += bench(diffGetTime);
}

A

// added for “heating up” prior to the main loop
bench(diffSubtract);
bench(diffGetTime);

// now benchmark
for (let i = 0; i < 10; i++) {
  time1 += bench(diffSubtract);
  time2 += bench(diffGetTime);
}

Modern JavaScript engines start applying advanced optimizations only to “hot code” that executes many times (no need to optimize rarely executed things). So, in the example above, first executions are not well-optimized. We may want to add a heat-up run:

(but better to just learn about what are the best ways to do something. there are many optimizations and contexts we may not know about)

41
Q

The proper format of the date string for Date.parse(str)

A

‘YYYY-MM-DDTHH:mm:ss.sssZ’
let ms = Date.parse(‘2012-01-26T13:51:50.417-07:00’);
(it’s not dash 07:00. It’s + or -
‘2012-01-26T13:51:50.417+07:00’)

42
Q

Create a date object using a Date.parse() return

Create a date object without using Date.parse()

A
let date = new Date( Date.parse('2012-01-26T13:51:50.417-07:00') );
But you don't have to:
let date = new Date('2012-01-26T13:51:50.417-07:00');
43
Q

Create a function getDateAgo(date, days) to return the day of month days ago from the date.

For instance, if today is 20th, then getDateAgo(new Date(), 1) should be 19th and getDateAgo(new Date(), 2) should be 18th.

Should work reliably for days=365 or more:

A
function getDateAgo(date, days) {
  date.setDate(date.getDate() - days);
  return date.getDate();
}
44
Q

Write a function getLastDayOfMonth(year, month) that returns the last day of month. Sometimes it is 30th, 31st or even 28/29th for Feb.

Parameters:

year – four-digits year, for instance 2012.
month – month, from 0 to 11.
For instance, getLastDayOfMonth(2012, 1) = 29 (leap year, Feb).

A
function getLastDayOfMonth(year, month) {
  let date = new Date(year, month + 1, 0);
  return date.getDate();
}

alert( getLastDayOfMonth(2012, 0) ); // 31
alert( getLastDayOfMonth(2012, 1) ); // 29
alert( getLastDayOfMonth(2013, 1) ); // 28

45
Q

Write a function getSecondsToday() that returns the number of seconds from the beginning of today.

For instance, if now were 10:00 am, and there was no daylight savings shift, then:

getSecondsToday() == 36000 // (3600 * 10)
The function should work in any day. That is, it should not have a hard-coded value of “today”.

A
function getSecondsToday() {
  let now = new Date();
  // create an object using the current day/month/year
  let today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
  let diff = now - today; // ms difference
  return Math.round(diff / 1000); // make seconds
}

alert( getSecondsToday() );

OR

function getSecondsToday() {
  let d = new Date();
  return d.getHours() * 3600 + d.getMinutes() * 60 + d.getSeconds();
}

alert( getSecondsToday() );

OR

46
Q

Write a function formatDate(date) that should format date as follows:

If since date passed less than 1 second, then “right now”.
Otherwise, if since date passed less than 1 minute, then “n sec. ago”.
Otherwise, if less than an hour, then “m min. ago”.
Otherwise, the full date in the format “DD.MM.YY HH:mm”. That is: “day.month.year hours:minutes”, all in 2-digit format, e.g. 31.12.16 10:00.
For instance:

alert( formatDate(new Date(new Date - 1)) ); // “right now”

alert( formatDate(new Date(new Date - 30 * 1000)) ); // “30 sec. ago”

alert( formatDate(new Date(new Date - 5 * 60 * 1000)) ); // “5 min. ago”

// yesterday's date like 31.12.16 20:00
alert( formatDate(new Date(new Date - 86400 * 1000)) );
A
function formatDate(date) {
  let diff = new Date() - date; // the difference in milliseconds

if (diff < 1000) { // less than 1 second
return ‘right now’;
}

let sec = Math.floor(diff / 1000); // convert diff to seconds

if (sec < 60) {
return sec + ‘ sec. ago’;
}

  let min = Math.floor(diff / 60000); // convert diff to minutes
  if (min < 60) {
    return min + ' min. ago';
  }
  // format the date
  // add leading zeroes to single-digit day/month/hours/minutes
  let d = date;
  d = [
    '0' + d.getDate(),
    '0' + (d.getMonth() + 1),
    '' + d.getFullYear(),
    '0' + d.getHours(),
    '0' + d.getMinutes()
  ].map(component => component.slice(-2)); // take last 2 digits of every component
  // join the components into date
  return d.slice(0, 3).join('.') + ' ' + d.slice(3).join(':');
}

alert( formatDate(new Date(new Date - 1)) ); // “right now”

alert( formatDate(new Date(new Date - 30 * 1000)) ); // “30 sec. ago”

alert( formatDate(new Date(new Date - 5 * 60 * 1000)) ); // “5 min. ago”

// yesterday's date like 31.12.2016 20:00
alert( formatDate(new Date(new Date - 86400 * 1000)) );

OR

function formatDate(date) {
  let dayOfMonth = date.getDate();
  let month = date.getMonth() + 1;
  let year = date.getFullYear();
  let hour = date.getHours();
  let minutes = date.getMinutes();
  let diffMs = new Date() - date;
  let diffSec = Math.round(diffMs / 1000);
  let diffMin = diffSec / 60;
  let diffHour = diffMin / 60;
  // formatting
  year = year.toString().slice(-2);
  month = month < 10 ? '0' + month : month;
  dayOfMonth = dayOfMonth < 10 ? '0' + dayOfMonth : dayOfMonth;
  hour = hour < 10 ? '0' + hour : hour;
  minutes = minutes < 10 ? '0' + minutes : minutes;
  if (diffSec < 1) {
    return 'right now';
  } else if (diffMin < 1) {
    return `${diffSec} sec. ago`
  } else if (diffHour < 1) {
    return `${diffMin} min. ago`
  } else {
    return `${dayOfMonth}.${month}.${year} ${hour}:${minutes}`
  }
}