javascript:显示远程位置的 AM/PM 时间以获取远程位置的天气数据



一个函数会是什么样子的,它会在这里返回本地时间,在那里返回本地时间,在那里返回日出,在那里返回日落,天气报告的时间以及每个 3 小时预报的时间?

在下面的单独帖子中回答我自己的问题。

我正在使用谷歌地方 api 来获取远程位置的纬度、经度和utc_offset。

我正在使用 openweathermap api 以 3 小时时间为增量获取远程位置当前天气报告、日出、日落和 24 小时预报的时间(UTC 格式)。

我创建了一个javascript date()对象,我使用getTimezoneOffset()函数从中获取我的(非远程)位置utc_offset。

我使用了一个函数(如下所示),它将UNIX时间戳转换为时钟小时和分钟(根据Stackoverflow 5-21-2011的这篇文章修改为我自己的使用,在JavaScript中将Unix时间戳转换为时间。

我需要做的是:-
获取报告的任何天气信息的远程 utc Unix 时间(当前天气、日出、日落、多个 3 小时预报)
- 减去我的本地 utc 偏移量以获得我当前在格林威治时间的调整后的 unix 时间。
- 添加远程 UTC 偏移量以获取远程位置
的调整后的 UNIX 时间 - 将调整后的 UNIX 时间传递给 TimeConverter 函数。

利用这些信息,我发布了下面的函数,该函数使用javascript类对象来保存并返回所需的所有信息。

这是我的UTC日期 来自我本地设备上的地理位置调用的时间戳。
时间戳:1523646017649(必须向下调整 1000 以匹配来自 Googleplaces 和 OpenWeather Map 的调整时间戳)

来自 openweathermap 的天气 API 调用远程经度和纬度
的时间戳 dt:1523644200 (response.dt)日出: 1523595750 (response.sys.日出)日落: 1523644821 (response.sys.日落)第一个 3 小时预报: 1523653200 (响应.sys.日出)



我的 utcoffset from dateObj = new date(); 然后日期Obj.getTimezoneOffset()
my utc_offset: 18000

来自谷歌地点的远程utc_offset api
utc_offset:7200

//To get current time where I am at, I can use this call and this function<br>
var myTime = timeConverter(Date.now()/1000,1);    
// Return Result: Apr-13-2018, 2pm    
function timeConverter(UNIX_timestamp,hhmmOpts){
// based on code written by @shomrat and edited by @Chin at
// https://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript    
var a = new Date((UNIX_timestamp) * 1000);    
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];    
var year = a.getFullYear();
var month = months[a.getMonth()];
var day = a.getDate();
var hour = a.getHours();
var amPm = ( (hour < 12) ? "a" : "p" );
if ( hour == 0) {
hour = "12" ;
} else if ( hour > 12 ) {
hour = (hour - 12);
}
var min = a.getMinutes().toString().padStart(2,"0") ;
var time = "" ;
if (hhmmOpts == 0) { // Apr 18, 2018, 11a    
time += month + '-' + padLeft(day,2,'0') + '-' + year + ',&nbsp;' + padLeft(hour,2,'&nbsp;&nbsp;') + amPm;
} else
if (hhmmOpts == 1) { // Apr 18, 2018, 11:27a
time += month + '-' + padLeft(day,2,'0') + '-' + year + ',&nbsp;' + hour + ":" + min + amPm;
} else
if (hhmmOpts == 2) { // 11p
time += hour + ":" + min + amPm;
}
return time;
}

Placetime class 用途:

// Get the googleplaces api offset (snipet)
...
google.maps.event.addListener(autocomplete, 'place_changed', function () {
var place = autocomplete.getPlace();
// console.log(place);
utcOffset = place.utc_offset;
getWeather(utcOffset);
}
...
// Create a new placetime object
// To get device location local time use utcOffset==0.
plTimeObj = new placeTime(utcOffset);
// Get current time at present location
var plTime = timeConverter(plTimeObj.getCurrentTime(),1) ;
// Get sunrise at remote location 
// Comes from call to openweathermap (see response.sys.sunrise above )
sunriseTime = timeConverter( plTimeObj.getPlaceTime(resp.sys.sunrise) ,2) ;
// Get the time of the weather report (from openweathermap)
var rptTime = timeConverter( plTimeObj.getPlaceTime(resp.list[i].dt),2 ) ;

以下类保存计算远程位置的本地时间所需的所有信息,并提供根据需要检索信息的方法。

class placeTime {
constructor(utcOffset) {
this.dateObj   = new Date();
this.local2Utc = this.dateObj.getTimezoneOffset() * 60 ;
this.utc2Place = (utcOffset==0 ? (0-this.local2Utc) : (utcOffset * 60) );
this.currentTime = this.dateObj.getTime()/1000 + this.local2Utc + this.utc2Place ;
}
getPlaceTime(localTime) {
var pTime = localTime + this.local2Utc + this.utc2Place ;
return pTime;
}
getCurrentTime() {
return this.currentTime ;
}
}

最新更新