Skip to content

Instantly share code, notes, and snippets.

@joaofnds
Last active November 21, 2018 21:08
Show Gist options
  • Select an option

  • Save joaofnds/24ca56a87df754ea1ea6852f9e0a3b2e to your computer and use it in GitHub Desktop.

Select an option

Save joaofnds/24ca56a87df754ea1ea6852f9e0a3b2e to your computer and use it in GitHub Desktop.
A simple example of how to show dates in user locale
const SERVER_URL = "https://donamaid.herokuapp.com";
const fetchServer = async (path, opts = {}) =>
(await fetch(`${SERVER_URL}/${path}`, opts)).json();
const createServerAPI = token => ({
get: (path, opts = {}) => {
return fetchServer(path, {
method: "GET",
headers: {
Authorization: `Bearer ${token}`
},
...opts
});
}
});
(async () => {
const { data: { token, account: { id } } } = await fetchServer(
"authentication",
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email: "USER_EMAIL",
password: "USER_PASSWORD"
})
}
);
const api = createServerAPI(token);
try {
const { data: { attributes: { language, time_zone } } } = await api.get(
`accounts/${id}`
);
console.log(new Date().toLocaleString(language, { timeZone: time_zone }));
} catch (e) {
console.log(e);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment