JavaScript Programming/Date and Time

This lesson introduces JavaScript objects using dates.

Objectives and Skills
Objectives and skills for this lesson include:
 * Complete and debug code that uses objects
 * Properties; methods; instantiation; Date object; retrieve date and time parts; localize date format (MM/DD vs DD/MM); add and subtract dates

Readings

 * 1)  JavaScript/Dates

Multimedia

 * 1) YouTube: Date Objects
 * 2) YouTube: Display and Change the Time And Date With JavaScript
 * 3) YouTube: Date methods in JavaScript
 * 4) YouTube: Displaying the Current/Live Date & Time on a Web Page
 * 5) YouTube: JavaScript Programming Tutorial 48 - Using Dates and Unix Timestamps in JavaScript
 * 6) #16 How to use the JS Date object | JavaScript Full Tutorial

Examples

 * W3Schools: JavaScript Date Objects
 * W3Schools: JavaScript Date Formats
 * W3Schools: JavaScript Date Get Methods
 * W3Schools: JavaScript Date Set Methods
 * W3Schools: Timing Events
 * JavaScript Get Date Methods
 * w3schools: Date Format For Date Picker
 * W3Schools: Javascript Date Examples
 * StackOverflow: How to update time regularly
 * /Example Code/

Activities
Complete the following activities using external JavaScript code. Apply JavaScript best practices, including comments, indentations, naming conventions, and constants. Use input elements for input and respond to input or change events, add buttons and respond to click events, or set a timer interval and respond to interval events. Use HTML elements for output. Use separate functions for each type of processing. Avoid global variables by passing parameters and returning results. Create test data to validate the accuracy of each program. Add comments at the top of the program and include references to any resources used.


 * 1) Create a web page to display the current date and time. Rather than displaying the date and time as a single string, use output elements and Date methods to separately display the year, month, day, hour, minute, and second. Set a timer interval so the current time updates automatically every second.
 * 2) Create a web page with a date picker (input type="date"). When a user selects a date, use output elements to separately display the selected month, day, and year. Be sure to account for UTC/time zone when displaying the selected date.
 * 3) Create a web page with a date picker (input type="date"). Add number fields (input type="number") that allow the user to add years, months, and days to the selected date. Limit additional months to 0-11. Limit additional days to 0-30. As the number fields change, use an output element to display the result of adding the given years, months, and days to the selected date.
 * 4) Create a web page with two date pickers (input type="date"). When a user selects both dates, use output elements to display the number of years, months, and days between the two dates.
 * 5) Use JavaScript Date.now and loops to time two different ways to implement the same thing. For example, is it faster to use a local variable or update an HTML element on each loop iteration?

Lesson Summary

 * The Date object is used to work with dates and times.
 * By default, JavaScript will use the browser's time zone and display a date as a full-text string.
 * Date objects are created with the new Date constructor, which can take parameters: year, month, day, hours, minutes, seconds, milliseconds.
 * JavaScript stores dates as the number of milliseconds since Unix Epoch (January 01, 1970, 00:00:00 UTC).
 * The JavaScript Date object has a list of get methods that can be used to get date's information such as day, hours, minutes, etc.
 * The JavaScript Date object also has a list of set methods that can be used to set date's information in a Date object.
 * The "date" input type creates a drop-down in html.
 * The timing events setTimeout executes once and setInterval executes function continously can be used to execute code at time interval.
 * A UNIX epoch time and JavaScript date are not the same. Unix epoch time is seconds Ince the Unix Epoch, whereas the JavaScript date is milliseconds.

Key Terms

 * epoch Time
 * Epoch time or Unix time is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC. Therefore, the Unix time stamp is merely the number of seconds between a particular date and the Unix Epoch.


 * ISO
 * ISO is the International standard for the representation of dates and times. The ISO syntax is YYYY-MM-DD, and is the preferred JavaScript date format.


 * new date
 * Returns a new Date object that represents the current date and time.


 * prototype
 * Allows you to add properties and methods to an object.


 * setInterval
 * A method calls a function or evaluates an expression at specified intervals (in milliseconds).


 * setTimeout
 * A method calls a function or evaluates an expression after a specified number of milliseconds.


 * UTC
 * The Universal Coordinated Time (UTC) is the time set by the World Time Standard.