📄 js_obj_date.asp@output=print
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JavaScript Date Object</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</head>
<body>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
<h1>JavaScript Date Object</h1>
<a href="js_obj_string.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_obj_array.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />
<p class="intro">The Date object is used to work with dates and times.</p>
<hr />
<h2>Examples</h2>
<p><a target="_blank" href="../jsref/tryit.asp@filename=tryjsref_date">Return today's date and time</a><br />
How to use the Date() method to get today's date.</p>
<p><a target="_blank" href="../jsref/tryit.asp@filename=tryjsref_gettime">getTime()</a><br />
Use getTime() to calculate the years since 1970.</p>
<p><a target="_blank" href="../jsref/tryit.asp@filename=tryjsref_setfullyear2">setFullYear()</a><br />
How to use setFullYear() to set a specific date.</p>
<p><a target="_blank" href="../jsref/tryit.asp@filename=tryjsref_toutcstring">toUTCString()</a><br />
How to use toUTCString() to convert today's date (according to UTC) to a string.</p>
<p><a target="_blank" href="../jsref/tryit.asp@filename=tryjsref_date_weekday">getDay()</a><br />
Use getDay() and an array to write a weekday, and not just a number.</p>
<p><a target="_blank" href="tryit.asp@filename=tryjs_timing_clock">Display a
clock</a><br />
How to display a clock on your web page.</p>
<hr />
<h2>Complete Date Object Reference</h2>
<p>For a complete reference of all the properties and methods that can be used with
the Date object, go to our <a href="../jsref/jsref_obj_date.asp">
complete Date object reference</a>.</p>
<p>The reference contains a brief description and examples of use for each
property and method!</p>
<hr />
<h2>Defining Dates</h2>
<p>The Date object is used to work with dates and times. </p>
<p>We define a Date object with the new keyword. The following code line defines
a Date object called myDate:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table1">
<tr>
<td>
<pre>var myDate=new Date()</pre>
</td>
</tr>
</table>
<p><b>Note:</b> The Date object will automatically hold the current date and
time as its initial value!</p>
<hr />
<h2>Manipulate Dates</h2>
<p>We can easily manipulate the date by
using the methods available for the Date object.</p>
<p>In the example below we set a Date object to a specific date (14th
January 2010):</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table5">
<tr>
<td>
<pre>var myDate=new Date();
myDate.setFullYear(2010,0,14);</pre>
</td>
</tr>
</table>
<p>And in the following example we set a Date object to be 5 days into the future:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table4">
<tr>
<td>
<pre>var myDate=new Date();
myDate.setDate(myDate.getDate()+5);</pre>
</td>
</tr>
</table>
<p><b>Note:</b> If adding five days to a date shifts the month or year, the changes are handled automatically by the
Date
object itself!</p>
<hr />
<h2>Comparing Dates</h2>
<p>The Date object is also used to compare two dates.</p>
<p>The following example compares today's date with the 14th
January 2010:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table3">
<tr>
<td>
<pre>var myDate=new Date();
myDate.setFullYear(2010,0,14);</pre>
<pre>var today = new Date();</pre>
<pre>if (myDate>today)
{
alert("Today is before 14th January 2010");
}
else
{
alert("Today is after 14th January 2010");
}</pre>
</td>
</tr>
</table>
<br />
<hr />
<a href="js_obj_string.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_obj_array.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -