4

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'

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that's what I like, easily understandable and I was also thinking in the same manner. Thanks again. Cheers!
1

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.

1 Comment

Thanks for your so quick reply and effort.
0

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

1 Comment

Thanks for your quick reply and effort.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.