JavaScript DHTML/Date Time/Date Calculation
Содержание
Append date to string
<source lang="html4strict">
<html> <head> <title></title> <script type="text/javascript">
var dt = Date(); var msg = "Today is " + dt; document.write(msg);
</script> </head> <body> </body> </html>
</source>
Calculate the days between two dates
<source lang="html4strict">
<html> <head>
<title>the date</title>
</head> <body>
<script type = "text/javascript"> var today = new Date(); var then = new Date(); then.setFullYear(3000,1,1); var diff = then.getTime() - today.getTime(); diff = Math.floor(diff / (1000 * 60 * 60 * 24)); var dateLoc = document.getElementById("dateField"); dateLoc.innerHTML = diff + " days"; </script>
</body> </html>
</source>
Date Object Calculations
<source lang="html4strict">
<html> <head> <title>Date Calculation</title> <script type="text/javascript"> function nextWeek() {
var todayInMS = today.getTime(); var nextWeekInMS = todayInMS + (60 * 60 * 24 * 7 * 1000); return new Date(nextWeekInMS);
} </script> </head> <body> Today is: <script type="text/javascript">
var today = new Date(); document.write(today);
</script>
Next week will be:
<script type="text/javascript">
document.write(nextWeek());
</script> </body> </html>
</source>
Get your timezone
<source lang="html4strict">
<html> <head>
</head> <body> <script type="text/javascript">
var today = new Date(); offset = (today.getTimezoneOffset() / 60) + 1; if (offset == 5) { alert("You are in the Eastern Timezone"); }
</script> </body> </html>
</source>
Putting a Time Stamp on a Page
<source lang="html4strict">
<html> <head> <title>Time Stamper</title> </head> <body> <script type="text/javascript"> update = new Date(document.lastModified); theMonth = update.getMonth() + 1; theDate = update.getDate(); theYear = update.getFullYear(); document.writeln("Last updated:" + theMonth + "/" + theDate + "/" + theYear + "<\/I>"); </script> </body> </html>
</source>