Skip to content

Instantly share code, notes, and snippets.

@dustinrouillard
Last active August 12, 2022 19:21
Show Gist options
  • Save dustinrouillard/8140fd47c5900d4421637b098b6d92c0 to your computer and use it in GitHub Desktop.
Save dustinrouillard/8140fd47c5900d4421637b098b6d92c0 to your computer and use it in GitHub Desktop.
Lanyard Spotify - Calculating current time and song length
import "isomorphic-fetch";
const lanyard = await fetch(
"https://api.lanyard.rest/v1/users/156114103033790464"
).then((r) => r.json());
const spotify = lanyard.data.spotify;
const currentTime = new Date().getTime();
const songLength = spotify.timestamps.end - spotify.timestamps.start;
const timeElapsed = currentTime - spotify.timestamps.start;
function msToMinSeconds(millis) {
var minutes = Math.floor(millis / 60000);
var seconds = Number(((millis % 60000) / 1000).toFixed(0));
return seconds == 60
? minutes + 1 + ":00"
: minutes + ":" + (seconds < 10 ? "0" : "") + seconds;
}
console.log("ms song length:", songLength);
console.log("ms time elapsed:", timeElapsed);
console.log("formatted song length:", msToMinSeconds(songLength));
console.log("formatted time elapsed:", msToMinSeconds(timeElapsed));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment