twig Date and time: Format, set Timezone and Language – twig-Attribute syntax

Task:
Format the date and time from a JSON-API. Also set the timezone and language.

If only simple formatting is required: see here.


Details for “dateformat”:
The twig-extension “dateformat” accepts three arguments:

  • The format of the date, see PHP strftime for that.
  • The second optional argument sets the Timezone, see PHP List of Supported Timezones for more. If empty the default value of the PHP-Installation is used.
  • The third optional argument sets the Language, see PHP Locale for more. If empty the default value of the PHP-Installation is used.

    In case of encodein problems add “.UTF-8” like this: {{JSONFIELD | dateformat(“%B %Y”, ‘Europe/Berlin’, ‘de_DE.UTF-8’)}}

Also note this syntax:
{{ attribute(c.definition.parkingRecord.parkingRecord, ‘@id’) }}
If a JSON-Node has a “special Name” like having special Chars or numbers you’re safe with such a syntax.

JCI-Shortcode:

[[jsoncontentimporterpro nameoftemplate="nottingham-parking.json"]]

JCI-Template:

{% for c in carParks.carPark | slice(0,3) %}
{{ attribute(c.definition.parkingRecord.parkingRecord, '@id') }} - parkingRecordVersionTime: 
{{ c.status.parkingRecord.parkingRecordStatus.parkingStatusOriginTime  | dateformat('%A %m/%d/%y %H:%M:%S ', 'Europe/Berlin', 'nl_NL')}}
<br>
parkingSiteStatus: {{c.status.parkingRecord.parkingRecordStatus.parkingSiteStatus }}<br>
parkingSiteOpeningStatus: {{c.status.parkingRecord.parkingRecordStatus.parkingSiteOpeningStatus}}<br>
measurementOrCalculationTime: {{c.status.parkingRecord.parkingRecordStatus.parkingOccupancy.vehicleCountAndRate.vehicleRate.measurementOrCalculationTime | date("d.m.Y, H:i") }}<br>
fillRate: {{c.status.parkingRecord.parkingRecordStatus.parkingOccupancy.vehicleCountAndRate.vehicleRate.fillRate.vehicleFlowRate}}<br>
exitRate: {{c.status.parkingRecord.parkingRecordStatus.parkingOccupancy.vehicleCountAndRate.vehicleRate.exitRate.vehicleFlowRate}}<hr>
{% endfor %}

In Action:

12233069 – parkingRecordVersionTime: donderdag 03/12/20 16:28:18
parkingSiteStatus: spacesAvailable
parkingSiteOpeningStatus: open
measurementOrCalculationTime: 07.08.2020, 21:46
fillRate: 0
exitRate: 0
12233071 – parkingRecordVersionTime: maandag 03/09/20 13:17:42
parkingSiteStatus: spacesAvailable
parkingSiteOpeningStatus: open
measurementOrCalculationTime: 07.08.2020, 21:46
fillRate: 0
exitRate: 0
12233072 – parkingRecordVersionTime: vrijdag 03/13/20 13:21:07
parkingSiteStatus: spacesAvailable
parkingSiteOpeningStatus: open
measurementOrCalculationTime: 07.08.2020, 21:46
fillRate: 0
exitRate: 0