B
B
Be Clack2018-09-12 23:32:04
JavaScript
Be Clack, 2018-09-12 23:32:04

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>


As a result, I get the date format - "2 Sep 2018, 10:48"
How to get only the time? + in another time zone ? for example the time zone is +6
and even better, how to turn this date into the form " 3 minutes ago " for example

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Sokolov, 2018-09-13
@be2clack

There, in addition to the field , another field is datereturned in the field - unix timestamp. It's more convenient to use it. Instead of something like this:#textutsvar 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) + ' дней назад';
} // ...  и так далее

D
dimkaholodov, 2018-09-12
@dimkaholodov

Get it as it is, then customize - https://habr.com/post/132654/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question