JavaScript DHTML/Date Time/Date Calculation

Материал из Web эксперт
Перейти к: навигация, поиск

Append date to string

   

<html>
<head>
<title></title>
<script type="text/javascript">
   var dt = Date();
   var msg = "Today is " + dt;
   document.write(msg);
</script>
</head>
<body>
</body>
</html>



Calculate the days between two dates

  
<html>
<head>
    <title>the date</title>
</head>
<body>
    <p id="dateField">&nbsp;</p>
    
    <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>



Date Object Calculations

   
<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> 
<br> 
Next week will be: 
<script type="text/javascript"> 
    document.write(nextWeek()); 
</script> 
</body> 
</html>



Get your timezone

   
<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>



Putting a Time Stamp on a Page

   
<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("<I>Last updated:" + theMonth + "/" + theDate + "/" + theYear + "<\/I>"); 
</script> 
</body> 
</html>