I'm having a problem with the twig date filter. I'm trying to universally change any post time to PST. But if I were to post an item at 5:20 ET, the post will say 9:20pm (which is UTC) when it is supposed to say 2:20pm (which is PST). I just want to know how to change UTC to PST because the date filter is not doing it for me.
{{ post.published_at }}
will give 2013-12-08 21:20:46"
{{ post.published_at|date("F jS \\a\\t g:ia", "PST") }}
will give "August 12th at 9:20pm"
Try
{# using "PST" is fine too as I noticed #}
{{ post.published_at|date("F jS \\a\\t g:ia", "America/Los_Angeles") }}
The timezone parameter uses the accepted values from PHP. Los Angeles should be PST so it should work.
See the list of supported timezones.
It's also shown in the twig documentation (or at least there's a hint) where they use Europe/Paris as timezone.
EDIT
Example to change an existing date, assuming you have a DateTime object.
PHP:
$date = new \DateTime('2013-12-08 21:20:46');
$pst = new \DateTimeZone('PST');
$date->setTimezone($pst);
And in twig:
{{ date|date("F jS \\a\\t g:ia", "PST") }}
Will output December 8th at 12:20pm
Related
How can I convert milliseconds date to formatted date.
I tried the following :
<set-body template="liquid">
{
"date" : "{{ "1610924400000" | Date: "MM/dd/yyyy" }}"
}
</set-body>
But it prints :
{"date": "1610924400000"}
Is it possible ? How can i do this pls ?
You should use "%m/%d/%Y" instead of "MM/dd/yyyy".
Liquid templates use the same format as Python's strftime. You can find the documentation about it here.
I am trying to format a datetime value into a human readable format
For example:
value = 2019-12-17T08:12:58.472+00:00
expected result = 17/12/2019.
The code has to be in Jinja. Been trying to do the following, but getting an error message:
{{ 2019-12-17T08:12:58.472+00:00 | from.strftime('%Y-%m-%d') }}
Can anybody help?
This works for me to pull out a date in your requested "human readable" format %d/%m/%Y:
>>> from jinja2 import Template
>>> from datetime import datetime
>>> t = Template("Hello, {{ your_jinja_date_var.strftime('%d/%m/%Y') }}!")
>>> t.render(your_jinja_date_var=datetime.now())
'Hello, 17/12/2019!'
Here are the API docs where you can find other incredible features of jinja.
Your strftime function from your question above does imply standard ISO date conventions %Y-%m-%d. If you'd like that date format:
>>> t = Template("Hello, {{ your_jinja_date_var.strftime('%Y-%m-%d') }}!")
>>> t.render(your_jinja_date_var=datetime.now())
'Hello, 2019-12-17!'
And to top it off, here's a nice tutorial article on the topic which I found useful, from the Real Python platform.
<p>Last seen: {{ user.last_seen.strftime('%d-%m-%Y, %T') }}</p>
This worked for me for Date and Time both.
I am displaying a list of datetimes from my database and using twig to format. This was working until today.
If I dump this: {{ dump(time.clockedIn) }}
I get this:
DateTime {#612 ▼
+"date": "2017-02-03 17:54:20.000000"
+"timezone_type": 3
+"timezone": "America/New_York"
}
When I dump this: {{ dump(time.clockedIn|date("m/d/Y h:m:s a")) }}
I get this: "02/03/2017 05:02:20 pm"
I cannot find out why this changed. Any ideas?
Try this instead:
{{ dump(time.clockedIn|date("m/d/Y g:i:s a")) }
As per the Twig date documentation, you should be using g:i:s a.
Note that this documentation references PHP's date function, where you'll see the proper format characters that you can use.
How can i change the timestamp format into dateTime format in laravel 5, while fetching the timestamp from the table.?
<td>{{ $nam->date("Y-m-d H:i:s", lastLoginTime) }}</td>
When I tried this one, its showing error "Call to undefined method stdClass::date()".
Anyone help me to solve it.
In the Model you can add the below function to add dates to diffForHumans() function:
funtion getDates()
{
return array('created_at', 'updated_at', 'lastLoginTime');
}
etc etc.
Or you can use the following:
{{ $nam->lastLoginTime->format('Y-m-d H:i:s') }}
This is my code
{{ commentaire.dateCommentaire|localizeddate('medium', 'medium') }}
and this is the output
12 MARS 2016 20:35:21
I'd like to add a text between date and hour to be like this
12 MARS 2016 à 20:35:21
How can I do this ?
You need to use format option:
format: Optional pattern to use when formatting or parsing. Possible
patterns are documented in the ICU user guide.
{{ commentaire.dateCommentaire|localizeddate('medium', 'medium', format='dd MMM yyyy à HH:mm:ss') }}