1. 程式人生 > 實用技巧 >JS_0042:js 獲取 省 市 縣 經緯度 北京時間

JS_0042:js 獲取 省 市 縣 經緯度 北京時間

1,

    // 獲取省市縣位置

    $(function () {
        var latlon = null;
        //ajax獲取使用者所在經緯度
        $.ajax({
            url: "http://api.map.baidu.com/location/ip?ak=2TGbi6zzFm5rjYKqPPomh9GBwcgLW5sS&coor=bd09ll",
            type: "POST",
            dataType: "jsonp",
            success: function (data) {
                
// 獲取經緯度 22.54605355,114.02597366 latlon = data.content.point.y + "," + data.content.point.x; console.log(latlon); //ajax根據經緯度獲取省市區 $.ajax({ type: "POST", dataType: "jsonp", url: 'http://api.map.baidu.com/geocoder/v2/?ak=2TGbi6zzFm5rjYKqPPomh9GBwcgLW5sS&callback=renderReverse&location=' + latlon + '&output=json&pois=0', success:
function (json) { if (json.status == 0) { // 獲取省市區 廣東省 深圳市 福田區 console.log(json.result.addressComponent.province+' '+json.result.addressComponent.city+' '+json.result.addressComponent.district); } } }); } });
// 獲取本機的北京時間 // Wed Dec 02 2020 11:39:04 GMT+0800 (中國標準時間) var ti = new Date($.ajax({async: false}).getResponseHeader("Date")); console.log(ti); // 2020-12-2 11:39:04 var datetime = ti.toLocaleString('chinese', { hour12: false }).split('/').join('-'); console.log(datetime); // 2020-12-2 11:39:04 var datetime1 = ti.getFullYear() + '-' + (ti.getMonth() + 1) + '-' + ti.getDate() + ' ' + ti.getHours() + ':' + ti.getMinutes() + ':' + ti.getSeconds(); console.log(datetime1); // 2020-12-2 var datetime2 = ti.getFullYear() + '-' + (ti.getMonth() + 1) + '-' + ti.getDate(); console.log(datetime2); // 騰訊獲取當前時間API http://vv.video.qq.com/checktime?otype=json // 蘇寧易購獲取當前時間API https://f.m.suning.com/api/ct.do // 淘寶獲取當前時間API http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp // 京東獲取當前時間API https://a.jd.com//ajax/queryServerData.html // 獲取網路的北京時間 $.ajax({ type: "POST", dataType: "jsonp", url: 'http://vv.video.qq.com/checktime?otype=json', success: function (json) { // {s: "o", t: 1606890761, ip: "119.136.32.154", pos: "---", rand: "q1KyWQ2XJEgfl7j2K7Vfrg=="} console.log(json); // 獲取當前北京時間戳 1606890339 // console.log(json.t); var ti = json.t * 1000; // 2020-12-02 console.log(getYMDHMS (ti)); // 獲取當前IP 119.136.32.154 console.log(json.ip); // 獲取唯一的隨機數 ffe7-72lffnTymU9am1VoA== console.log(json.rand); // if (json.status == 0) { // } } }); // 獲取本機時間戳 // console.log(Math.round(new Date() / 1000)); // 時間戳轉換yyyy-mm-dd function getYMDHMS (timestamp) { let time = new Date(timestamp) let year = time.getFullYear() let month = time.getMonth() + 1 let date = time.getDate() let hours = time.getHours() let minute = time.getMinutes() let second = time.getSeconds() if (month < 10) { month = '0' + month } if (date < 10) { date = '0' + date } if (hours < 10) { hours = '0' + hours } if (minute < 10) { minute = '0' + minute } if (second < 10) { second = '0' + second } // return year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second return year + '-' + month + '-' + date }; });