1 / 41

ECA 225

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( );.

Download Presentation

ECA 225

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ECA 225 AppliedOnline Programming javascript dates ECA 225 Applied Interactive Programming

  2. 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( ); ECA 225 Applied Interactive Programming

  3. 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 ECA 225 Applied Interactive Programming

  4. 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 ECA 225 Applied Interactive Programming

  5. 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 ) ECA 225 Applied Interactive Programming

  6. 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 ) ECA 225 Applied Interactive Programming

  7. 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( ) ] ) ; ECA 225 Applied Interactive Programming

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

  9. 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( ) ] ) ; ECA 225 Applied Interactive Programming

  10. 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 ) ECA 225 Applied Interactive Programming

  11. 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 ) ECA 225 Applied Interactive Programming

  12. 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 ) ECA 225 Applied Interactive Programming

  13. 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 ) ECA 225 Applied Interactive Programming

  14. 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 ) ECA 225 Applied Interactive Programming

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

  16. 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 ECA 225 Applied Interactive Programming

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

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

  19. 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 ECA 225 Applied Interactive Programming

  20. 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 ) ECA 225 Applied Interactive Programming

  21. 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 ECA 225 Applied Interactive Programming

  22. 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 ); ECA 225 Applied Interactive Programming

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

  24. 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 ); ECA 225 Applied Interactive Programming

  25. 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 ); ECA 225 Applied Interactive Programming

  26. 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 ); ECA 225 Applied Interactive Programming

  27. 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 ); ECA 225 Applied Interactive Programming

  28. 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 ); ECA 225 Applied Interactive Programming

  29. 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 ); ECA 225 Applied Interactive Programming

  30. 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 ); ECA 225 Applied Interactive Programming

  31. 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 ); ECA 225 Applied Interactive Programming

  32. 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 ); ECA 225 Applied Interactive Programming

  33. 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 ); ECA 225 Applied Interactive Programming

  34. 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 ); ECA 225 Applied Interactive Programming

  35. 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 ECA 225 Applied Interactive Programming

  36. 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; ECA 225 Applied Interactive Programming

  37. .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 ECA 225 Applied Interactive Programming

  38. .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> ECA 225 Applied Interactive Programming

  39. .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 code • do not place any confidential or sensitive material inside a .js file ECA 225 Applied Interactive Programming

  40. 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 ); ECA 225 Applied Interactive Programming

  41. 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 ); ECA 225 Applied Interactive Programming

More Related