Date and Time Information


var d = new Date(); var mdy = (d.getMonth()+1) + "/" + d.getDate() + 

JavaScript's Date object provides access to the current date and is also capable of doing certain Date calculations (using the epoch value, the number of milliseconds since January 1, 1970). Table 2.2 contains the most important methods of the Date class. The preceding code creates a date in the format month/day/year.

Table 2.2. Some Date Properties

Method

Description

getdate()

Day of the month

getFullYear()

Four-digit year

getHours()

Hours

getMinutes()

Minutes

getMonth()

Month minus 1 (!)

getSeconds()

Seconds

getTime()

Epoch value

toString()

String representation

toUTCString()

UTC string representation


Understanding Regular Expressions

Regular expressions are, to put it simply, patterns that can be matched with strings. A pattern in a regular expression contains a string that can be searched for in a larger string. However, this can also be done (faster) using indexOf(). The advantage of regular expressions is that some special features such as wildcards are available. Table 2.3 shows some special characters and their meanings.

Table 2.3. Special Characters in Regular Expressions

Special Character

Description

Example

^

Beginning of the string

^a means a string that starts with a

$

End of the string

a$ means a string that ends with a

?

0 or 1 times (refers to the previous character or expression)

ab? means a or ab

*

0 or more times (refers to the previous character or expression)

ab* means a or ab or abb or…

+

1 or more times (refers to the previous character or expression)

ab+ means ab or abb or abbb or…

[...]

Alternative characters

PHP[45] means PHP4 or PHP5

- (used within square brackets)

A sequence of values

ECMAScript [3-5] means ECMAScript 3 or ECMAScript 4 or ECMAScript 5

^ (used within square brackets)

Does match anything but the following characters

[^A-C] means D or E or F or…

|

Alternative patterns

ECMAScript 3|ECMAScript 4 means ECMAScript 3 or ECMAScript 4, as does ECMAScript (3|4)

(...)

Defines a subpattern

(a)(b) means ab, but with two subpatterns (a and b)

.

Any character

. means a, b, c, 0, 1, 2, $, ^,…

{min, max}

Minimum and maximum number of occurrences; if either min or max is omitted, it means 0 or infinite

a{1,3} means a, aa, or aaa. a{,3} means empty string, a, aa, or aaa. a{1,} means a, aa, aaa,…

\

escapes the following character

\. stands for .


Other special characters and expressions are available, for instance, a character that refers to a digit (\d).





JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net