I am getting my date from my API in following json format:
"mydate":{"year":2023,"month":2,"day":1}
I tried to use in template like this:
<td>{{ mydate | date }}</td>
Which is giving me error.
Unable to convert "[object Object]" into a date' for pipe 'DatePipe
How can I show my date as Jan 2, 2023 Format?
Make a Pipe which converts your format to Date.
See this doc: Making custom pipe Angular docs
After that you can change your template to something like this:
<td>{{ mydate | appToDate | date }}</td>
Related
I am getting the following error in my console - "Error: InvalidPipeArgument: 'Unable to convert "[object Object]" into a date' for pipe 'DatePipe'".
I have a calendar input, which should allow the user to select a date and then pass on that date in a certain format, 'dd/MM/yyyy'. I want the date selected to show in the calendar input once they have selected a date.
I realise I cannot have two way binding on the [ngModel] if I have a pipe there so I'm using
(ngModelChange). If I remove the #createdByCutOffDate="ngModel" then the error is removed but I cannot see the selected date show in the calendar input.
I also tried the updateCreatedByCutOffDate() method taking a date type or string.
this.createdByCutOffDate is in the following format - 'Thu Feb 17 2022 00:00:00 GMT+0000 (Greenwich Mean Time)'
component.html
<input type="date"
id="createdByCutOffDate"
[ngModel]="createdByCutOffDate | date:'dd/MM/yyyy'"
#createdByCutOffDate="ngModel"
(ngModelChange)="updateCreatedByCutOffDate($event)" />
component.ts
createdByCutOffDate: Date;
updateCreatedByCutOffDate(date: string) {
this.createdByCutOffDate = new Date(date);
}
createdByCutOffDate is a Date object that has its methods and properties.
So to solve your problem, use "createdByCutOffDate.date | date:'dd/MM/yyyy'" instead of "createdByCutOffDate | date:'dd/MM/yyyy'"
I have a Jekyll site with a _data directory full of files organized by date:
/_data/
mydata_2020-08-28.json
mydata_2020-08-29.json
mydata_2020-08-30.json
mydata_2020-08-31.json
I'd like to pull data from these files based on today's date, but I can't quite seem to get it right.
Let's say that I want to pull data from {{ site.data.mydata_2020-08-31.somefield }}, but I want to dynamically generate the date (YYYY-MM-DD) as today's date using {{ 'now' | date: "%Y-%m-%d" }}. I need to insert the date object into the data file object, but I can't find a way to do it.
I've tried:
{{ site.data.mydata_{{ 'now' | date: "%Y-%m-%d" }}.somefield }}
but it returns the truncated end of my liquid object:
.somefield
And I tried:
{{site.data.mydata_| append: 'now' | date: "%Y-%m-%d" | append:.somefield}}
which returns today's date:
2020-08-31
I also read through most of the documentation at https://shopify.github.io/liquid/, but maybe I'm overlooking something?
If you want to fetch data based on the date in your case, try with the following:
{% assign today_data = 'now' | date: "%Y-%m-%d" | prepend: 'mydata_' %}
# You can fetch data file with particular date as follows:
{{ site.data[today_data] }}
NOTE: .somefield can't be fetched in above approach.
I have a table with a period and sales column.
the period look like this
2017001
2017002
......
2017011
2017012
I use the Carbon extension to extract the month number into month name with
{{ Carbon\Carbon::parse(substr($saleCurrent->period, -2))->month }}
but i'm getting an error
DateTime::__construct(): Failed to parse time string (01) at position 0 (0): Unexpected character
This is because the month should be 1 not 01 ?
Use the CreateFromFormat method so that you can pass in the format your dates are stored in:
{{ Carbon\Carbon::createFromFormat('Ymd', $saleCurrent->period)->month }}
I am using the Jira API, and need the start and end dates for a sprint.
The JSON data I get back is :
{"jodaTimeZoneId":"Europe/Berlin","sprints":[{"id":5,"start":"13082015044305","end":"27082015044305",...
Normally, json returns the date in milliseconds, and you need to deserialize that.
Now however, I can clearly see the date (13-08-2015 & 27-08-2015) followed by some other numbers I don't care about. Is there anyway Angular can get the correct format using | date? Or any other way I can use?
When I use {{13082015044305 | date:'dd-MM-yyyy'}} it returns 21-07-2384. The parsing date format is wrong. So change the format to recognized way.
So I used
input.toString().replace(/(\d\d)(\d\d)(\d\d\d\d)(\d\d\d\d\d\d)/, '$1-$2-$3');
Used it in a custom filter.
app.filter('correctDateFormat', function() {
return function(input) {
return input.toString().replace(/(\d\d)(\d\d)(\d\d\d\d)(\d\d\d\d\d\d)/, '$1-$2-$3');
};
});
Then
Display the date as
{{13082015044305 | correctDateFormat }}
I think you can use
{{ data | filter:options }}
where data is your json and date filter
{{'1388123412323' | date:'MM/dd/yyyy # h:mma'}}
an option like this.
I have two objects in Twig:
{{ date }} is DateTime object
{{ interval }} is DateInterval object
I need to show date with interval added. How can I use DateObject php function like add() with this two objects?
I can't do this on controller side, I need interval to date in twig. :(
You can use date_modify.
In your case, it would go as:
{{ yourDate|date_modify(yourInterval)|date("m/d/Y") }}