Answer the question
In order to leave comments, you need to log in
How to get only time with json api?
Hello, help me get the correct time from
https://ws.audioscrobbler.com/2.0/?method=user.get...
I take this:
<script>
// last.fm user get recent tracks
// docs: https://www.last.fm/api/show/user.getRecentTracks
$.getJSON("https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=tumarfm&api_key=0a1d291e241f00167608af030298c212&format=json", function (data) {
$.each(data["recenttracks"]["track"], function (key, value) {
var song = value["name"];
var artist = value["artist"]["#text"];
var date = value["date"]["#text"];
var html = "<li>";
html += "<a href=\"#\"></span>";
html += "<b>" + song + "</b>";
html += "<br/>" + artist + "</a>";
html += "</li>";
$("#results").append(html);
});
})
// Errors are your friend!.. Use them..
.error(function (jqXHR, textStatus, errorThrown) {
console.log("error " + textStatus);
console.log("error throw " + errorThrown);
console.log("incoming Text " + jqXHR.responseText);
}) // End of .error
.complete(function () {
console.log("complete");
});
</script>
Answer the question
In order to leave comments, you need to log in
There, in addition to the field , another field is date
returned in the field - unix timestamp. It's more convenient to use it. Instead of something like this:#text
uts
var date = value["date"]["#text"];
var date = new Date(1000 * parseInt(value["date"]["uts"])); // дата-время из трека
var now = new Date(); // сейчас
// теперь сравнивайте две даты
var diff = Math.floor((now - date) / 1000); // прошло секунд
var when = '';
if( diff < 3600) {
when = '' + Math.floor(diff / 60) + ' минут назад';
} else if( diff < 86400) {
when = '' + Math.floor(diff / 3600) + ' часов назад';
} else if( diff < (7 * 86400)) {
when = '' + Math.floot(diff / 86400) + ' дней назад';
} // ... и так далее
Get it as it is, then customize - https://habr.com/post/132654/
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question