I have a column named birth_date in MySQL database with a type of date.
Now, I would like to show it in my view in this format : May 23, 2008
I tried:
{{ $user->user_information->birth_date->format('m-d-Y') }}
it showed:
ErrorException (E_ERROR) Call to a member function format() on string
I also tried
{{ date('m-d-Y', $user->user_information->baptism_date) }}
it showed:
ErrorException (E_ERROR) A non well formed numeric value encountered
Can anyone assist me with the right formatting code?
You can use the date() function for this purpose like:
date('m-d-Y', strtotime($user->user_information->birth_date));
or you can also use Carbon for this purpose like:
{{ \Carbon\Carbon::parse($user->user_information->birth_date)->format('m-d-Y')}}
For May 23, 2008:
date('M d, Y', strtotime($user->user_information->birth_date));
Working code snippet
date() format list is available here
M - A short textual representation of a month, three letters
Try this code.
{{ Carbon\Carbon::parse($user->user_information->birth_date)->format('F d, Y') }}
you can use date function. try this code:
{{ date('F j, Y', strtotime($user->user_information->birth_date)); }}
Related
I am trying to read a variable which is defined in my javascript. It is a get request from a mongoDB. The whole database is then stored under a variable and is read by the HTML and displayed.
The get request from the MongoDB has an output like this:
(Lets say this is stored under the variable database):
[
{
0:
{
TITLE1: valueone
}
},
{
1:
{
TITLE2: valuetwo
}
}
]
My HTML looks like this:
<p> {{ database?.TITLE1 }} </p>
I get the error Cannot read property '0' of undefined. I understand this is because I need to define [0] to be able to read TITLE1.
Based on this I have tried the following:
<p> {{ database?[0].TITLE1 }} </p>
This has this error: Template parse errors:
Parser Error: Conditional expression database?[0].TITLE1 requires all 3 expressions at the end of the expression [{{ database?[0].TITLE1 }}]
<p> {{ database?.[0].TITLE1 }} </p>
This has this error: Template parse errors:
Parser Error: Unexpected token [, expected identifier or keyword at column 7 in [{{ database?.[0].TITLE1 }}]
<p> {{ database?.0.TITLE1 }} </p>
This has the same error as the one above.
What is the correct way to be able to read the values that I am after. In the HTML the output should be valueone.
Because your database is array, use need use [0] to get first item and your key is number so you need use ['0'] to get property value, you also can use ? to check object null before using TITLE1
Finally you can use {{ database[0]['0']?.TITLE1 }}
Demo https://stackblitz.com/edit/angular-o79rra
Well I mean, at some point you have to think a little about JS basics instead of trying everything until it works ...
From the code you have provided, the syntax would be
{{ data[0]['0'].TITLE1 }}
Try
<p> {{ database[0]['0']?.TITLE1 }} </p>
Do it dynamically by:
<p *ngFor="let item of database; let i = index">
{{item[i]['TITLE' + (i + 1)]}}
</p>
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 currently have a date that is formatted in unicode:
k = u'2015-02-01'
I tried to add this to a list and change it into a string:
date = []
date.append(str(k))
Then I want to pass this as a Django context to my template.
However, the date is showing up with the following:
'2015-02-01'
How do I just rid of $#39; and replace it with a double quote (")?
Thanks much.
You can try to prevent string escape in template like this:
{{ variable|safe }}
In-view way:
from django.utils.safestring import mark_safe
from django.template import Context
data=mark_safe(data)
inescapable = Context({'data': data}, autoescape=False)
I know this is old but other people might stumble upon this with the same problem
Try
{% autoscape off %} {{ date }} {% endautoscape %}
It worked fine for me
When requesting graphs from google charts the data must be sent as a text array. The csv file has to be pure text with no apostrophes.
however
code fragment
data = repr(textData)
returns data bounded by ' '
this is interpreted as "'" in html
The solution to this is to javascript split method
var par = textData.split(""'") textArray = par[1] // the part without '
rest of code
I'm using Google app engine and I'm trying to set the value of a textarea based on the concatenation of two string variables. Let's say I have 4 items, and each item has multiple fields. So in my Python I'm passing the dictionary { 'value0': newValue }. I'm using a for loop (iterator value num) and I want to use in my HTML something equivalent to {{ value }}{{ num }} where the variable referenced is value0.
I've tried {{ value~num }} but nothing works. If I could use an array that would be even better - such as {{ value[num] }}
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") }}