四虎精品视频-四虎精品成人免费网站-四虎黄色网-四虎国产视频-国产免费91-国产蜜臀97一区二区三区

JavaScript Date對象使用總結

//全局函數
Date
//Date 類的靜態方法
Date.parse
Date.UTC
//Date 對象的建立方法
new Date()
new Date(毫秒數)
new Date(標準時間格式字符串)
new Date(年, 月, 日, 時, 分, 秒, 毫秒)
//Date 對象的更多方法
getFullYear (getUTCFullYear)
getMonth (getUTCMonth)
getDate (getUTCDate)
getDay (getUTCDay)
getHours (getUTCHours)
getMinutes (getUTCMinutes)
getSeconds (getUTCSeconds)
getMilliseconds (getUTCMilliseconds)
getTime
getTimezoneOffset
setFullYear (setUTCFullYear)
setMonth (setUTCMonth)
setDate (setUTCDate)
setHours (setUTCHours)
setMinutes (setUTCMinutes)
setSeconds (setUTCSeconds)
setMilliseconds (setUTCMilliseconds)
setTime
toDateString
toTimeString
toUTCString
toLocaleString
toLocaleDateString
toLocaleTimeString
toString
valueOf
--------------------------------------------------------------------------------
根據一個或多個數值建立時間對象, 及本地計時與世界標準計時的區別
--------------------------------------------------------------------------------
//先用最容易理解的方式建立一個時間對象
var d = new Date(2009, 2, 27, 12, 59, 59, 999);
alert(d); //Fri Mar 27 12:59:59 UTC+0800 2009
alert(d.toString()); //Fri Mar 27 12:59:59 UTC+0800 2009
alert(d.toUTCString()); //Fri, 27 Mar 2009 04:59:59 UTC
alert(d.toLocaleString()); //2009年3月27日 12:59:59
alert(d.toDateString()); //Fri Mar 27 2009
alert(d.toLocaleDateString()); //2009年3月27日
alert(d.toTimeString()); //12:59:59 UTC+0800
alert(d.toLocaleTimeString()); //12:59:59
/* 時間在計算機中被記為一個整數, 這是從 UTC 時間的 1970-1-1 0:0:0 到此時間的毫秒數 */
alert(d.valueOf()); //1238129999999
alert(d.getTime()); //1238129999999
/* 獲取本地時間和世界標準計時的時差 */
alert(d.getTimezoneOffset()); //-480; 單位是分鐘, 也就是 8 小時
/* 直接使用時間值(毫秒數, 譬如上面的: 1238129999999)建立時間對象 */
var d = new Date(1238129999999);
alert(d.toLocaleString()); //2009年3月27日 12:59:59
/* 但建立函數有 2-7 個參數時, 將是根據 "年, 月, 日, 時, 分, 秒, 毫秒" 建立時間 */
d = new Date(2009, 2, 27, 12, 59, 59, 999);
alert(d.toLocaleString()); //2009年3月27日 12:59:59
d = new Date(2009, 2, 27, 12, 59, 59);
alert(d.toLocaleString()); //2009年3月27日 12:59:59
d = new Date(2009, 2, 27, 12, 59);
alert(d.toLocaleString()); //2009年3月27日 12:59:00
d = new Date(2009, 2, 27, 12);
alert(d.toLocaleString()); //2009年3月27日 12:00:00
d = new Date(2009, 2, 27);
alert(d.toLocaleString()); //2009年3月27日 0:00:00
d = new Date(2009, 2);
alert(d.toLocaleString()); //2009年3月1日 0:00:00
/* Date 類的靜態函數 UTC 的參數也是和上面一樣的 2-7 個, 但 UTC 獲取的是個 number */
var n = Date.UTC(2009, 0); //這只是獲取了那個表示時間的毫秒數
alert(typeof n); //number
var d = new Date(n); //根據剛剛獲取的數、重新建立為時間對象
alert(d.toUTCString()); //Thu, 1 Jan 2009 00:00:00 UTC
alert(d.toLocaleString()); //2009年1月1日 8:00:00
--------------------------------------------------------------------------------
無參數建立的時間對象、和用全局函數 Date 獲取的時間的區別
--------------------------------------------------------------------------------
var d1 = new Date(); //返回時間對象
var d2 = Date(); //返回時間字符串
alert(d1); //Fri Feb 27 13:20:58 UTC+0800 2009
alert(d2); //Fri Feb 27 13:20:58 2009
alert(d1.valueOf()); //1235712058340
alert(d2.valueOf()); //Fri Feb 27 13:20:58 2009
alert(typeof d1); //object
alert(typeof d2); //string
//明顯看出 d2 只是字符串, 它可以使用 String 類的方法, 而不能使用 Date 類的方法.
--------------------------------------------------------------------------------
使用字符串參數建立時間對象
--------------------------------------------------------------------------------
var d;
d = new Date('Fri Mar 27 12:59:59 UTC+0800 2009');
alert(d.toLocaleString()); //2009年3月27日 12:59:59
d = new Date('Fri Mar 27 12:59:59 2009');
alert(d.toLocaleString()); //2009年3月27日 12:59:59
d = new Date('Fri Mar 27 2009');
alert(d.toLocaleString()); //2009年3月27日 0:00:00
d = new Date('Mar 27 2009');
alert(d.toLocaleString()); //2009年3月27日 0:00:00
/* 可使用 Date() 返回的字符串 */
var ts = Date();
d = new Date(ts);
alert(d.toLocaleString()); //2009年3月27日 14:04:38
/* Date 類的靜態函數 parse 也是需要一樣的字符參數, 不過它返回的是個數字(那個毫秒數) */
var n;
n = Date.parse('Mar 27 2009');
alert(n); //1238083200000
alert(typeof n); //number
d = new Date(n);
alert(d.toLocaleString()); //2009年3月27日 0:00:00
--------------------------------------------------------------------------------
分別獲取和設置: 年、月、日、時、分、秒、毫秒, 其中 "星期幾" 可獲取但不能設置
--------------------------------------------------------------------------------
var d = new Date(2009, 2, 27, 12, 58, 59, 999);
alert(d.toLocaleString()); //2009年3月27日 0:00:00
alert(d.getFullYear()); //2009
alert(d.getMonth()); //2 (從 0 起, 2 指 3 月)
alert(d.getDate()); //27
alert(d.getDay()); //5 (星期五)
alert(d.getHours()); //12
alert(d.getMinutes()); //58
alert(d.getSeconds()); //59
alert(d.getMilliseconds()); //999 (毫秒)
d.setFullYear(2010);
d.setMonth(1);
d.setDate(2);
d.setHours(3);
d.setMinutes(4);
d.setSeconds(5);
d.setMilliseconds(6);
alert(d.toLocaleString()); //2010年2月2日 3:04:05
alert(d.getFullYear()); //2010
alert(d.getMonth()); //1 (從 0 起, 1 指 2 月)
alert(d.getDate()); //2
alert(d.getDay()); //2 (星期二)
alert(d.getHours()); //3
alert(d.getMinutes()); //4
alert(d.getSeconds()); //5
alert(d.getMilliseconds()); //6 (毫秒)
/* 還有一個 setTime */
var d = new Date();
d.setTime(0);
alert(d.toUTCString()); //Thu, 1 Jan 1970 00:00:00 UTC

JavaScript技術JavaScript Date對象使用總結,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 禁忌爱情| 白事专用歌曲100首| 苏小懒| 黑帆第三季电视剧完整免费观看高清 | 孕妇电视剧| 黑凤凰电视剧剧情介绍| 李天方| 学校急招水电工一名| 变形金刚7免费高清电影| 任喜宝| 六年级上册美术教案人教版| 美女被吃掉| 下载抖音app| 樱井步| 加入社团的个人简历怎么写| 黄色网址在线免费播放| 低糖食物一览表| 大追捕在线完整免费观看| 被抛弃的青春1982| 秦腔《铡美案》全本| 日本无毛| 想要更多| 电视剧暗夜与黎明剧情介绍| 女同性恨| 格伦鲍威尔| 巨乳姐妹| 蛇蝎美人第四季| www.56.com| 许半夏电视剧在线观看| cctv5+体育赛事直播时间| 家书1000字| 冲锋衣品牌排行榜| 火花 电影| 肥皂泡节选阅读理解答案三年级| kyo| 搜狐手机网首页新闻| 北京卫视今天全部节目表| 最佳女婿 电影| 皮囊之下| 最爱电影完整版在线观看免费高清 | 风筝 电影|