Part 6- A Smarter Way to Learn JavaScript Flashcards
Code for converting a string that’s a number to its equivalent number
Number(variableName);
(the name of the variable is at the end)
Note: works for integer strings and floating-point number strings
Code for converting a number to a string?
variableName.toString();
(the name of the variable is at the beginning)
Limit the decimal places on a number?
varName.toFixed(# of digits);
HOWEVER this also converts it to a string
What is the code to get a new date object and assign it to the variable “now”?
var now = new Date();
*notice that “Date” is capitalized
In a Date object, what number is assigned to Sunday?
0
In a Date object, what number is assigned to Saturday?
6
In a Date object, what number is assigned to Thursday?
4
How would I extract the day of the week from a Date object called “party”?
party.getDay();
What’s the difference between these?
getDate();
getDay();
Date is day of the month
It’s indexed with 1
Day is the day of the week
It’s indexed with 0
What does getTime() return from a Date object??
The milliseconds since midnight, January 1, 1970
What does getMonth() return from a Date object??
A number. 0 for January, 1 for Feb etc. until December, which is 11
What’s the code that returns the year from a Date object “d”?
d.getFullYear();
What’s the code to return the smallest unit of time that JavaScript recognizes from a Date object “d”?
d.getMilliseconds()
Before you can getDate, getHours, or getFullYear, what must you do?
Get a Date object:
var d = new Date();
What is the code to return the hour from a Date object “d”?
d.getHours();
*Note that HOURS is plural. MINUTES, SECONDS, and MILLISECONDS
What is tricky about returning getMonth() from a Date object?
The numbers are off because the counting starts at zero. So January is zero and December is 11.
What is tricky about returning getDate() from a Date object?
The count starts at 1, not zero.
This is different from all of the other Date object parts, like MONTH, HOURS, etc.
How do you create a Date object for a time that is not right now?
new Date(“date info here”);
*The date should be in this format:
“June 30, 2024 13:10:53”
Code a single statement that displays the milliseconds that elapsed between the reference date and the beginning of 1980.
new Date(“January 1, 1980”).getTime();
What does “d” mean?
var d = new Date(“January 31, 2023”).getTime();
d is the number of milliseconds from the reference date (Jan 1, 1970) through Jan 31, 2023.