//======================获取当前时间===========================
function getNowFormatDate() {//
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1<10? "0"+(date.getMonth() + 1):date.getMonth() + 1;
var strDate = date.getDate()<10? "0" + date.getDate():date.getDate();
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
+ " " + date.getHours() + seperator2 + date.getMinutes()
+ seperator2 + date.getSeconds();
return currentdate;
}
//========================获取本机IP=============================
function getUserIP(onNewIP) { // onNewIp - your listener function for new IPs
//compatibility for firefox and chrome
var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
var pc = new myPeerConnection({
iceServers: []
}),
noop = function() {},
localIPs = {},
ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
key;
function iterateIP(ip) {
if (!localIPs[ip]) onNewIP(ip);
localIPs[ip] = true;
}
//create a bogus data channel
pc.createDataChannel("");
// create offer and set local description
pc.createOffer().then(function(sdp) {
sdp.sdp.split('\n').forEach(function(line) {
if (line.indexOf('candidate') < 0) return;
line.match(ipRegex).forEach(iterateIP);
});
pc.setLocalDescription(sdp, noop, noop);
}).catch(function(reason) {
// An error occurred, so handle the failure to connect
});
//sten for candidate events
pc.onicecandidate = function(ice) {
if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
};
}
// Usage
getUserIP(function(ip){
alert("Got IP! :" + ip);
});
获取本机IP和当前时间
最新推荐文章于 2023-11-01 12:53:25 发布
本文介绍了一种使用JavaScript获取当前系统时间的格式化方法,并提供了一个利用WebRTC API获取本地IP地址的实用函数。这些方法适用于网页应用中需要记录时间戳或获取客户端IP的场景。
1万+

被折叠的 条评论
为什么被折叠?



