JS 取當(dāng)前日期、時(shí)間的代碼 收藏 1.JS 取當(dāng)前日期,、時(shí)間的代碼?
--------------------------------------------------------------------------------
view plaincopy to clipboardprint? <SCRIPT LANGUAGE="JavaScript"> <!-- var myDate = new Date(); myDate.getYear(); //獲取當(dāng)前年份(2位) myDate.getFullYear(); //獲取完整的年份(4位,1970-????) myDate.getMonth(); //獲取當(dāng)前月份(0-11,0代表1月) myDate.getDate(); //獲取當(dāng)前日(1-31) myDate.getDay(); //獲取當(dāng)前星期X(0-6,0代表星期天) myDate.getTime(); //獲取當(dāng)前時(shí)間(從1970.1.1開(kāi)始的毫秒數(shù)) myDate.getHours(); //獲取當(dāng)前小時(shí)數(shù)(0-23) myDate.getMinutes(); //獲取當(dāng)前分鐘數(shù)(0-59) myDate.getSeconds(); //獲取當(dāng)前秒數(shù)(0-59) myDate.getMilliseconds(); //獲取當(dāng)前毫秒數(shù)(0-999) myDate.toLocaleDateString(); //獲取當(dāng)前日期 var mytime=myDate.toLocaleTimeString(); //獲取當(dāng)前時(shí)間 myDate.toLocaleString( ); //獲取日期與時(shí)間 if (mytime<"23:30:00") { alert(mytime); } //--> </SCRIPT> <SCRIPT LANGUAGE="JavaScript"> <!-- var myDate = new Date(); myDate.getYear(); //獲取當(dāng)前年份(2位) myDate.getFullYear(); //獲取完整的年份(4位,1970-????) myDate.getMonth(); //獲取當(dāng)前月份(0-11,0代表1月) myDate.getDate(); //獲取當(dāng)前日(1-31) myDate.getDay(); //獲取當(dāng)前星期X(0-6,0代表星期天) myDate.getTime(); //獲取當(dāng)前時(shí)間(從1970.1.1開(kāi)始的毫秒數(shù)) myDate.getHours(); //獲取當(dāng)前小時(shí)數(shù)(0-23) myDate.getMinutes(); //獲取當(dāng)前分鐘數(shù)(0-59) myDate.getSeconds(); //獲取當(dāng)前秒數(shù)(0-59) myDate.getMilliseconds(); //獲取當(dāng)前毫秒數(shù)(0-999) myDate.toLocaleDateString(); //獲取當(dāng)前日期 var mytime=myDate.toLocaleTimeString(); //獲取當(dāng)前時(shí)間 myDate.toLocaleString( ); //獲取日期與時(shí)間 if (mytime<"23:30:00") { alert(mytime); } //--> </SCRIPT>
2.如何用js得到 yyyy-mm-dd格式的當(dāng)前日期,?
--------------------------------------------------------------------------------
view plaincopy to clipboardprint? function appendZero(s){return ("00"+ s).substr((s+"").length);} //補(bǔ)0函數(shù) var d = new Date(); alert(d.getFullYear() + "-" + appendZero(d.getMonth() + 1) + "-" + appendZero(d.getDate())); function appendZero(s){return ("00"+ s).substr((s+"").length);} //補(bǔ)0函數(shù) var d = new Date(); alert(d.getFullYear() + "-" + appendZero(d.getMonth() + 1) + "-" + appendZero(d.getDate()));
3.javascript 獲得指定日期的臨近日期的方法
view plaincopy to clipboardprint? //取得日期字符串,返回YYYY-MM-DD function getDate(date) { var thisYear = date.getYear(); var thisMonth = date.getMonth() + 1; //如果月份長(zhǎng)度是一位則前面補(bǔ)0 if(thisMonth<10) thisMonth = "0" + thisMonth; var thisDay = date.getDate(); //如果天的長(zhǎng)度是一位則前面補(bǔ)0 if(thisDay<10) thisDay = "0" + thisDay; return thisYear + "-" + thisMonth + "-" + thisDay; } //取得日期時(shí)間字符串,返回YYYY-MM-DD HH:mm:SS function getDateTime(date) { var thisYear = date.getYear(); var thisMonth = date.getMonth() + 1; //如果月份長(zhǎng)度是一位則前面補(bǔ)0 if(thisMonth<10) thisMonth = "0" + thisMonth; var thisDay = date.getDate(); //如果天的長(zhǎng)度是一位則前面補(bǔ)0 if(thisDay<10) thisDay = "0" + thisDay; var thisHour = date.getHours(); //如果小時(shí)長(zhǎng)度是一位則前面補(bǔ)0 if(thisHour<10) thisHour = "0" + thisHour; var thisMinute = date.getMinutes(); //如果分鐘長(zhǎng)度是一位則前面補(bǔ)0 if(thisMinute<10) thisMinute = "0" + thisMinute; var thisSecond = date.getSeconds(); //如果分鐘長(zhǎng)度是一位則前面補(bǔ)0 if(thisSecond<10) thisSecond = "0" + thisSecond; return thisYear + "-" + thisMonth + "-" + thisDay + " " + thisHour + ":" + thisMinute + ":" + thisSecond; } //根據(jù)日期字符串生成日期對(duì)象,日期字符串格式為YYYY-MM-DD function setDate(strDate) { var aDate = strDate.split("-"); return new Date(aDate[0],aDate[1]-1,aDate[2]); } //獲得指定日期的臨近日期 //strDate:指定的日期,格式為yyyy-mm-dd nDay:與指定日期相鄰的天數(shù) 1為明天 -1為昨天 function getNearDay(strDate,nDay) { try { var oDate = setDate(strDate); var newDate = new Date(oDate.valueOf() + nDay*24*60*60*1000); return getDate(newDate); } catch(ex) { return "error"; } }
本文來(lái)自CSDN博客,,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/lord_is_layuping/archive/2009/09/03/4514130.aspx
|