Emi
Emi

Reputation: 1215

Get time from datetime format

I'm getting this date format from back-end: 1970-01-01T10:59:00Z

How can I get the time from it with JavaScript and put it in this input:

<input type="time" ng-model='content.time' />

Thanks for your help!

Upvotes: 0

Views: 921

Answers (7)

Sa E Chowdary
Sa E Chowdary

Reputation: 2075

place this code in the controller where after the data u receive from backend

var d = new Date("1970-01-01T10:59:09Z");//pass the object which have ur date

$scope.content.time=d.getHours() + ":" + d.getMinutes()+":"+ d.getSeconds();

Upvotes: 0

Efe Omoregie Elijah
Efe Omoregie Elijah

Reputation: 488

In your controller add this function

var parseDateTime = function (input) {
    vars = input.split('T');
    date = vars[0].split('-');
    time = vars[1].split(':');
    //return date in 'yyyy-MM-dd H:i:s'
    return date[0] + '-' + date[1] + '-' + date[2] + ' ' + time[0] + ':' + time[1] + ':00';
}

Then you can use it with your datetime

var dateTime = DATE_RECEIVED_FROM_BACKEND;
$scope.content.time = parseDateTime(dateTime);

Hope this helps :)

Upvotes: 0

n daniel
n daniel

Reputation: 148

With angular, and not just vanilla javascript/DOM manipulation, in your controller js:

var dateTime=<referenceToDataRetrievedFromBackEnd>;
/*Format the date you've retrieved*/
var hours = new Date().getHours();
var minutes = new Date().getMinutes();
var seconds = new Date().getSeconds();
var formattedTimeString=hours + ":" + minutes + ":" + seconds

/*This is the part that actual ties the retrieved and reformatted data to the view*/
$scope.content.time=formattedTimeString;

Upvotes: 0

Shobhit Walia
Shobhit Walia

Reputation: 496

var date = new Date($scope.content.time);
var converted = date.getHours() + ":" + date.getMinutes();
console.log(converted);

Upvotes: 1

Richard Hamilton
Richard Hamilton

Reputation: 26444

You could use the getHours(), getMinutes() and getSeconds() function and concatenate the values.

var result = document.getElementById("result");

var dateTime = new Date();
var hours = new Date().getHours();
var minutes = new Date().getMinutes();
var seconds = new Date().getSeconds();

result.innerHTML = hours + ":" + minutes + ":" + seconds;
<p id="result"></p>

Upvotes: 1

NTL
NTL

Reputation: 1007

var date = new Date("1970-01-01T10:59:00Z");
var converted = date.toLocaleString();
console.log(converted);

Then you'd just assign the converted value to the model.

Upvotes: 0

GrahamJ
GrahamJ

Reputation: 528

This might help you:

var dt = new Date(); var tm = dt.getUTCHours();

For UTC.

Also you can use:

new dt.getHours()

new dt.getMinutes()

Upvotes: 0

Related Questions