Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split datetime of api response

I'm getting info from an API, and what I want to do is get the response of date time. This is part of my code:

function perDetail(){
    $http.get('/api/values')
    .then(function(data){
        $scope.details = data.data.Response;
});
}

Part of what i get is:

"Date":"2018-07-16T18:00:00"

And what I wanna do is get the hour:

18:00

Someone can guide me, please? I know there's a split and splice method, but I can't get the value. Maybe sounds too easy but right now, I'm stuck with this.

I'm using AngularJs and Javascript.

Thanx in advance.

like image 808
Chuck Villavicencio Avatar asked Feb 16 '26 05:02

Chuck Villavicencio


2 Answers

Here's one way to do it.

var date = "2018-07-16T18:00:00";
var time = date.split("T")[1]; // time is "18:00:00"
var justHour = time.slice(0, 5); //from index 0 to index 5 (first 5 characters)
return justHour; //"18:00"
like image 127
Partik Avatar answered Feb 18 '26 17:02

Partik


If I understood correctly, "Date" is a key in the response object you are getting from your API call, right? And you just want to get the hours:minutes part in the corresponding value?

If that's correct, I think slicing the value in the right places would do the job:

data["Date"].slice(-8, -3)
like image 28
selmanbey Avatar answered Feb 18 '26 18:02

selmanbey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!