原生態javascript擷取日期
複製代碼 代碼如下:<SCRIPT LANGUAGE="JavaScript">
var myDate = new Date();
myDate.getYear(); //擷取當前年份(2位)
myDate.getFullYear(); //擷取完整的年份(4位,1970-????)
myDate.getMonth(); //擷取當前月份(0-11,0代表1月)
myDate.getDate(); //擷取當前日(1-31)
myDate.getDay(); //擷取當前星期X(0-6,0代表星期天)
myDate.getTime(); //擷取目前時間(從1970.1.1開始的毫秒數)
myDate.getHours(); //擷取當前小時數(0-23)
myDate.getMinutes(); //擷取當前分鐘數(0-59)
myDate.getSeconds(); //擷取當前秒數(0-59)
myDate.getMilliseconds(); //擷取當前毫秒數(0-999)
myDate.toLocaleDateString(); //擷取當前日期
var mytime=myDate.toLocaleTimeString(); //擷取目前時間
myDate.toLocaleString( ); //擷取日期與時間
if (mytime<"23:30:00")
{
alert(mytime);
}
</SCRIPT>
基於jquery擷取日期複製代碼 代碼如下:<!DOCTYPE html>
<html class="no-js">
<head>
<meta charset="UTF-8">
<title>當期時間</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<style type="text/css">
.date{height:100px;line-height:100px;margin:0 auto;font-family:Arial, Helvetica, sans-serif;font-size:40px;
border:solid 1px #ccc;box-shadow:0 0 3px rgba(0,0,0,0.3) inset;border-radius:5px;text-shadow:1px 1px 2px rgba(0,0,0,0.3);}
.date strong{padding:0 10px;display:inline-block;height:100px;border-right:solid 1px #ccc;}
</style>
</head>
<body>
<h1>js簡單的擷取當前日期和時間</h1>
<p class="date">
<strong id="Y"></strong>
<strong id="MH"></strong>
<strong id="TD"></strong>
<strong id="D"></strong>
<strong id="H"></strong>
<strong id="M"></strong>
<strong id="S"></strong>
</p>
<script type="text/javascript">
$(document).ready(function(){
setInterval(showTime, 1000);
function timer(obj,txt){
obj.text(txt);
}
function showTime(){
var today = new Date();
var weekday=new Array(7)
weekday[0]="星期一"
weekday[1]="星期二"
weekday[2]="星期三"
weekday[3]="星期四"
weekday[4]="星期五"
weekday[5]="星期六"
weekday[6]="星期日"
var y=today.getFullYear()+"年";
var month=today.getMonth()+"月";
var td=today.getDate()+"日";
var d=weekday[today.getDay()];
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
timer($("#Y"),y);
timer($("#MH"),month);
timer($("#TD"),td);
timer($("#D"),d);
timer($("#H"),h);
timer($("#M"),m);
timer($("#S"),s);
}
})
</script>
</body>
</html>
可判斷早上好晚上好代碼複製代碼 代碼如下:<script language="JavaScript">
//定義一個tick函數,以擷取系統的時間
function tick()
{
var year,month,day,hours,minutes,seconds,ap;
var intYear,intMonth,intDay,intHours,intMinutes,intSeconds;
var today;
today=new Date();
intYear=today.getYear();
intMonth=today.getMonth()+1;
intDay=today.getDate();
intHours=today.getHours();
intMinutes=today.getMinutes();
intSeconds=today.getSeconds();
//擷取系統時間的小時數
if(intHours==0)
{
hours=intHours+":";
ap="淩晨";
}
else if(intHours<12)
{
hours=intHours+":";
ap="早晨";
}
else if(intHours==12)
{
hours=intHours+":";
ap="中午";
}
else
{
intHours=intHours-12;
hours=intHours+":";
ap="下午";
}
//擷取系統時間的分數
if(intMinutes<10)
{
minutes="0"+intMinutes+":";
}
else
minutes=intMinutes+":";
//擷取系統時間的秒數
if(intSeconds<10)
seconds="0"+intSeconds+" ";
else
seconds=intSeconds+" ";
timeString=year+month+day+hours+minutes+seconds+ap;
Clock.innerHTML=timeString;
//每隔0.1秒鐘執行一次tick函數
window.setTimeout("tick()",100);
}
window.onload=tick;
</script>
下面這句是調用函數
<div id="Clock"></div>