-
Notifications
You must be signed in to change notification settings - Fork 1
/
gmaps_timezone.js
25 lines (23 loc) · 939 Bytes
/
gmaps_timezone.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const express = require('express');
const request = require('request');
const moment = require('moment-timezone');
const app = express();
app.get('/timezone/:lat/:lng', (req, res) => {
const lat = req.params.lat;
const lng = req.params.lng;
const apiKey = 'YOUR_GMAPS_API_KEY';
const url = https://maps.googleapis.com/maps/api/timezone/json?location=${lat},${lng}×tamp=${Math.floor(Date.now() / 1000)}&key=${apiKey};
request(url, (error, response, body) => {
if (!error && response.statusCode === 200) {
const data = JSON.parse(body);
const timezone = data.timeZoneId;
const currentTime = moment().tz(timezone).format('dddd, MMMM Do YYYY, h:mm:ss a');
res.send(The current time in ${timezone} is ${currentTime});
} else {
res.send('Error getting timezone information from Google Maps API');
}
});
});
app.listen(3000, () => {
console.log('Server listening on port 3000');
});