Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 2016, Fred McClurg All Rights Reserved

Similar presentations


Presentation on theme: "© Copyright 2016, Fred McClurg All Rights Reserved"— Presentation transcript:

1 © Copyright 2016, Fred McClurg All Rights Reserved
Dates 8/27/2018 Dates “Bad dates.” ‒ Sallah (John Rhys-Davies), Raiders of the Lost Ark (1981) © Copyright 2016, Fred McClurg All Rights Reserved Introduction to JavaScript

2 Setting Today’s Date What is a Date? Dates in JavaScript are objects.
Example: // set to current date/time var today = new Date(); console.log( today ); dateSetToday.html

3 Setting Date via Arguments
How do I set a specific date? A date can be set via multiple arguments. Examples: var year = 1941; var month = ; // 0=january var day = 7; var hour = 7; var minute = 48; // set to December 7, :48 am var pearlHarbor = new Date( year, month, day, hour, minute ); console.log( pearlHarbor ); dateSetArgs.html

4 Setting Date via String
Dates 8/27/2018 Setting Date via String Can I set a date with a string? Dates can also be set via a string. Examples: var dateStr = "December 7, :48"; var pearlHarbor = new Date( dateStr ); console.log( pearlHarbor ); dateSetStr.html Introduction to JavaScript

5 Getting Date Values What is a Date?
The date object has a number of methods for retrieving values. Examples: // set to current date/time var today = new Date(); console.log( today ); today.getFullYear(); // YYYY today.getMonth(); // =january today.getDate(); // 1-31 day of month today.getDay(); // 0-6 dow 0=sun today.getHours(); // 0-23 today.getTime(); // ms since 1/1/1970 dateMethods.html

6 Day of Week Calculator var dobStr = prompt( "Enter birthdate: (no dashes)", "December 7, 1941" ); // default // convert string to date object var dobObj = new Date( dobStr ); // calculate day of week var dowNum = dobObj.getDay(); // 0-6 day of week 0=Sun // create day of week array var dowStr = [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ]; // create message var msg = "Born on: " + dowStr[ dowNum ]; alert( msg ); dateDowPrompt.html


Download ppt "© Copyright 2016, Fred McClurg All Rights Reserved"

Similar presentations


Ads by Google