What would a function look like that would return local time here, local time there, sunrise there, sunset there, the time of the weather report and the times of each of the 3hr forecasts?
Answering my own question in separate post below.
I am using google places api to get latitude, longitude, and utc_offset of remote location.
I am using openweathermap api to get time (in utc format) of remote location's current weather report, sunrise, sunset, and 24 hour forecast in 3hr time increments.
I create a javascript date() object from which I obtain my (not remote) location's utc_offset using getTimezoneOffset() function.
I use a function (shown below) that converts a UNIX timestamp into clock hours and minutes (modified for my own use from this post on stackoverflow 5-21-2011 Convert a Unix timestamp to time in JavaScript.)
What I need to do is this:
- get the remote utc Unix time for whatever weather information being reported (current weather, sunrise, sunset, multiple 3h forecasts)
- subtract my local utc offset to get an adjusted unix time for my current time at Greenwich.
- add the remote utc offset to get an adjusted unix time at remote location
- pass adjusted unix time to the TimeConverter function.
Using this information, I posted the function below which uses a javascript class object to hold and return all the information needed.
Here are my utc dates
Timestamp from geolocation call on my local device.
timestamp: 1523646017649 (this has to be adjusted down by 1000 to match adjusted timestamps from googleplaces and openweather map)
Timestamp from openweathermap's weather api call for remote longitude and latitude
dt:1523644200 (response.dt)
sunrise: 1523595750 (response.sys.sunrise)
sunset: 1523644821 (response.sys.sunset)
first 3h forecast: 1523653200 (response.sys.sunrise)
My utcoffset from dateObj = new date(); then dateObj.getTimezoneOffset()
my utc_offset: 18000
Remote utc_offset from googleplacesapi
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 + ', ' + padLeft(hour,2,' ') + amPm;
} else
if (hhmmOpts == 1) { // Apr 18, 2018, 11:27a
time += month + '-' + padLeft(day,2,'0') + '-' + year + ', ' + hour + ":" + min + amPm;
} else
if (hhmmOpts == 2) { // 11p
time += hour + ":" + min + amPm;
}
return time;
}