Javascript-7對象:字符串、時間

JavaScript-7對象:字符串、時間

       字符串(String)對象

              Javascript是面向對象的編程語言,我們可以定義自己的對象和變量類型。

       計算字符串長度

var txt="Hello world!"

document.write(txt.length)

結果爲:12

       字符串樣式

              document.write("<p>Big: " + txt.big() + "</p>")

document.write("<p>Small: " + txt.small() + "</p>")

document.write("<p>Bold: " + txt.bold() + "</p>")

document.write("<p>Italic: " + txt.italics() + "</p>")

document.write("<p>Blink: " + txt.blink() + " (does not work in IE)</p>")

document.write("<p>Fixed: " + txt.fixed() + "</p>")

document.write("<p>Strike: " + txt.strike() + "</p>")

document.write("<p>Fontcolor: " + txt.fontcolor("Red") + "</p>")

document.write("<p>Fontsize: " + txt.fontsize(16) + "</p>")

document.write("<p>Lowercase: " + txt.toLowerCase() + "</p>")

document.write("<p>Uppercase: " + txt.toUpperCase() + "</p>")

document.write("<p>Subscript: " + txt.sub() + "</p>")

document.write("<p>Superscript: " + txt.sup() + "</p>")

document.write("<p>Link: " + txt.link("http://www.baidu.com") + "</p>")

       相信學習過HTML的不還沒有忘記那些關鍵字吧,沒錯,就是對應的意思。

 

       indexOf() 用來測定字符串中的字符首次出現的位置

              document.write(str.indexOf("world"))

              結果爲:6

       match() 搜索字符串中的字符,找到返回此字符,否則返回null

              document.write(str.match("world!"))

              結果爲:world

       replace()替換目標字符

document.write(txt.replace(/World/,"BeiJing"))

結果爲:Hello BeiJing

 

       日期date對象

              Date對象用於處理日期和時間。

              顯示當前時間:

                     document.write(Date())

              設定時間:

                     var myDate=new Date(2008,7,9)

                     月份的參數是0-11,所以設置8月要寫成7

              gerTime() 獲得從1970到現在過了多少秒(計算機從1970開始初始值的)

                     var d = new Date();

 var t = d.getTime();

 var y = t/(1000*60*60*24*365);

 document.write(y);

以上計算了距離1970多少天。

              getDate()獲取當前日期。getYear()獲取當前年份,getMonth獲取當前月份,getDay獲取當前星期

                     var d = new Date();

 var t = d.getDate();

 document.write(t);

              setFullYear()設定年(月日),得到精確時間。

                     var d = new Date()

d.setFullYear(2011,2,14)  //可以只寫2011

document.write(d)

              getDay,如何顯示星期,而不是數字

                     var d=new Date()

var weekday=new Array(7)

weekday[0]="星期日"

weekday[1]="星期一"

weekday[2]="星期二"

weekday[3]="星期三"

weekday[4]="星期四"

weekday[5]="星期五"

weekday[6]="星期六"

document.write("今天是" + weekday[d.getDay()])

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章