Reputation: 6077
I have a backend service written in WebApi and front end with AngularJS. From backend I have returned DateTime as below
public async Task<DateTime> GetDateTime(Guid id)
{
DateTime dateTime = GetDateTime();
return dateTime;
}
Inside angular controller I have received response like this
service.getDateTime({
id: $scope.id
},
function(data) {
$scope.minSelectedScheduleDate = new Date(data);
$scope.isBusy = false;
},
function(response) {
$scope.isBusy = false;
});
But minSelectedScheduleDate
become invalid date.
When I tried to see the data into console I saw something like this:
Resource {
0: "2",
1: "0",
2: "1",
3: "6",
4: "-",
5: "1",
6: "0",
7: "-",
8: "2",
9: "8",
10: "T",
11: "1",
12: "1",
13: ":",
14: "3",
15: "0",
16: ":",
17: "0",
18: "0"
}
My question is, how can I receive a valid Date in Angular Controller?
Upvotes: 2
Views: 1463
Reputation: 1990
We have an Angular application that returns a revisionDate: Date property from a WebApi service. The first thing we do to it after it loads to convert it to a javascript Date like so:
// convert WebApi date format to javascript format
myJsonObject.revisionDate = new Date(myJsonObject.revisionDate.toString());
Upvotes: 0
Reputation: 6077
The problem is I am returning DateTime object directly. If I return an anonymous object from my controller, then Javascript deserialize everything fine. Here is my current controller:
public async Task<DateTime> GetDateTime(Guid id)
{
DateTime dateTime = GetDateTime();
return new
{
ScheduleDate = dateTime
};
}
Upvotes: 0
Reputation: 557
I think you get date as response in object format of string, so try this
function(data) {
var dateString="";
for(var prop in data)
dateString+=data[prop];
$scope.minSelectedScheduleDate=new Date(dateString);
}
Upvotes: 0