JS date对象

Date对象用于处理日期和时间。

Date():返回当日的日期和时间。
getDate():从Date()对象中返回一个月中的某一天(1-31)。
getDay():从Date()对象中返回一周中的某一天(0-6)。
getMonth():从Date()对象中返回月份(0-11)。
getFullYear():从Date()对象中返回四位数字的年份。
getHours():从Date()对象中返回小时(0-23)。
getMinutes():从Date()对象中返回分钟(0-59)。
getSeconds():从Date()对象中返回秒数(0-59)。
getMilliseconds():从Date()对象中返回毫秒数(0-999)。

getTime():返回1970年1月1日至今的毫秒数。
parse():返回1970年1月1日到指定日期的毫秒数。
setTime():以毫秒数设置Date对象。

setDate():设置Date()对象中月的某一天(1-31)。
setMonth():设置Date()对象中的月份(0-11)。
setFullYear():设置Date()对象中的年份(四位数字)。
setHours():设置Date()对象中的小时(0-23)。
setMinutes():设置Date()对象中的分钟(0-59)。
setSeconds():设置Date()对象中的秒数(0-59)。
setMilliseconds():设置Date()对象中的毫秒数(0-999)。

toString():把Date()对象转换为字符串。
toTimeString():把Date()对象的时间部分转换成字符串。
toDateString():把Date()对象的日期部分转成字符串.
toLocaleString():根据本地时间格式,把 Date 对象转换为字符串。

js中由Date向Long转化:
var begin="2013-03-12";
var num1 = Date.parse(begin) / 1000;

案例截图:

源代码:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" Content="text/html; charset=utf-8;">
 5 <title> JS date对象 </title>
 6 <meta name="author" content="rainna" />
 7 <meta name="keywords" content="rainna's js" />
 8 <meta name="description" content="JS date对象" />
 9 <style>
10 .wrap{width:600px;margin:0 auto 150px;line-height:1.8;font-family:Microsoft yahei;}
11 </style>
12 </head>
13 
14 <body>
15 <div class="wrap" id="date">
16     <label>当前日期:</label><input type="text" /><br /><br />
17     <label>昨天:</label><input type="text"/>
18     <label>前天:</label><input type="text"/><br />
19     <label>明天:</label><input type="text"/>
20     <label>后天:</label><input type="text"/><br /><br />
21     <label>上月:</label><input type="text"/>
22     <label>下月:</label><input type="text"/><br />
23     <label>去年:</label><input type="text"/>
24     <label>明年:</label><input type="text"/><br /><br />
25     <label>请输入天数:</label><input type="text"/>天后的日期为<input type="text"/>
26 </div>
27 
28 <script>
29 var dd = function(){
30     //公共函数
31     function T$(id){
32         return document.getElementById(id);
33     }
34     function T$$(root,tag){
35         return (root||document).getElementsByTagName(tag);
36     }
37     
38     var showDate = function(_addDay,_addMonth,_addYear){   
39         var dd = new Date();
40         if(!!_addDay) dd.setDate(dd.getDate() + _addDay);
41         if(!!_addMonth) dd.setMonth(dd.getMonth() + _addMonth);
42         if(!!_addYear) dd.setFullYear(dd.getFullYear() + _addYear);
43         var newdd = dd.getFullYear() + '-' + (dd.getMonth() + 1) + '-' + dd.getDate();
44         return newdd;
45     };
46     
47     return function(){
48         var currdateIpt,yesterdayIpt,daybeforeIpt,tomorrowIpt,dayafterIpt,lastmonthIpt,nextmonthIpt,lastyearIpt,nextyearIpt,adddayIpt,newdateIpt,i = 0;
49         currdateIpt = T$$(T$('date'),'input')[i++];
50         yesterdayIpt = T$$(T$('date'),'input')[i++];
51         daybeforeIpt = T$$(T$('date'),'input')[i++];
52         tomorrowIpt = T$$(T$('date'),'input')[i++];
53         dayafterIpt = T$$(T$('date'),'input')[i++];
54         lastmonthIpt = T$$(T$('date'),'input')[i++];
55         nextmonthIpt = T$$(T$('date'),'input')[i++];
56         lastyearIpt = T$$(T$('date'),'input')[i++];
57         nextyearIpt = T$$(T$('date'),'input')[i++];
58         adddayIpt = T$$(T$('date'),'input')[i++];
59         newdateIpt = T$$(T$('date'),'input')[i++];
60 
61         currdateIpt.value = showDate(0,0,0);
62         yesterdayIpt.value = showDate(-1,0,0);
63         daybeforeIpt.value = showDate(-2,0,0);
64         tomorrowIpt.value = showDate(1,0,0);
65         dayafterIpt.value = showDate(2,0,0);
66         lastmonthIpt.value = showDate(0,-1,0);
67         nextmonthIpt.value = showDate(0,1,0);
68         lastyearIpt.value = showDate(0,0,-1);
69         nextyearIpt.value = showDate(0,0,1);
70         adddayIpt.onblur = function(){
71             newdateIpt.value = showDate(parseInt(adddayIpt.value),0,0);
72         }
73     }
74 }();
75 
76 dd();
77 </script>
78 </body>
79 </html>

 

posted @ 2014-07-28 19:40  ZRainna  阅读(821)  评论(0编辑  收藏  举报