musigh
musigh

Reputation: 175

How to pass and process date from asp.net webapi to angular component?

I am sending date from asp.net web api like this:

SubmitDate.ToString()

and it is returning:

12/13/2018 8:24:20 AM

in angular component and I'm using angular pipe to show date in "December 12, 2018" format, It is working fine with my current PC date format but when I change date format of my PC, It gives me error as shown below

'Unable to convert "18/12/13 1:54:20 PM" into a date' for pipe 'DatePipe'

Please help to understand it and I have tried these ways

SubmitDate.ToLocalTime().ToString()

SubmitDate.ToUniversalTime().ToString()

but with no success. How to pass and process date from web api to angular component?

With angular pipe:

solution.SubmitDate | date:'MMMM d, yyyy'

Upvotes: 4

Views: 3728

Answers (4)

nabakumar samanta
nabakumar samanta

Reputation: 1

Please try this. It is working for me.

var utcDate1 = new Date(Date.UTC(96, 1, 2, 3, 4, 5)); // expected output: Fri, 02 Feb 1996 03:04:05 GMT

Upvotes: 0

Tanmay
Tanmay

Reputation: 1165

You can fix the format of the date at API side SubmitDate.ToString("MMM d, yyyy");

Upvotes: 0

Sergii Shumakov
Sergii Shumakov

Reputation: 116

You can use DateTime.ToString() overload which accepts a parameter to allow you to format your date.

For example, to format to the date which is working for you at this point, you can use:

var date = new DateTime(2018, 12, 13, 8, 24, 20);
Console.WriteLine(date.ToString("MM/dd/yyyy h:mm:ss tt", CultureInfo.InvariantCulture));
//12/13/2018 8:24:20 AM

CultureInfo.InvariantCulture is used here to not take into account any culture specific formatting/localization.

More documentation about date formatting - https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

Upvotes: 2

Jonas Praem
Jonas Praem

Reputation: 2445

Javascript has a date object. If you send the date as an accepted format, you could automatic parse this to a javascript date object.

Declare your response interface like this for automatic parsing.

export interface something {
  time: Date;
}

From asp.net you need to make sure that you send in a format that javascript understand. From the docs accepted formats could look like this:

December 17, 1995 03:24:00

or

1995-12-17T03:24:00

The problem is that asp.net and javascript have different standards for date formats.

If you want to make a client side solution [JavaScript]:

function ToJavaScriptDate(value) {
  var pattern = /Date\(([^)]+)\)/;
  var results = pattern.exec(value);
  var dt = new Date(parseFloat(results[1]));
  return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
}

But the best way is a server side solution. Here is a function that returns true UTC time [C#].

return DateTime.UtcNow
               .Subtract(new DateTime(1970,1,1,0,0,0,DateTimeKind.Utc))
               .TotalMilliseconds;

That way you know exactly what is meant on the client side. Time may differ from client to client.

Upvotes: 1

Related Questions