Download presentation
Presentation is loading. Please wait.
1
Strings and Dates in JavaScript
MIS 2402 Maxwell Furman Department of MIS Fox School of Business Temple University
2
Let’s talk about … Chapter 13
Last class we saw that “Math” and “Number” are JavaScript objects. Is Math.PI a property or a method? Is Math.random() a property or a method? How can you easily distinguish between properties and methods? Are strings and dates objects too?
3
Agenda Review last week's challenge Methods and Properties of Strings
Representing Dates in Javascript Exam 2 review / lab period
4
Strings What will be the value of result_1?
6
Examples of methods of String objects
What will be the value of each result variable?
7
Examples of methods of String objects (continued)
What would be the advantage to converting user-provided data to all upper or lower case?
8
Reflection: While these individual string functions may not seem impressive, they can be used to parse and manipulate text. Used together with functions, conditional statements and loops to do elaborate things. See:
9
Date and time Notice the new operator here.
JavaScript Date objects can only be instantiated by calling JavaScript Date as a constructor: calling it as a regular function (i.e. without the new operator) will return a string rather than a Date object. Unlike other JavaScript object types, JavaScript Date objects have no literal syntax… that is, you *must* use the new operator to create a Date object.
10
Create a Date object by specifying date parts
11
How to create a Date object by copying another Date object
Some unexpected results when specifying a date string
12
The formatting methods of a Date object
13
The get methods of a Date object
14
The set methods of a Date object
15
Putting it together… A common programming challenge is to perform simple date "math" on a value. So for example, given that you have a certain date, what date will it be three hours from then? Or - if today is the 27th, what month will it be in two days? There are many ways of doing this, but one simple way is just combine a get and set call. //First, start with a particular time var date = new Date(); //Add two hours date.setHours(date.getHours() + 2); //Go back 3 days date.setDate(date.getDate() - 3); //One minute ago... date.setMinutes(date.getMinutes() - 1);
16
Let’s give this a try… Use countdown.zip provided by your instructor. Use Chrome Developer tools to set a breakpoint and step through the code. Observe how dates and strings are manipulated. Pro tip! You can avoid stepping into jQuery source code with “blackboxing” See:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.