1.获取系统的ip地址:
可利用搜狐的IP地址查询接口:http://pv.sohu.com/cityjson (默认GBK) 或:http://pv.sohu.com/cityjson?ie=utf-8 (可设置编码);
function showIP(){
/**必须引用 http://pv.sohu.com/cityjson?ie=utf-8 */
document.getElementById("ipAddress").innerHTML = returnCitySN["cip"];
document.getElementById("cityName").innerHTML = returnCitySN["cname"];
}
2.获取当前时间:
function showTime(){
var date = new Date();
var year = date.getFullYear(); //当前年份
var month = date.getMonth < 9 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1); //当前月份(0-11,0代表1月)
var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); //当前日(1-31)
var weekday = date.getDay(); //当前星期(0-6,0代表星期日)
var weekdayArray = ["日", "一", "二", "三", "四", "五", "六"];
var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); //当前小时
var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); //当前分钟
var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); //当前秒
var milliseconds = date.getMilliseconds(); //当前毫秒
var time = date.getTime(); //当前时间的毫秒数
var strYMD = date.toLocaleDateString(); //获取当前时间: 年/月/日
var strTime = date.toLocaleString(); //获取当前时间: 年/月/日 上/下午 时:分:秒
var str = "";
str += year + "-" + month + "-" + day;
str += " 星期" + weekdayArray[weekday];
str += " " + hours + ":" + minutes + ":" + seconds + ":" + milliseconds;
document.getElementById("nowTime").innerHTML = str;
}
全部代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>获取一些系统常用数据</title>
<script type="text/javascript" src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
<style type="text/css">
label{
font-weight: 700;
}
div{
margin-top: 10px;
}
</style>
</head>
<body>
<div>
<label>ip地址:</label>
<span id="ipAddress"></span>
<label>所在地址:</label>
<span id="cityName"></span>
</div>
<div>
<label>当前时间:</label>
<span id="nowTime"></span>
</div>
</body>
<script type="text/javascript">
showIP();
setInterval(showTime,1);
showTime();
function showIP(){
/**必须引用 http://pv.sohu.com/cityjson?ie=utf-8 */
document.getElementById("ipAddress").innerHTML = returnCitySN["cip"];
document.getElementById("cityName").innerHTML = returnCitySN["cname"];
}
function showTime(){
var date = new Date();
var year = date.getFullYear(); //当前年份
var month = date.getMonth < 9 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1); //当前月份(0-11,0代表1月)
var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); //当前日(1-31)
var weekday = date.getDay(); //当前星期(0-6,0代表星期日)
var weekdayArray = ["日", "一", "二", "三", "四", "五", "六"];
var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); //当前小时
var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); //当前分钟
var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); //当前秒
var milliseconds = date.getMilliseconds(); //当前毫秒
var time = date.getTime(); //当前时间的毫秒数
var strYMD = date.toLocaleDateString(); //获取当前时间: 年/月/日
var strTime = date.toLocaleString(); //获取当前时间: 年/月/日 上/下午 时:分:秒
var str = "";
str += year + "-" + month + "-" + day;
str += " 星期" + weekdayArray[weekday];
str += " " + hours + ":" + minutes + ":" + seconds + ":" + milliseconds;
document.getElementById("nowTime").innerHTML = str;
}
</script>
</html>
本文介绍了一种使用JavaScript获取系统IP地址及当前时间的方法。通过调用搜狐的API,可以轻松获取用户的IP地址及其地理位置信息。同时,文章还提供了一个显示实时时间的脚本,包括年、月、日、星期、小时、分钟、秒和毫秒。

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



