JavaScript DHTML/Date Time/Date Get

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

Convert Date to Locale String()

  
    
<html>
<body>
<button onclick="var myDate = new Date(); 
alert(myDate.toLocaleString());">Date toLocaleString</button>
</body>
</html>



Get Day

  
    
<html>
<body>
<button onclick="var myDate = new Date(); alert(myDate.getDay());">Date: get Day</button>
</body>
</html>



Get Full Year

  
    
<html>
<body>
<button onclick="var myDate = new Date(); 
                 alert(myDate.getFullYear());">
                 Date: get Full Year</button>
</body>
</html>



Get Milliseconds

  
    
<html>
<body>
<button onclick="var myDate = new Date(); 
                 alert(myDate.getMilliseconds());">
                 Date: get Milliseconds</button>
</body>
</html>



Get Month

  
    
<html>
<body>
<button onclick="var myDate = new Date(); alert(myDate.getMonth());">
Date: get Month
</button>
</body>
</html>



Get Seconds

  
    
<html>
<body>
<button onclick="var myDate = new Date(); alert(myDate.getSeconds());">
Date: get Seconds
</button>
</body>
</html>



Get Time

  
    
<html>
<body>
<button onclick="var myDate = new Date(); alert(myDate.getTime());">
Date: get Time
</button>
</body>
</html>



Get Year

  
    
<html>
<body>
<button onclick="var myDate = new Date(); 
alert(myDate.getYear());">Date: GETYEAR</button>
</body>
</html>



Local time and local date

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



Make the time readable

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



Output date and time in a human readble form

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



Readable date value

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



To Date Source

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



Use toPrecision to set the date precision

 
<html>
<head>
    <title>the date</title>
    <script type = "text/javascript">
    var started = new Date();
    var now = started.getTime();
    </script>
</head>
<body>
    <p id="dateField"></p>
    <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>



Value Of a Date

  
    
<html>
<body>
<button onclick="var myDate = new Date(); alert(myDate.valueOf());">valueOf</button>
</body>
</html>