Top Banner
ECA 225 Applied Interactiv e Programming ECA 225 Applied Online Programming javascript dates
41

ECA 225

Jan 02, 2016

Download

Documents

ishmael-paul

ECA 225. Applied Online Programming. javascript dates. Date( ) constructor. to create a new Date object use the new operator this instance of a Date( ) object contains the current date and time, the exact date and time, to the millisecond, it was created. var now = new Date( );. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ECA 225

ECA 225 Applied Interactive Programming

ECA 225AppliedOnline

Programming

javascript dates

Page 2: ECA 225

ECA 225 Applied Interactive Programming

Date( ) constructor

to create a new Date object use the new operator

this instance of a Date( ) object contains the current date and time, the exact date and time, to the millisecond, it was created var now = new Date( );

Page 3: ECA 225

ECA 225 Applied Interactive Programming

Date( ) constructor cont …

if we use the alert( ) method to display the current date

we get a value similar to: var now = new Date( );alert( now );

Thu Sep 25 15:28:20 EDT 2003

Page 4: ECA 225

ECA 225 Applied Interactive Programming

Date( ) methods

the Date( ) object can be broken down into its individual pieces using a variety of Date( ) methods

because the variable name we created ( now ) holds a reference to a Date( ) object, we can use dot notation to access methods

Page 5: ECA 225

ECA 225 Applied Interactive Programming

Date( ) methods cont …

Date.getDate( ) returns the numeric day of the month

var now = new Date( );

var d = now.getDate( ); // returns day of month

alert( d )

Page 6: ECA 225

ECA 225 Applied Interactive Programming

Date( ) methods cont …

Date.getDay( ) returns the numeric day of the week, 0 – 6

var now = new Date( );

var d = now.getDay( ); // returns day of week

alert( d )

Page 7: ECA 225

ECA 225 Applied Interactive Programming

Date( ) methods cont …

to return a string representation of the day of the week, use an array

var now = new Date( );

var days = new Array( “Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday” );

alert( days[ now.getDay( ) ] ) ;

Page 8: ECA 225

ECA 225 Applied Interactive Programming

Date( ) methods cont …

Date.getMonth( ) returns the numeric month, 0 – 11

var now = new Date( );

var month = now.getMonth( ); // returns month

alert( month )

Page 9: ECA 225

ECA 225 Applied Interactive Programming

Date( ) methods cont …

to return a string representation of the month, use an array

var now = new Date( );

var month = new Array( “January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December” );

alert( month[ now.getMonth( ) ] ) ;

Page 10: ECA 225

ECA 225 Applied Interactive Programming

Date( ) methods cont …

Date.getFullYear( ) returns the numeric 4-digit year

var now = new Date( );

var y4 = now.getFullYear( ); // returns 4-digit year

alert( y4 )

Page 11: ECA 225

ECA 225 Applied Interactive Programming

Date( ) methods cont …

Date.getYear( ) returns the numeric 2-digit year, pre-2000

var now = new Date( );

var y2 = now.getYear( ); // returns 2-digit year

alert( y2 )

Page 12: ECA 225

ECA 225 Applied Interactive Programming

Date( ) methods cont …

Date.getHours( ) returns the numeric hours, 0 – 23

var now = new Date( );

var h = now.getHours( ); // returns hours, 0 – 23

alert( h )

Page 13: ECA 225

ECA 225 Applied Interactive Programming

Date( ) methods cont …

Date.getMinutes( ) returns the numeric minutes, 0 – 59

var now = new Date( );

var m = now.getMinutes( ); // returns hours, 0 – 59

alert( m )

Page 14: ECA 225

ECA 225 Applied Interactive Programming

Date( ) methods cont …

Date.getSeconds( ) returns the numeric seconds, 0 – 59

var now = new Date( );

var s = now.getSeconds( ); // returns seconds, 0 – 59

alert( s )

Page 15: ECA 225

ECA 225 Applied Interactive Programming

Date( ) methods cont …

Date.getMilliseconds( ) returns the numeric milliseconds, 0 – 999

var now = new Date( );

var ms = now.getMilliseconds( ); // returns 0 – 999

alert( ms )

Page 16: ECA 225

ECA 225 Applied Interactive Programming

local time

the date and time to which a Date( ) object is set is based upon the local time of the user’s computer, not the server

scripts read the clock setting of the user’s computer

to make time comparisons between a user’s local time and another time zone a standard reference point is needed

Page 17: ECA 225

ECA 225 Applied Interactive Programming

Greenwich Mean Time GMT

Uzbekistan+5

Ohio– 5

IDL

Page 18: ECA 225

ECA 225 Applied Interactive Programming

GMT / UTC

another name for GMT is UTC, Coordinated Universal Time

many JavaScript methods have 2 versionsone for local timeone for GMT or UTC

Page 19: ECA 225

ECA 225 Applied Interactive Programming

milliseconds

basic unit of measuring JavaScript time is one millisecond, or 1/1000th of a second

JavaScript maintains date information in the form of a count, in milliseconds, since 1 January, 1970, at midnight, in Greenwich, England

midnight, 1 January, 1970 is known as the Unix Epoch

Page 20: ECA 225

ECA 225 Applied Interactive Programming

Date( ) methods cont …

Date.getTime( ) returns the number of milliseconds since the Unix

Epoch, midnight, 1 January, 1970

var now = new Date( );

var ts = now.getTime( ); // returns the number

// of milliseconds since midnight, 1 Jan, 1970

alert( ts )

Page 21: ECA 225

ECA 225 Applied Interactive Programming

comparing Date( ) objects

to compare different Date( ) objects, return the number of milliseconds that have passed for each, since the Unix Epoch

compare the milliseconds – the one which is smaller in size occurred first

Page 22: ECA 225

ECA 225 Applied Interactive Programming

creating Date( ) objects

to create Date( ) object to a specific day and time, such as a future date

new Date( “Month dd, yyyy hh:mm:ss “ ); new Date( “Month dd, yyyy” ); new Date( yy,mm,dd,hh,mm,ss ); new Date( yy,mm,dd ); new Date( milliseconds );

Page 23: ECA 225

ECA 225 Applied Interactive Programming

to create a Date( ) object holding the values for New Year’s Day, midnight, 2013

creating Date( ) objects cont …

var newYear = new Date(2013,00,01,00,00,00)

var newYear = new Date( “January 1 2013 00:00:00 “ );

Page 24: ECA 225

ECA 225 Applied Interactive Programming

Date( ) set methods JavaScript’s Date( ) set methods are used to set

or reset the values associated with the object

set methods return the new date in milliseconds

var date1 = new Date( );alert ( date1 );date1.setFullYear( 2007 );alert ( date1 );

Page 25: ECA 225

ECA 225 Applied Interactive Programming

Date( ) set methods cont …

Date.setDate( ) set the day, given a number between 1 – 31

var date1 = new Date( );alert ( date1 );date1.setDate( 13 );alert ( date1 );

Page 26: ECA 225

ECA 225 Applied Interactive Programming

Date( ) set methods cont …

Date.setMonth( ) set the month, given a number between 0 – 11

var date1 = new Date( );alert ( date1 );date1.setMonth( 3 );alert ( date1 );

Page 27: ECA 225

ECA 225 Applied Interactive Programming

Date( ) set methods cont …

Date.setFullYear( ) set the year, given a 4 digit year

var date1 = new Date( );alert ( date1 );date1.setFullYear( 2013 );alert ( date1 );

Page 28: ECA 225

ECA 225 Applied Interactive Programming

Date( ) set methods cont …

Date.setYear( ) set the year, given a 2 or 4 digit year

var date1 = new Date( );alert ( date1 );date1.setYear( 2013 );alert ( date1 );

Page 29: ECA 225

ECA 225 Applied Interactive Programming

Date( ) set methods cont …

Date.setHours( ) set the hour, given a number between 0 – 23

var date1 = new Date( );alert ( date1 );date1.setHour( 13 );alert ( date1 );

Page 30: ECA 225

ECA 225 Applied Interactive Programming

Date( ) set methods cont …

Date.setMinutes( ) set the minutes, given a number between 0 – 59

var date1 = new Date( );alert ( date1 );date1.setMinutes( 33 );alert ( date1 );

Page 31: ECA 225

ECA 225 Applied Interactive Programming

Date( ) set methods cont …

Date.setSeconds( ) set the seconds, given a number between 0 – 59

var date1 = new Date( );alert ( date1 );date1.setSeconds( 33 );alert ( date1 );

Page 32: ECA 225

ECA 225 Applied Interactive Programming

Date( ) set methods cont …

Date.setMilliseconds( ) set the milliseconds, given a number between 0 – 999

var date1 = new Date( );alert ( date1 );date1.setMilliseconds( 613 );alert ( date1 );

Page 33: ECA 225

ECA 225 Applied Interactive Programming

Date( ) set methods cont …

Date.setTime( ) set a date, given the number of milliseconds since

1 January 1970

var date1 = new Date( );alert ( date1 );date1.setTime(1036090020000 );alert ( date1 );

Page 34: ECA 225

ECA 225 Applied Interactive Programming

Date( ) set methods cont …

a construction used to set a Date( ) object to a specific distance in the future:

var date1 = new Date( );alert ( date1 );date1.setFullYear( date1.getFullYear( ) + 1 );alert ( date1 );

Page 35: ECA 225

ECA 225 Applied Interactive Programming

Date & Time Arithmetic

the millisecond is the JavaScript basic unit of time

precise time calculations involving the addition or subtraction of dates should be performed using milliseconds

Page 36: ECA 225

ECA 225 Applied Interactive Programming

Date & Time Arithmetic cont … creation of variables holding the number of

milliseconds in a minute, hour, day and week may be useful

var one_minute = 60 * 1000;var one_hour = one_minute * 60;var one_day = one_hour * 24;var one_week = one_day * 7;

Page 37: ECA 225

ECA 225 Applied Interactive Programming

.js library

the previous variables, plus the arrays holding the names of the days and months, can be placed in an external JavaScript file

the external file, or library, can then be referenced by any page, similar to an external style sheet

external files must end with a .js extension

Page 38: ECA 225

ECA 225 Applied Interactive Programming

.js library cont … the reference to the external .js file will look like:

the <script> tag MUST have a corresponding closing </script> tag

avoid putting any other code inside the opening and closing <script> tags

place reference inside <head> tags

<script type=‘text/javascript’ href=‘path_to_js_file.js’ ></script>

Page 39: ECA 225

ECA 225 Applied Interactive Programming

.js library cont …

the server must be configured correctly to serve an external .js file

CAUTION: using an external .js file does not hide your codedo not place any confidential or sensitive material

inside a .js file

Page 40: ECA 225

ECA 225 Applied Interactive Programming

Date & Time Arithmetic cont … EG, to create a date exactly 26 weeks from the

current date:

var date1 = new Date( );alert ( date1 );date1.setTime( date1.getTime( ) + one_week * 26 );alert ( date1 );

Page 41: ECA 225

ECA 225 Applied Interactive Programming

Date & Time Arithmetic cont … to set the expiration date for a cookie to 6

months hence

or

var exp = new Date( );exp.setTime( exp.getTime( ) + one_week * 26 );

var exp = new Date( );exp.setMonth( exp.getMonth( ) + 6 );