I am with Angular 6 , I receive from the server a JSON whose date is: "loadDate": "2017-02-12T00:00:00+01:00"
,
When displaying this date in the html I have:
<td class="left">{{ scenario.loadDate | date:'shortDate':'':'es' }}</td>
This pipe returns the date 12/2/17
.
When I modify other data of the object and send this object again, what is being sent is the date with a lot of rubbish:
"2017-02-12T00:00:00+01:00"
What I want is to send is 12/2/17
that it is shown in it HTML
or give it my own format 2017-02-12
, I had thought of cutting the string that I receive in the T
and keeping the part on the left, the problem is that it is not unknown how the date will come .
If I have understood your problem correctly, what you would like is to have the date already in the correct format in the component, as shown when you use the pipe .
In that case the solution is simple: use the function that DatePipe uses internally in the component:
HolaEdu, you can make your own custom pipe and thus be able to return the date depending on how it comes, you put all the cases in your custom pipe, in this case you will always receive the date in string and already within the custom pipe you can work with that date data in different ways of logic within the pipe, likewise, the pipe would already return with the data that you want to work with, well, from my point of view, I would do it with a custom pipe so that the code looks cleaner and of course take advantage of custom pipes :)
Here I leave you an example so that you understand me.
In this example that I mention, a custom pipe was created that evaluates depending on the argument that is passed to the pipe, as you can see, it is passed through letters: 'portugues'.
In the pipe, the body of what you are going to work on is received first, in this case it would be the value, then comes the argument that you want to show or work on, which would be Portuguese, Spanish, Castilian.
The custom pipe works that logic as you can see with a switch routine and I think you can understand that logic which is very easy.
I will give you an example of how to generate your
pipe
for the date format you need:First of all, execute the following code in the console where you see you
MiFechaFormat
could also put a routemisPipes/MiFechaFormat
:Obviously you can put the name you want:
Now open the file that was generated from the pipe, here I will show you an example that I made for the pipe of your date but you would put the code you need to format it:
And in your
HTML
it should be:You could even send one type if you need multiple types of formatting, for example:
And in
HTML
it you would use it in the following way:I hope it helps you ;) Greetings
Very good, take a look at the official documentation of Angular:
https://angular.io/api/common/DatePipe
In "Custom format options" it tells you how you can customize the date format. But it looks like for your specific request, it should be:
date:'dd-MM-yyyy'