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') }}
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.
Ok, so i want to change a datetime value from mysql database into a desired format. so my code goes:
file: app.py
from flask import Flask, render_template, flash, redirect, url_for, session, request, logging
from datetime import datetime
from flask_moment import Moment
app = Flask(__name__)
moment = Moment(app)
mysql = MySQL(cursorclass=DictCursor)
#app.route('/users')
def users():
# create cursor
cur = mysql.get_db().cursor()
# execute query
cur.execute('SELECT * FROM users')
# get results
users = cur.fetchall()
return render_template('users.html', title = 'User Accounts', users=users)
then on my users.html, i display the datetime value on a table which goes:
file: users.html
<tbody>
{% for row in users %}
<tr>
<td>{{row.id}}</td>
<td>{{row.username}}</td>
<td>{{row.fullname}}</td>
<td>{{moment(row.created_at).format('LLL')}}</td> # this causes an error
</tr>
{% endfor %}
</tbody>
But when i put in the following code for the datetime:
<td>{{moment().format('LLL')}}</td> # this is working
So in short,
# this is not working
# Causes an "AttributeError: 'str' object has no attribute 'strftime'" error
moment(row.created_at).format('LLL')
# this is working
# but i can't modify the data based on the value from mysql database
moment().format('LLL')
# by the way, this is also not working
# and it also causes the same error
row.created_at.strftime('%M %d, %Y')
What i need to know is how to format datetime value in the flask template and Flask-Moment seems to be the only way
EDIT:
You can also try using:
{{ row.created_at.strftime('%M %d, %Y') }}
or the jinja datetime filter:
{{ row.created_at|datetime }}
ORIGINAL ANSWER:
It appears you need to convert from MySQL DateTime to Python datetime. Without seeing more of your code, maybe something like this would work, though there is probably a more efficient way:
# get results
users = cur.fetchall()
for user in users:
user.created_at = datetime.strptime(user.created_at, '%Y-%M-%d') # you have to use the mysql format here
return render_template('users.html', title = 'User Accounts', users=users)
You are using strftime when it appears that python is interpreting the MySQL datetime as a string, not a datetime() object, so you have to use strptime
Momentjs couldn't parse it because it is an 'str'. Use datetime to parse the string before passing it to the momentjs object e.g
from datetime import datetime
row_created_at = datetime.strptime(row_created_at, %m %d,%Y ')
then pass it to the momentjs object like so
{{moment(row_created).format("LLL")}}
That should do the trick
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.
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