ECA 225 Applied Online Programming javascript dates ECA

  • Slides: 41
Download presentation
ECA 225 Applied Online Programming javascript dates ECA 225 Applied Interactive Programming

ECA 225 Applied Online Programming javascript dates ECA 225 Applied Interactive Programming

Date( ) constructor to create a new Date object use the new operator var

Date( ) constructor to create a new Date object use the new operator var now = new Date( ); this instance of a Date( ) object contains the current date and time, the exact date and time, to the millisecond, it was created ECA 225 Applied Interactive Programming

Date( ) constructor cont … if we use the alert( ) method to display

Date( ) constructor cont … if we use the alert( ) method to display the current date var now = new Date( ); alert( now ); we get a value similar to: Thu Sep 25 15: 28: 20 EDT 2003 ECA 225 Applied Interactive Programming

Date( ) methods v the Date( ) object can be broken down into its

Date( ) methods v the Date( ) object can be broken down into its individual pieces using a variety of Date( ) methods v because the variable name we created ( now ) holds a reference to a Date( ) object, we can use dot notation to access methods ECA 225 Applied Interactive Programming

Date( ) methods v Date. get. Date( v returns cont … ) the numeric

Date( ) methods v Date. get. Date( v returns cont … ) the numeric day of the month var now = new Date( ); var d = now. get. Date( ); // returns day of month alert( d ) ECA 225 Applied Interactive Programming

Date( ) methods v Date. get. Day( v returns cont … ) the numeric

Date( ) methods v Date. get. Day( v returns cont … ) the numeric day of the week, 0 – 6 var now = new Date( ); var d = now. get. Day( ); // returns day of week alert( d ) ECA 225 Applied Interactive Programming

Date( ) methods cont … v to return a string representation of the day

Date( ) methods cont … v 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. get. Day( ) ] ) ; ECA 225 Applied Interactive Programming

Date( ) methods v Date. get. Month( v returns cont … ) the numeric

Date( ) methods v Date. get. Month( v returns cont … ) the numeric month, 0 – 11 var now = new Date( ); var month = now. get. Month( ); // returns month alert( month ) ECA 225 Applied Interactive Programming

Date( ) methods cont … v to return a string representation of the month,

Date( ) methods cont … v 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. get. Month( ) ] ) ; ECA 225 Applied Interactive Programming

Date( ) methods v Date. get. Full. Year( v returns cont … ) the

Date( ) methods v Date. get. Full. Year( v returns cont … ) the numeric 4 -digit year var now = new Date( ); var y 4 = now. get. Full. Year( ); // returns 4 -digit year alert( y 4 ) ECA 225 Applied Interactive Programming

Date( ) methods v Date. get. Year( v returns cont … ) the numeric

Date( ) methods v Date. get. Year( v returns cont … ) the numeric 2 -digit year, pre-2000 var now = new Date( ); var y 2 = now. get. Year( ); // returns 2 -digit year alert( y 2 ) ECA 225 Applied Interactive Programming

Date( ) methods v Date. get. Hours( v returns cont … ) the numeric

Date( ) methods v Date. get. Hours( v returns cont … ) the numeric hours, 0 – 23 var now = new Date( ); var h = now. get. Hours( ); // returns hours, 0 – 23 alert( h ) ECA 225 Applied Interactive Programming

Date( ) methods v Date. get. Minutes( v returns cont … ) the numeric

Date( ) methods v Date. get. Minutes( v returns cont … ) the numeric minutes, 0 – 59 var now = new Date( ); var m = now. get. Minutes( ); // returns hours, 0 – 59 alert( m ) ECA 225 Applied Interactive Programming

Date( ) methods v Date. get. Seconds( v returns cont … ) the numeric

Date( ) methods v Date. get. Seconds( v returns cont … ) the numeric seconds, 0 – 59 var now = new Date( ); var s = now. get. Seconds( ); // returns seconds, 0 – 59 alert( s ) ECA 225 Applied Interactive Programming

Date( ) methods cont … v Date. get. Milliseconds( v returns ) the numeric

Date( ) methods cont … v Date. get. Milliseconds( v returns ) the numeric milliseconds, 0 – 999 var now = new Date( ); var ms = now. get. Milliseconds( ); // returns 0 – 999 alert( ms ) ECA 225 Applied Interactive Programming

local time v the date and time to which a Date( ) object is

local time v 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 v scripts read the clock setting of the user’s computer v to make time comparisons between a user’s local time and another time zone a standard reference point is needed ECA 225 Applied Interactive Programming

Greenwich Mean Time GMT Ohio – 5 Uzbekistan +5 IDL ECA 225 Applied Interactive

Greenwich Mean Time GMT Ohio – 5 Uzbekistan +5 IDL ECA 225 Applied Interactive Programming

GMT / UTC v another name for GMT is UTC, Coordinated Universal Time v

GMT / UTC v another name for GMT is UTC, Coordinated Universal Time v many Java. Script methods have 2 versions v one for local time v one for GMT or UTC ECA 225 Applied Interactive Programming

milliseconds v basic unit of measuring Java. Script time is one millisecond, or 1/1000

milliseconds v basic unit of measuring Java. Script time is one millisecond, or 1/1000 th of a second v Java. Script maintains date information in the form of a count, in milliseconds, since 1 January, 1970, at midnight, in Greenwich, England v midnight, 1 January, 1970 is known as the Unix Epoch ECA 225 Applied Interactive Programming

Date( ) methods v Date. get. Time( cont … ) v returns the number

Date( ) methods v Date. get. Time( cont … ) v returns the number of milliseconds since the Unix Epoch, midnight, 1 January, 1970 var now = new Date( ); var ts = now. get. Time( ); // returns the number // of milliseconds since midnight, 1 Jan, 1970 alert( ts ) ECA 225 Applied Interactive Programming

comparing Date( ) objects v to compare different Date( ) objects, return the number

comparing Date( ) objects v to compare different Date( ) objects, return the number of milliseconds that have passed for each, since the Unix Epoch v compare the milliseconds – the one which is smaller in size occurred first ECA 225 Applied Interactive Programming

creating Date( ) objects to create Date( ) object to a specific day and

creating Date( ) objects to create Date( ) object to a specific day and time, such as a future date 1. 2. 3. 4. 5. 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 ); ECA 225 Applied Interactive Programming

creating Date( ) objects cont … v to create a Date( ) object holding

creating Date( ) objects cont … v to create a Date( ) object holding the values for New Year’s Day, midnight, 2013 var new. Year = new Date( “January 1 2013 00: 00 “ ); var new. Year = new Date(2013, 00, 01, 00, 00) ECA 225 Applied Interactive Programming

Date( ) set methods v Java. Script’s Date( ) set methods are used to

Date( ) set methods v Java. Script’s Date( ) set methods are used to set or reset the values associated with the object var date 1 = new Date( ); alert ( date 1 ); date 1. set. Full. Year( 2007 ); alert ( date 1 ); v set methods return the new date in milliseconds ECA 225 Applied Interactive Programming

Date( ) set methods v Date. set. Date( v set cont … ) the

Date( ) set methods v Date. set. Date( v set cont … ) the day, given a number between 1 – 31 var date 1 = new Date( ); alert ( date 1 ); date 1. set. Date( 13 ); alert ( date 1 ); ECA 225 Applied Interactive Programming

Date( ) set methods v Date. set. Month( v set cont … ) the

Date( ) set methods v Date. set. Month( v set cont … ) the month, given a number between 0 – 11 var date 1 = new Date( ); alert ( date 1 ); date 1. set. Month( 3 ); alert ( date 1 ); ECA 225 Applied Interactive Programming

Date( ) set methods v Date. set. Full. Year( v set cont … )

Date( ) set methods v Date. set. Full. Year( v set cont … ) the year, given a 4 digit year var date 1 = new Date( ); alert ( date 1 ); date 1. set. Full. Year( 2013 ); alert ( date 1 ); ECA 225 Applied Interactive Programming

Date( ) set methods v Date. set. Year( v set cont … ) the

Date( ) set methods v Date. set. Year( v set cont … ) the year, given a 2 or 4 digit year var date 1 = new Date( ); alert ( date 1 ); date 1. set. Year( 2013 ); alert ( date 1 ); ECA 225 Applied Interactive Programming

Date( ) set methods v Date. set. Hours( v set cont … ) the

Date( ) set methods v Date. set. Hours( v set cont … ) the hour, given a number between 0 – 23 var date 1 = new Date( ); alert ( date 1 ); date 1. set. Hour( 13 ); alert ( date 1 ); ECA 225 Applied Interactive Programming

Date( ) set methods v Date. set. Minutes( v set cont … ) the

Date( ) set methods v Date. set. Minutes( v set cont … ) the minutes, given a number between 0 – 59 var date 1 = new Date( ); alert ( date 1 ); date 1. set. Minutes( 33 ); alert ( date 1 ); ECA 225 Applied Interactive Programming

Date( ) set methods v Date. set. Seconds( v set cont … ) the

Date( ) set methods v Date. set. Seconds( v set cont … ) the seconds, given a number between 0 – 59 var date 1 = new Date( ); alert ( date 1 ); date 1. set. Seconds( 33 ); alert ( date 1 ); ECA 225 Applied Interactive Programming

Date( ) set methods v Date. set. Milliseconds( v set cont … ) the

Date( ) set methods v Date. set. Milliseconds( v set cont … ) the milliseconds, given a number between 0 – 999 var date 1 = new Date( ); alert ( date 1 ); date 1. set. Milliseconds( 613 ); alert ( date 1 ); ECA 225 Applied Interactive Programming

Date( ) set methods v Date. set. Time( cont … ) v set a

Date( ) set methods v Date. set. Time( cont … ) v set a date, given the number of milliseconds since 1 January 1970 var date 1 = new Date( ); alert ( date 1 ); date 1. set. Time(1036090020000 ); alert ( date 1 ); ECA 225 Applied Interactive Programming

Date( ) set methods cont … va construction used to set a Date( )

Date( ) set methods cont … va construction used to set a Date( ) object to a specific distance in the future: var date 1 = new Date( ); alert ( date 1 ); date 1. set. Full. Year( date 1. get. Full. Year( ) + 1 ); alert ( date 1 ); ECA 225 Applied Interactive Programming

Date & Time Arithmetic v the millisecond is the Java. Script basic unit of

Date & Time Arithmetic v the millisecond is the Java. Script basic unit of time v precise time calculations involving the addition or subtraction of dates should be performed using milliseconds ECA 225 Applied Interactive Programming

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

Date & Time Arithmetic v creation cont … 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; ECA 225 Applied Interactive Programming

. js library v the previous variables, plus the arrays holding the names of

. js library v the previous variables, plus the arrays holding the names of the days and months, can be placed in an external Java. Script file v the external file, or library, can then be referenced by any page, similar to an external style sheet v external files must end with a. js extension ECA 225 Applied Interactive Programming

. js library v the cont … reference to the external. js file will

. js library v the cont … reference to the external. js file will look like: <script type=‘text/javascript’ href=‘path_to_js_file. js’ ></script> v the <script> tag MUST have a corresponding closing </script> tag v avoid putting any other code inside the opening and closing <script> tags v place reference inside <head> tags ECA 225 Applied Interactive Programming

. js library cont … v the server must be configured correctly to serve

. js library cont … v the server must be configured correctly to serve an external. js file v CAUTION: using an external. js file does not hide your code v do not place any confidential or sensitive material inside a. js file ECA 225 Applied Interactive Programming

Date & Time Arithmetic cont … v EG, to create a date exactly 26

Date & Time Arithmetic cont … v EG, to create a date exactly 26 weeks from the current date: var date 1 = new Date( ); alert ( date 1 ); date 1. set. Time( date 1. get. Time( ) + one_week * 26 ); alert ( date 1 ); ECA 225 Applied Interactive Programming

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

Date & Time Arithmetic cont … v to set the expiration date for a cookie to 6 months hence var exp = new Date( ); exp. set. Time( exp. get. Time( ) + one_week * 26 ); or var exp = new Date( ); exp. set. Month( exp. get. Month( ) + 6 ); ECA 225 Applied Interactive Programming