js-----時間格式化、獲取當前網頁路徑
01:時間型別轉字串
程式碼:直接放到js中
Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小時
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
};
使用:在執行上述程式碼後,顯示當前時間
setInterval(function(){
var date = new Date();
datefmt=date.Format("yyyy-MM-dd hh:mm:ss")
console.log("當前時間 : "+datefmt);
},1000);
02:獲取當前網頁路徑
var utils_localhost=null;
function getPath(){
if(utils_localhost!=null)return utils_localhost;
var curWwwPath = window.document.location.href;
// 獲取主機地址之後的目錄,如: uimcardprj/share/meun.jsp
var pathName = window.document.location.pathname;
var pos = curWwwPath.indexOf(pathName);
// 獲取主機地址,如: http://localhost:8083
var localhostPaht = curWwwPath.substring(0, pos);
// 獲取帶"/"的專案名,如:/uimcardprj
var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
utils_localhost = localhostPaht + projectName + "/";
return utils_localhost;
}
相關文章
- javascript如何獲取當前頁面的路徑JavaScript
- ThinkPHP 在頁面獲取當前時間PHP
- js時間物件:獲取當前時間(格式化)- 程式碼篇JS物件
- mybatis獲取當前時間MyBatis
- java獲取當前時間Java
- Qt獲取當前時間QT
- js獲取當前時間JS
- Qt 獲取當前時間QT
- c++ 獲取當前時間周初凌晨時間戳(獲取當前時間週一凌晨時間戳)C++時間戳
- python 如何獲取當前時間Python
- javascript獲取當前的時間戳JavaScript時間戳
- QT獲取系統當前時間QT
- js獲取當前的具體時間JS
- 如何用Java獲取當前時間戳?Java時間戳
- Javascript獲取當前時間戳的方法JavaScript時間戳
- Qt學習(5)獲取當前系統時間-實時顯示當前時間QT
- javascript獲取當前頁面的來路頁面地址JavaScript
- 獲取當前js檔案被引用的路徑JS
- Flutter獲取當前網路型別Flutter型別
- 實時獲取當前時間程式碼例項
- js獲取當前時間上一個月前的時間JS
- 用Delphi獲取當前系統時間 (轉)
- 如何用js獲取當前時間和ip地址以及當前城市JS
- Python如何獲取當前執行檔案路徑?Python
- JavaScript中獲取當前專案的絕對路徑JavaScript
- javascript如何獲取當前網頁的url地址JavaScript網頁
- linux下獲取程式當前目錄絕對路徑Linux
- PHP獲取當前季度的開始時間和結束時間PHP
- js獲取指定時間日期和當前時間日期的相差多少時間JS
- Java 獲取本地時間與網路時間Java
- 獲取當前頁面的topViewControllerViewController
- C#—獲取網路時間C#
- JavaScript怎麼獲取當前時間的年月日?JavaScript
- Qt獲取當前時間的兩種方式筆記QT筆記
- 獲取當前頁面的上一個頁地址
- python獲取網路時間和本地時間Python
- react獲取當前頁面的url引數React
- PHP中獲取當前頁面的完整URLPHP