tailieunhanh - JavaScript Bible, Gold Edition part 114

JavaScript Bible, Gold Edition part 114. This book will bring programmers and non-technical professionals, including casual programmers and scripters, painlessly up to speed on all aspects of mastering JavaScript. Key topics include programming fundamentals, JavaScript language elements and how to use them effectively, and how to easily and efficiently add powerful new functionality to HTML documents and Java applets. | 978 Part IV JavaScript Core Language Reference Date and time arithmetic You may need to perform some math with dates for any number of reasons. Perhaps you need to calculate a date at some fixed number of days or weeks in the future or figure out the number of days between two dates. When calculations of these types are required remember the lingua franca of JavaScript date values milliseconds. What you may need to do in your date-intensive scripts is establish some variable values representing the number of milliseconds for minutes hours days or weeks and then use those variables in your calculations. Here is an example that establishes some practical variable values building on each other var oneMinute 60 1000 var oneHour oneMinute 60 var oneDay oneHour 24 var oneWeek oneDay 7 With these values established in a script I can use one to calculate the date one week from today var targetDate new Date var dateInMs dateInMs oneWeek dateInMs Another example uses components of a date object to assist in deciding what kind of greeting message to place in a document based on the local time of the user s PC clock. Listing 36-2 adds to the scripting from Listing 36-1 bringing some quasi-intelligence to the proceedings. Again this script uses the older array creation mechanism to be compatible with Navigator 2 and Internet Explorer 3. Listing 36-2 A Dynamic Welcome Message HTML HEAD TITLE Date String Maker TITLE SCRIPT LANGUAGE JavaScript function MakeArray n n return this monthNames new MakeArray 12 monthNames 1 January monthNames 2 February monthNames 3 March monthNames 4 April monthNames 5 May monthNames 6 June monthNames 7 July monthNames 8 August monthNames 9 September monthNames 10 oCtober monthNames 11 November monthNames 12 December Chapter 36 The Date Object 979 dayNames new MakeArray 7 dayNames 1 Sunday dayNames 2 Monday dayNames 3 Tuesday dayNames 4 Wednesday dayNames 5 Thursday dayNames 6 Friday dayNames 7 Saturday

TỪ KHÓA LIÊN QUAN