JavaScript DHTML/Date Time/Date Get
Содержание
- 1 Convert Date to Locale String()
- 2 Get Day
- 3 Get Full Year
- 4 Get Milliseconds
- 5 Get Month
- 6 Get Seconds
- 7 Get Time
- 8 Get Year
- 9 Local time and local date
- 10 Make the time readable
- 11 Output date and time in a human readble form
- 12 Readable date value
- 13 To Date Source
- 14 Use toPrecision to set the date precision
- 15 Value Of a Date
Convert Date to Locale String()
<source lang="html4strict">
<html> <body> <button onclick="var myDate = new Date(); alert(myDate.toLocaleString());">Date toLocaleString</button> </body> </html>
</source>
Get Day
<source lang="html4strict">
<html> <body> <button onclick="var myDate = new Date(); alert(myDate.getDay());">Date: get Day</button> </body> </html>
</source>
Get Full Year
<source lang="html4strict">
<html> <body> <button onclick="var myDate = new Date();
alert(myDate.getFullYear());"> Date: get Full Year</button>
</body> </html>
</source>
Get Milliseconds
<source lang="html4strict">
<html> <body> <button onclick="var myDate = new Date();
alert(myDate.getMilliseconds());"> Date: get Milliseconds</button>
</body> </html>
</source>
Get Month
<source lang="html4strict">
<html> <body> <button onclick="var myDate = new Date(); alert(myDate.getMonth());"> Date: get Month </button> </body> </html>
</source>
Get Seconds
<source lang="html4strict">
<html> <body> <button onclick="var myDate = new Date(); alert(myDate.getSeconds());"> Date: get Seconds </button> </body> </html>
</source>
Get Time
<source lang="html4strict">
<html> <body> <button onclick="var myDate = new Date(); alert(myDate.getTime());"> Date: get Time </button> </body> </html>
</source>
Get Year
<source lang="html4strict">
<html> <body> <button onclick="var myDate = new Date(); alert(myDate.getYear());">Date: GETYEAR</button> </body> </html>
</source>
Local time and local date
<source lang="html4strict">
<html> <head>
<title>the date</title>
</head> <body>
<script type = "text/javascript"> var myDate = new Date(); var dateString = myDate.toLocaleDateString() + " " + myDate.toLocaleTimeString(); var dateLoc = document.getElementById("dateField"); dateLoc.innerHTML = "Now: " + dateString; </script>
</body> </html>
</source>
Make the time readable
<source lang="html4strict">
<html> <head> <title>A Simple Page</title> <script language="JavaScript"> function gettime() {
var date= new Date(); var hr = date.getHours(); var m = date.getMinutes(); var s = date.getSeconds(); if(m < 10) { m = "0" + m } if(s < 10) { s = "0" + s } document.clockform.clock.value = hr + ":" + m + ":" + s; setTimeout("gettime()",100)
} </script> </head> <body onload="gettime()"> <form name="clockform"> <input type="text" name="clock"> </form> </body> </html>
</source>
Output date and time in a human readble form
<source lang="html4strict">
<html> <head> <title>A Simple Page</title> <script language="JavaScript"> function gettime() {
var date= new Date(); var hr = date.getHours(); var m = date.getMinutes(); var s = date.getSeconds(); var ampm="AM"; if (hr > 11) { ampm="PM" } if (hr > 12) { hr -= 12 } if(m < 10) { m = "0" + m } if(s < 10) { s = "0" + s } document.clockform.clock.value = hr + ":" + m + ":" + s + " " +ampm; setTimeout("gettime()",100)
} </script> </head> <body onload="gettime()"> <form name="clockform"> <input type="text" name="clock"> </form> </body> </html>
</source>
Readable date value
<source lang="html4strict">
<HTML> <HEAD> <SCRIPT language="JavaScript"> function theclock(){
var rightnow= new Date(); var month= rightnow.getMonth(); var today= rightnow.getDate(); var year= rightnow.getYear(); var hour= rightnow.getHours(); var min= rightnow.getMinutes(); var second= rightnow.getSeconds(); month+=1; if (year<2000) year+=1900; if (hour<10) hour="0"+hour; if (min<10) min="0"+min; if (second<10) second="0"+second; if (hour<12) var ampm="AM"; else var ampm="PM"; if (hour==0) hour=12; if (hour>=13) hour-=12; document.write( month+"/"+today+"/"+year+" "+hour+":"+min+":"+second+" "+ampm);
} setInterval("theclock()",1000); </SCRIPT> </HEAD> <BODY> </BODY> </HTML>
</source>
To Date Source
<source lang="html4strict">
<html> <body> <button onclick="var myA = new Array(10,11,12,13,14,15);
alert(myA.toSource());">Array TOSOURCE</button>
<button onclick="var myDate = new Date(); alert(myDate.toSource());"> Date toSource </button> </body> </html>
</source>
Use toPrecision to set the date precision
<source lang="html4strict">
<html> <head>
<title>the date</title> <script type = "text/javascript"> var started = new Date(); var now = started.getTime(); </script>
</head> <body>
<script type = "text/javascript"> var bottom = new Date(); var diff = (bottom.getTime() - now)/1000; var finaltime = diff.toPrecision(5); var dateLoc = document.getElementById("dateField"); dateLoc.innerHTML = "Time: " + finaltime + " seconds."; </script>
</body> </html>
</source>
Value Of a Date
<source lang="html4strict">
<html> <body> <button onclick="var myDate = new Date(); alert(myDate.valueOf());">valueOf</button> </body> </html>
</source>