獲取客戶端電腦日期時間js代碼(jquery)
更新時間:2012年09月12日 15:28:01 作者:
在js或jquery中獲取客戶端的日期時間都是非常簡單的,下面我們來看看它們分別介紹吧
原生態(tài)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開始的毫秒數(shù))
myDate.getHours(); //獲取當前小時數(shù)(0-23)
myDate.getMinutes(); //獲取當前分鐘數(shù)(0-59)
myDate.getSeconds(); //獲取當前秒數(shù)(0-59)
myDate.getMilliseconds(); //獲取當前毫秒數(shù)(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函數(shù),以獲取系統(tǒng)的時間
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();
//獲取系統(tǒng)時間的小時數(shù)
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="下午";
}
//獲取系統(tǒng)時間的分數(shù)
if(intMinutes<10)
{
minutes="0"+intMinutes+":";
}
else
minutes=intMinutes+":";
//獲取系統(tǒng)時間的秒數(shù)
if(intSeconds<10)
seconds="0"+intSeconds+" ";
else
seconds=intSeconds+" ";
timeString=year+month+day+hours+minutes+seconds+ap;
Clock.innerHTML=timeString;
//每隔0.1秒鐘執(zhí)行一次tick函數(shù)
window.setTimeout("tick()",100);
}
window.onload=tick;
</script>
下面這句是調(diào)用函數(shù)
<div id="Clock"></div>
復制代碼 代碼如下:
<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開始的毫秒數(shù))
myDate.getHours(); //獲取當前小時數(shù)(0-23)
myDate.getMinutes(); //獲取當前分鐘數(shù)(0-59)
myDate.getSeconds(); //獲取當前秒數(shù)(0-59)
myDate.getMilliseconds(); //獲取當前毫秒數(shù)(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函數(shù),以獲取系統(tǒng)的時間
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();
//獲取系統(tǒng)時間的小時數(shù)
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="下午";
}
//獲取系統(tǒng)時間的分數(shù)
if(intMinutes<10)
{
minutes="0"+intMinutes+":";
}
else
minutes=intMinutes+":";
//獲取系統(tǒng)時間的秒數(shù)
if(intSeconds<10)
seconds="0"+intSeconds+" ";
else
seconds=intSeconds+" ";
timeString=year+month+day+hours+minutes+seconds+ap;
Clock.innerHTML=timeString;
//每隔0.1秒鐘執(zhí)行一次tick函數(shù)
window.setTimeout("tick()",100);
}
window.onload=tick;
</script>
下面這句是調(diào)用函數(shù)
<div id="Clock"></div>
您可能感興趣的文章:
- jquery 日期分離成年月日的代碼
- jQuery實現(xiàn)倒計時(倒計時年月日可自己輸入)
- jQuery實現(xiàn)動態(tài)生成年月日級聯(lián)下拉列表示例
- jquery中實現(xiàn)時間戳與日期相互轉(zhuǎn)換
- jQuery DateTimePicker 日期和時間插件示例
- jQuery帶時間的日期控件代碼分享
- jQuery移動端日期(datedropper)和時間(timedropper)選擇器附源碼下載
- jquery仿蘋果的時間/日期選擇效果
- jQuery時間日期三級聯(lián)動(推薦)
- 貼近用戶體驗的Jquery日期、時間選擇插件
- jQuery時間戳和日期相互轉(zhuǎn)換操作示例
- JavaScript自動生成 年月范圍 選擇功能完整示例【基于jQuery插件】
相關文章
JS 操作日期 順便實現(xiàn) 上一周 和 下一周 功能
JS本身沒有提供日期加減操作能力,所以借此寫了幾個方法,用以操作日期,主要是實現(xiàn)日期的加減2009-10-10Javascript 倒計時源代碼.(時.分.秒) 詳細注釋版
基于js的倒計時實現(xiàn)代碼,并有詳細的注釋比較適合學習使用。2011-05-05JavaScript自定義DateDiff函數(shù)(兼容所有瀏覽器)
JavaScript自定義DateDiff函數(shù)兼容所有瀏覽器,DateDiff用來返回兩個日期的時間間隔2012-03-03JAVASCRIPT 實現(xiàn)普通日期轉(zhuǎn)換多少小時前、多少分鐘前、多少秒
貌似最近很流行這個,就寫了個js函數(shù)實現(xiàn)之2009-05-05