A. js倒計時精確到毫秒
Js獲取當前日期時間及其它操作
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( ); //獲取日期與時間
日期時間腳本庫方法列表
Date.prototype.isLeapYear 判斷閏年
Date.prototype.Format 日期格式化
Date.prototype.DateAdd 日期計算
Date.prototype.DateDiff 比較日期差
Date.prototype.toString 日期轉字元串
Date.prototype.toArray 日期分割為數組
Date.prototype.DatePart 取日期的部分信息
Date.prototype.MaxDayOfDate 取日期所在月的最大天數
Date.prototype.WeekNumOfYear 判斷日期所在年的第幾周
StringToDate 字元串轉日期型 IsValidDate 驗證日期有效性
CheckDateTime 完整日期時間檢查
daysBetween 日期天數差
B. 如何實現時間戳轉換
以前遇到過一個關於時間戳的問題,為了不被大家鄙視,先說一下概念。
具體時間戳怎麼定義的我也不清楚,但網路中有這么一句:「時間戳是自 1970 年 1 月 1 日(00:00:00 GMT)至當前時間的總秒數」。
按這個定義,編程語言中倒是有一種類似的函數,getTime(),但這個函數返回的是自1970年1月1日到當前時間的總 毫秒數 ,而不是總 秒數。
在js中,將一個字元轉化成Date型也不是什麼難事:
var str = '2013-08-30'; // 日期字元串
str = str.replace(/-/g,'/'); // 將-替換成/,因為下面這個構造函數只支持/分隔的日期字元串
var date = new Date(str); // 構造一個日期型數據,值為傳入的字元串
在上面,new Date(str)構造了一個日期,參數str至少要提供年月日三部分,也就是形如「2013/03/08」的字元串,不能是"2013/03",否則將得到一個NaN。此時構造出來的時間是:2013/03/08 00:00:00。同時你還可以傳入小時、分鍾和秒數,但不能只傳入小時,比如「2013/03/08 17」,這樣的參數同樣會得到一個NaN。參數可以是「2013/03/08 17:20」或者「2013/03/08 17:20:05」,這樣都可以得到正確的時間,其中如果秒數沒給出,則默認為0。
此時得到的是日期型數據,如果要得到上面所謂的時間戳,可以這樣:
var time = date.getTime();
這樣得到的是一個數值,表示的是從1970年1月1日0點0分0秒到date那一刻的毫秒數,如果把這個數字除以1000,就得到了秒數,同樣繼續除以60,得到分鍾,再除以60得到小時等等。
提示,通過這個getTime()函數,你可以得到兩個日期的毫秒數,繼而轉化成秒數、分鍾、小時甚至天數,比較兩個日期的差值。
C. js日期相減求天數 精確到秒
java">
varmyDate=newDate();
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();//獲取當前日期
varmytime=myDate.toLocaleTimeString();//獲取當前時間
myDate.toLocaleString();//獲取日期與時間
日期時間腳本庫方法列表
Date.prototype.isLeapYear判斷閏年
Date.prototype.Format日期格式化
Date.prototype.DateAdd日期計算
Date.prototype.DateDiff比較日期差
Date.prototype.toString日期轉字元串
Date.prototype.toArray日期分割為數組
Date.prototype.DatePart取日期的部分信息
Date.prototype.MaxDayOfDate取日期所在月的最大天數
Date.prototype.WeekNumOfYear判斷日期所在年的第幾周
StringToDate字元串轉日期型
IsValidDate驗證日期有效性
CheckDateTime完整日期時間檢查
daysBetween日期天數差
js代碼:
//---------------------------------------------------
//判斷閏年
//---------------------------------------------------
Date.prototype.isLeapYear=function()
{
return(0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0)));
}
//---------------------------------------------------
//日期格式化
//格式YYYY/yyyy/YY/yy表示年份
//MM/M月份
//W/w星期
//dd/DD/d/D日期
//hh/HH/h/H時間
//mm/m分鍾
//ss/SS/s/S秒
//---------------------------------------------------
Date.prototype.Format=function(formatStr)
{
varstr=formatStr;
varWeek=['日','一','二','三','四','五','六'];
str=str.replace(/yyyy|YYYY/,this.getFullYear());
str=str.replace(/yy|YY/,(this.getYear()%100)>9?(this.getYear()%100).toString():'0'+(this.getYear()%100));
str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0'+this.getMonth());
str=str.replace(/M/g,this.getMonth());
str=str.replace(/w|W/g,Week[this.getDay()]);
str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0'+this.getDate());
str=str.replace(/d|D/g,this.getDate());
str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0'+this.getHours());
str=str.replace(/h|H/g,this.getHours());
str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0'+this.getMinutes());
str=str.replace(/m/g,this.getMinutes());
str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0'+this.getSeconds());
str=str.replace(/s|S/g,this.getSeconds());
returnstr;
}
//+---------------------------------------------------
//|求兩個時間的天數差日期格式為YYYY-MM-dd
//+---------------------------------------------------
functiondaysBetween(DateOne,DateTwo)
{
varOneMonth=DateOne.substring(5,DateOne.lastIndexOf('-'));
varOneDay=DateOne.substring(DateOne.length,DateOne.lastIndexOf('-')+1);
varOneYear=DateOne.substring(0,DateOne.indexOf('-'));
varTwoMonth=DateTwo.substring(5,DateTwo.lastIndexOf('-'));
varTwoDay=DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf('-')+1);
varTwoYear=DateTwo.substring(0,DateTwo.indexOf('-'));
varcha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)-Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000);
returnMath.abs(cha);
}
//+---------------------------------------------------
//|日期計算
//+---------------------------------------------------
Date.prototype.DateAdd=function(strInterval,Number){
vardtTmp=this;
switch(strInterval){
case's':returnnewDate(Date.parse(dtTmp)+(1000*Number));
case'n':returnnewDate(Date.parse(dtTmp)+(60000*Number));
case'h':returnnewDate(Date.parse(dtTmp)+(3600000*Number));
case'd':returnnewDate(Date.parse(dtTmp)+(86400000*Number));
case'w':returnnewDate(Date.parse(dtTmp)+((86400000*7)*Number));
case'q':returnnewDate(dtTmp.getFullYear(),(dtTmp.getMonth())+Number*3,dtTmp.getDate(),dtTmp.getHours(),dtTmp.getMinutes(),dtTmp.getSeconds());
case'm':returnnewDate(dtTmp.getFullYear(),(dtTmp.getMonth())+Number,dtTmp.getDate(),dtTmp.getHours(),dtTmp.getMinutes(),dtTmp.getSeconds());
case'y':returnnewDate((dtTmp.getFullYear()+Number),dtTmp.getMonth(),dtTmp.getDate(),dtTmp.getHours(),dtTmp.getMinutes(),dtTmp.getSeconds());
}
}
//+---------------------------------------------------
//|比較日期差dtEnd格式為日期型或者有效日期格式字元串
//+---------------------------------------------------
Date.prototype.DateDiff=function(strInterval,dtEnd){
vardtStart=this;
if(typeofdtEnd=='string')//如果是字元串轉換為日期型
{
dtEnd=StringToDate(dtEnd);
}
switch(strInterval){
case's':returnparseInt((dtEnd-dtStart)/1000);
case'n':returnparseInt((dtEnd-dtStart)/60000);
case'h':returnparseInt((dtEnd-dtStart)/3600000);
case'd':returnparseInt((dtEnd-dtStart)/86400000);
case'w':returnparseInt((dtEnd-dtStart)/(86400000*7));
case'm':return(dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())*12)-(dtStart.getMonth()+1);
case'y':returndtEnd.getFullYear()-dtStart.getFullYear();
}
}
//+---------------------------------------------------
//|日期輸出字元串,重載了系統的toString方法
//+---------------------------------------------------
Date.prototype.toString=function(showWeek)
{
varmyDate=this;
varstr=myDate.toLocaleDateString();
if(showWeek)
{
varWeek=['日','一','二','三','四','五','六'];
str+='星期'+Week[myDate.getDay()];
}
returnstr;
}
//+---------------------------------------------------
//|日期合法性驗證
//|格式為:YYYY-MM-DD或YYYY/MM/DD
//+---------------------------------------------------
functionIsValidDate(DateStr)
{
varsDate=DateStr.replace(/(^s+|s+$)/g,'');//去兩邊空格;
if(sDate=='')returntrue;
//如果格式滿足YYYY-(/)MM-(/)DD或YYYY-(/)M-(/)DD或YYYY-(/)M-(/)D或YYYY-(/)MM-(/)D就替換為''
//資料庫中,合法日期可以是:YYYY-MM/DD(2003-3/21),資料庫會自動轉換為YYYY-MM-DD格式
vars=sDate.replace(/[d]{4,4}[-/]{1}[d]{1,2}[-/]{1}[d]{1,2}/g,'');
if(s=='')//說明格式滿足YYYY-MM-DD或YYYY-M-DD或YYYY-M-D或YYYY-MM-D
{
vart=newDate(sDate.replace(/-/g,'/'));
varar=sDate.split(/[-/:]/);
if(ar[0]!=t.getYear()||ar[1]!=t.getMonth()+1||ar[2]!=t.getDate())
{
//alert('錯誤的日期格式!格式為:YYYY-MM-DD或YYYY/MM/DD。注意閏年。');
returnfalse;
}
}
else
{
//alert('錯誤的日期格式!格式為:YYYY-MM-DD或YYYY/MM/DD。注意閏年。');
returnfalse;
}
returntrue;
}
//+---------------------------------------------------
//|日期時間檢查
//|格式為:YYYY-MM-DDHH:MM:SS
//+---------------------------------------------------
functionCheckDateTime(str)
{
varreg=/^(d+)-(d{1,2})-(d{1,2})(d{1,2}):(d{1,2}):(d{1,2})$/;
varr=str.match(reg);
if(r==null)returnfalse;
r[2]=r[2]-1;
vard=newDate(r[1],r[2],r[3],r[4],r[5],r[6]);
if(d.getFullYear()!=r[1])returnfalse;
if(d.getMonth()!=r[2])returnfalse;
if(d.getDate()!=r[3])returnfalse;
if(d.getHours()!=r[4])returnfalse;
if(d.getMinutes()!=r[5])returnfalse;
if(d.getSeconds()!=r[6])returnfalse;
returntrue;
}
//+---------------------------------------------------
//|把日期分割成數組
//+---------------------------------------------------
Date.prototype.toArray=function()
{
varmyDate=this;
varmyArray=Array();
myArray[0]=myDate.getFullYear();
myArray[1]=myDate.getMonth();
myArray[2]=myDate.getDate();
myArray[3]=myDate.getHours();
myArray[4]=myDate.getMinutes();
myArray[5]=myDate.getSeconds();
returnmyArray;
}
//+---------------------------------------------------
//|取得日期數據信息
//|參數interval表示數據類型
//|y年m月d日w星期ww周h時n分s秒
//+---------------------------------------------------
Date.prototype.DatePart=function(interval)
{
varmyDate=this;
varpartStr='';
varWeek=['日','一','二','三','四','五','六'];
switch(interval)
{
case'y':partStr=myDate.getFullYear();break;
case'm':partStr=myDate.getMonth()+1;break;
case'd':partStr=myDate.getDate();break;
case'w':partStr=Week[myDate.getDay()];break;
case'ww':partStr=myDate.WeekNumOfYear();break;
case'h':partStr=myDate.getHours();break;
case'n':partStr=myDate.getMinutes();break;
case's':partStr=myDate.getSeconds();break;
}
returnpartStr;
}
//+---------------------------------------------------
//|取得當前日期所在月的最大天數
//+---------------------------------------------------
Date.prototype.MaxDayOfDate=function()
{
varmyDate=this;
varary=myDate.toArray();
vardate1=(newDate(ary[0],ary[1]+1,1));
vardate2=date1.dateAdd(1,'m',1);
varresult=dateDiff(date1.Format('yyyy-MM-dd'),date2.Format('yyyy-MM-dd'));
returnresult;
}
//+---------------------------------------------------
//|取得當前日期所在周是一年中的第幾周
//+---------------------------------------------------
Date.prototype.WeekNumOfYear=function()
{
varmyDate=this;
varary=myDate.toArray();
varyear=ary[0];
varmonth=ary[1]+1;
varday=ary[2];
document.write('<scriptlanguage=VBScript>
');
document.write('myDate=Datue(''+month+'-'+day+'-'+year+'')
');
document.write('result=DatePart('ww',myDate)
');
document.write('
');
returnresult;
}
//+---------------------------------------------------
//|字元串轉成日期類型
//|格式MM/dd/YYYYMM-dd-YYYYYYYY/MM/ddYYYY-MM-dd
//+---------------------------------------------------
functionStringToDate(DateStr)
{
varconverted=Date.parse(DateStr);
varmyDate=newDate(converted);
if(isNaN(myDate))
{
//vardelimCahar=DateStr.indexOf('/')!=-1?'/':'-';
vararys=DateStr.split('-');
myDate=newDate(arys[0],--arys[1],arys[2]);
}
returnmyDate;
}
若要顯示:當前日期加時間(如:2009-06-1212:00)
functionCurentTime()
{
varnow=newDate();
varyear=now.getFullYear();//年
varmonth=now.getMonth()+1;//月
varday=now.getDate();//日
varhh=now.getHours();//時
varmm=now.getMinutes();//分
varclock=year+"-";
if(month<10)
clock+="0";
clock+=month+"-";
if(day<10)
clock+="0";
clock+=day+"";
if(hh<10)
clock+="0";
clock+=hh+":";
if(mm<10)clock+='0';
clock+=mm;
return(clock);
}
這是我博客里的一篇文章。上邊有清晰的思路。你自己改改。
D. js如何去當前時間前一天的時間
1、使用new Date()獲取當前日期,new Date().getTime()獲取當前毫秒數
2、計算公式,等於獲取的當前日期減去或者加上一天的毫秒數。一天的毫秒數的計算公式:24小時*60分鍾*60秒*1000毫秒,也是86400000毫秒。
舉例:
Date curDate = new Date();
var preDate = new Date(curDate.getTime() - 24*60*60*1000); //前一天
var nextDate = new Date(curDate.getTime() + 24*60*60*1000); //後一天
以下圖片使用後台輸出表示。
(4)jsstringtodata擴展閱讀
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( ); //獲取日期與時間
Date.prototype.isLeapYear 判斷閏年
Date.prototype.Format 日期格式化
Date.prototype.DateAdd 日期計算
Date.prototype.DateDiff 比較日期差
Date.prototype.toString 日期轉字元串
Date.prototype.toArray 日期分割為數組
Date.prototype.DatePart 取日期的部分信息
Date.prototype.MaxDayOfDate 取日期所在月的最大天數
Date.prototype.WeekNumOfYear 判斷日期所在年的第幾周
StringToDate 字元串轉日期型
IsValidDate 驗證日期有效性
CheckDateTime 完整日期時間檢查
daysBetween 日期天數差
E. 我要用JS寫個判斷時間的操作 就是去判斷今天是否過了 ,用系統時間去和00:00:00比較(時分秒)
<script language=JavaScript>
var timerID = null;
var timerRunning = false;
function stopclock (){
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;}
function startclock () {
stopclock();
showtime();}
function showtime () {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds()
var timeValue = "" +((hours >= 12) ? "下午 " : "上午 " )
timeValue += ((hours >12) ? hours -12 :hours)
timeValue += ((minutes < 10) ? ":0" : ":") + minutes
timeValue += ((seconds < 10) ? ":0" : ":") + seconds
document.clock.thetime.value = timeValue;
timerID = setTimeout("showtime()",1000);
timerRunning = true;}
</SCRIPT>
F. js日期轉換成字元串
<scripttype="text/javascript">
varnow=newDate();
varyear=now.getFullYear();
varmonth=(now.getMonth()+1).toString();
varday=(now.getDate()).toString();
if(month.length==1){
month="0"+month;
}
if(day.length==1){
day="0"+day;
}
vardateTime=year+month+day;
document.write(dateTime);
</script>
G. js 中怎麼獲取當前系統時間
系統時間一般是值服務端時間,js獲取服務端時間的方法是直接用獲取。
編寫顯示時間的頁面:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Server date/time</title>
<script language="javascript" src="serverDate.js"></script>
</head>
<script language="javascript">
var localTime = new Date();
document.write("Local machine time is: " + localTime + "<br>");
document.write("Server time is: " + date);
</script>
<body>
</body>
ajax腳本獲取server的時間
var xmlHttp;
function srvTime(){
try {
//創建xmlHttp對象
xmlHttp = new XMLHttpRequest();
}
catch (err1) {
//ie瀏覽器
try {
xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (err2) {
try {
xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (eerr3) {
//ajax不支持
alert("AJAX not supported");
}
}
}
//打開xmlHttp請求
xmlHttp.open('HEAD',window.location.href.toString(),false);
//設置xmlHttp請求頭
xmlHttp.setRequestHeader("Content-Type", "text/html");
//發送請求
xmlHttp.send('');
// 獲取response中的Date參數
return xmlHttp.getResponseHeader("Date");
}
var st = srvTime(); //伺服器時間賦值給st變數
var date = new Date(st); //轉換js的date對象
// 輸出伺服器時間
document.write("伺服器時間: " + date);
JavaScript一種直譯式腳本語言,是一種動態類型、弱類型、基於原型的語言,內置支持類型。它的解釋器被稱為JavaScript引擎,為瀏覽器的一部分,廣泛用於客戶端的腳本語言,最早是在HTML(標准通用標記語言下的一個應用)網頁上使用,用來給HTML網頁增加動態功能。
語句:JavaScript程序是由若干語句組成的,語句是編寫程序的指令。JavaScript提供了完整的基本編程語句,
它們是:賦值語句、switch選擇語句、while循環語句、for循環語句、for each循環語句、do...while循環語句、break循環中止語句、continue循環中斷語句、with語句、try…catch語句、if語句(if..else,if…else if…)。