I'm trying to display a DateInterval in Twig using the following code:
{{ event.endTime.diff(event.startTime)|date("i's''") }}
where event is an Entity who get 2 DateTime object: endTime and startTime. With that command I've got this output:
i's''
Instead of min'sec'' like 08'15''
It is said in the date doc that
The date filter accepts [...] DateInterval instances
This work to display min and sec from a date object.
Note that doing: {{ (event.endTime.diff(event.startTime))|date("i's''") }} doesn't change anything
I also tried {{ date(event.endTime.diff(event.startTime))|date("i's''") }} but this lead to an exception Object of class DateInterval could not be converted to string
I've also seen time_diff from Twig Extensions but this return a string (in or ago) instead of a Date object, then I can't display it as I want.
Let me know if you need more informations. Thank you for your help.
As #Yoshi said:
(new \DateTime('tomorrow'))->diff(new \DateTime('now'))->format("i's''") also gives "i's''", so I'd say this is not a twig-thing. From the manual:
"The following characters are recognized in the format parameter
string. Each format character must be prefixed by a percent sign (%)."
So to fix my proble I just had to do:
{{ event.endTime.diff(event.startTime)|date("%i'%s''") }}
When putting in the method format the parameters 'i' and 's' in lowercase, when the value to be displayed is less than 10, put a single digit.
Example if you need to display the time of 5 minutes and 2 seconds by showing a leading zero (05:02). You have to capitalize the parameters, it would be like this:
{{ event.endTime.diff(event.startTime)|date("%I:%S") }}
The allowed parameters can be found in: DateInterval format
Related
I am trying to format the date using formatDate. None of the options is working in terms of formatting the date. Please suggest. We are using the insert tag to fetch values from the template.
{{insert notes_lead_creation_date}}
This date has to be formatted to DD/MM/YYYY. Currently the date has : notes_lead_creation_date:2058-07-25
Also, getting an error message.
This message has been dropped.
As per the documentation, you cannot format a date within an insert helper. If you are using insert to provide a fallback, you should use a conditional, like so:
{{#if notes_lead_creation_date}}
{{ formatDate notes_lead_creation_date "DD/MM/YYYY"}}
{{else}}
default content
{{/if}}
Note that the date must be provided in ISO8601 format too.
I need to add to my dbt model a column which shows the current date when the model is run.
I have tried {{ run_started_at.strftime("%Y-%m-%d") }} by adding it directly to my model and also have tried on setting a variable called date and creating this piece of code ( {% set report_date = dbt_utils.get_query_results_as_dict("select dateadd(day,-1,current_date()) as col")["COL"][0] %} ) but the problem is that the dates, as we are today at 2021-12-14, the result for this column is 1995 ( this is the substraction of 2021 - 12 - 14). So, does anybody now a way to cast this variable to a date? thanks!
Well I think I know the secret now
Please remember dbt is a tool for code-generating-like
In your case, I guess you must to wrap your jinja code in a string quote
WRONG:
SELECT {{ run_started_at.strftime("%Y-%m-%d") }} as YourColumn
--compiled: SELECT 2021-12-14 as YourColumn
--YourColumn=1995
CORRECT:
SELECT '{{ run_started_at.strftime("%Y-%m-%d") }}' as YourColumn
--compiled: SELECT '2021-12-14' as YourColumn
--YourColumn=2021-12-14
When I create a post, in the front matter I have to insert layout, title, date etc. My concern is regarding date front matter. It is very inconvenient to insert date time and time offset in date field manually.
I'm not able to figure out how to do this automatically. I want the front matter date to update automatically with system time.
The date variable in front matter is an optional variable that you can set to be able to override the date that you set when you title your post according what Jekyll is looking for (i.e. 2019-12-14-post-title.md). It should only refer to the date the post was originally made, and not the current system date. Except for certain edge cases, you really shouldn't have to define date in the front matter.
You can use Liquid templating to post the current time to a page by passing the "now" keyword to date in your liquid template as follows:
This page was last updated at {{ "now" | date: "%Y-%m-%d %H:%M" }}.
to get the the output:
This page was last updated at 2019-12-14 17:48.
However this method has a big drawback for Jekyll usage, because it will refer to the current date when the page was last generated from the template and not when the page is being visited by the user.
The best way to access the current system time is likely to use javascript instead. This is fairly easy depending on your familiarity with the language, but essentially you can make a Date object and then output the info you are looking to display. Here is a quick snippet I made that shows a couple methods that act on the Date object to output different values such as the date, time, and year:
<!DOCTYPE html>
<html>
<body>
<h3>The toDateString() method converts a date to a date string:</h3>
<p id="demo1"></p>
<h3>The toLocaleTimeString() method outputs formatted time:</h3>
<p id="demo2"></p>
<h3>The getFullYear() method outputs the year:</h3>
<p id="demo3"></p>
<script>
// create date object
var d = new Date();
// use toDateString() to output formatted date
document.getElementById("demo1").innerHTML = d.toDateString();
// use toLocaleTimeString() to output formatted time
document.getElementById("demo2").innerHTML = d.toLocaleTimeString();
// use getFullYear() to output year
document.getElementById("demo3").innerHTML = d.getFullYear();
</script>
</body>
</html>
W3schools has a good rundown of the Date object and how to format output if you need more examples and reference on usage.
If you need to know how to use Javascript with Jekyll there is a helpful post on Jekyll Talk that will help.
First of all, you do not HAVE to insert 'layout, title, date etc'. You can use Front Matter defaults for them. This only makes sense for the layout as the title can or should be default, nor the date.
The date MUST be set in your filename and can be overridden in the Front Matter. Please note that this is ONLY true for the built-in collection posts. If you use a custom collection you do not need a date at all.
However, if you choose to use posts nevertheless and you want to add the date automatically, there is only one real option: Use a CMS that automates this input, like Forestry.io or CloudCannon.
I got a Mediawiki-form with different attributes. If i got an optional text field which keeps empty most of the time. My problem is that even when im filling my text field with an empty string by default it will not be transferred to the template and generates ugly artefacts (a missing reference instead of a blank field).
Is there any way to make sure the field is transferred to the template?
Excerpt from my form:
! Priority:
|{{{field|Priority|input type=category|default=High|top
category=Priority|height=85|hideroot=on}}}
|-
! Date:
| {{{field|Date|input type=datepicker|default= {{CURRENTYEAR}}/
{{CURRENTMONTH}}/{{CURRENTDAY}} }}}
! Comment:
|{{{field|Comment|input type=text|default=" "|height=85|hideroot=on}}}
|}
It generates the template:
{{Template
|Priority=High
|Date=01/01/1970
}}
But it doesnt generate:
{{Template
|Priority=High
|Date=01/01/1970
|Comment=
}}
I tried this if-check in the template but if the attribute is null it shows {{{Comment}}}:
! [[Attribut:Comment| Comment]] | [[Comment::{{{Comment}}}
|{{#if:{{{Attribut:Comment}}}|{{{Comment}}}| }}]]
The problem is that blank. You can set the default to >non< and check is the comment string >non<.
! Comment:
|{{{field|Comment|input type=text|default="non"|height=85|hideroot=on}}}
|}
A other solution is to use ParserFunctions especially the if functionality
{{#if:{{{1|}}}|You entered text in variable 1|There is no text in variable 1 }}
Why i have double error when i want to customize my form ?
Template:
<div>
{{ form_errors(edit_form.wiek) }}
{{ form_widget(edit_form.wiek) }}
</div>
HTML:
You probably see the error on the field level, but also on the form level. It can look like you have two errors especially if there's only one field in your form.
Dig into the customizing error output of the form cookbook for more details.
Edit
The doc gives a much better explanation :)
You can also customize the error output for just one specific field type. For example, certain errors that are more global to your form (i.e. not specific to just one field) are rendered separately, usually at the top of your form.