0

I have a time field in my content type. But when I entered time in the format 05:00 AM, I get a five digit timestamp eg. 18000 (for 05:00 AM).

When I print this using node.field_time.value|date('h:i A') it prints

10:30 AM

which is 05:30 Hrs extra than the entered one. How do I'll get the correct time?

The default timezone is set to Kolkata which is +05:30 UTC. Is this something causing the problem?

9
  • By default, it considers the entered time as UTC. So when we render the time field, it appears with respect to the user's time zone. Maybe by using drupal_get_user_timezone().
    – Hashmat
    Jan 19 '19 at 11:47
  • Yes, simply piping to the date format won’t take time zone into account as it’s just a value, as opposed to letting Drupal render a date value where it will do that.
    – Kevin
    Jan 19 '19 at 14:20
  • 2
    I think when you install a field module you should also configure the field formatter of this module and then print the formatted output, for example with {{ content.field_time }}.
    – 4k4
    Jan 19 '19 at 16:50
  • @Kevin I use node.field_time.value|date('h:i A', 'UTC') and get the desired output. But have doubt if it is a correct way or not.
    – Hashmat
    Jan 19 '19 at 17:36
  • 1
    @Kevin, the module has a field formatter cgit.drupalcode.org/time_field/tree/src/Plugin/Field/…. Hashmat, configure the field formatter in Manage Display of the content type where you've added the field.
    – 4k4
    Jan 19 '19 at 18:09
1

By default, it considers the entered time as UTC. So when we render the value of the time field, it prints the value according to the user's timezone.

I used below code to print the same value as entered in the backend

node.field_time.value|date('h:i A', 'UTC')

or we can also use false instead of UTC to get the same output without setting any time zone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.