How do I render time in twig without time zone interference - twig

I want to grab the time only from a Drupal Date Field, and render it in a twig template separately from its date.
In my twig template for Events I currently have date listed 3 ways:
{{ content }}
displays all the fields.
And the time (sourced from a Date field) displays correctly as;
28/04/2019 - 1:11
This is the correct time as per what was entered when the content was created.
On the same page, when using:
{{ content.field_date_start }}
28/04/2019 - 1:11
is also displayed corrected.
But I want to grab the time only to display separately from the date
so I put this in the twig:
{{ node.field_date_start.value|date("g:ia") }}
which displays as:
3:11am
This is incorrect, off by 14hours (the 1.11 when entered was 1.11 pm)
How do I separate the time to a separate display within twig, but also retain the correct time as per how it was entered.
How do I enter that into a twig?
I don't want the time to change if people are entering content or viewing content in different time zones.
Example - someone creates content in Melbourne, Australia and someone else views it in London, England - the time should display the same regardless.

Drupal saves dates in the database as per the UTC timezone. E.g If you enter 09:00 AM as the time as a user who has a timezone of London (UTC + 1) selected in the profile, it is saved as 08:00 AM in the database. Same is true if the site's timezone is set to London (UTC +1).
I would suggest preprocessing the field and setting a variable for the start time.
function HOOK_preprocess_node(&$variables) {
$node = $variables['elements']['#node'];
$timezone = drupal_get_user_timezone();
$formatter = \Drupal::service('date.formatter');
$field_date_start = $node->get('field_date_start')->getValue()->getTimestamp();
$time = $formatter->format($field_date_start, 'custom', 'g:ia', $timezone);
$variables['start_time'] = $time;
}
And then print the value in the template as
{{ start_time }}

Related

DOB field as date tab prompts date picker

I have a date field and it has to be a date field as I need to run formula on the field to identify is the person is over 18 or not. Formula field is to find if dob is 18 or over. And date formula's does not work on text field.
Now the issue is, because it is a date field, docusign prompts date picker and any date format I provide either through validation or by changing the regional setting does not change the date format. It is always 2021-06-24T12:00:00-04:00 (ie YYYY-MM-DDTHH24:MI:SS).
Date tab gets added using API and so are the validationPattern. Please can you help me with the following issues.
How can I stop the date picker from showing?
Even if the date picker shows up, how to control the date format. I am looking for mm/dd/yyyy
Regional settings, Time Zone does not have a Date format without timestamp. Inspite of picking something from there, date picker does not match what was picked.
Update, you can use formulateTab to calculate the date in 18 years like this:
AddYears( [Text <GUID>] , 18)
Note above is the unique ID of the text tab where the user enters their DOB.
Hope that helps.
Make it into a text field and you can then just use Date validation with one of the formats below: (make sure it works for you first from the web app - you can later do this all with the eSign API)

Changing entry post date format to DD:MM:YYYY

I am trying to change the date format in Craft 3 (Entries > all entries > "post date" column) from (MM:DD:YYYY) to (DD:MM:YYYY)
At the moment it is converting it to Iso8601.
Locales are set correctly. Have searched high and low, can't seem to find anything.
You can change date format directly inside twig template.
For example:
{{ date_post|date('DD:MM:YYYY') }}
Look at official docs about twig

How can i display date and time in a field with date type

How can i display the current date and time in a field with date type.
I can show the current date with today() but how can i display the current time.
Furthermore i want display these two in one field.
Thanks for every help.
Felix
I am afraid that is not possible as a field of type date holds just that - the date. Use type utcdatetime and the appropriate control UtcDateTimeEdit to show dates & times in one control.
To retrieve the current date & time you can call DateTimeUtil::utcNow() to which you may want to apply the local time zone via DateTimeUtil::applyTimeZoneOffset
Try this:
utcDateTime _utcDateTime;
;
_utcDateTime = DateTimeUtil::getSystemDateTime();
Then you can extract separately the date and the time if you want.

How can I set the default value of a DateTime field in a SharePoint list to also contain the current time?

I have a DateTime field in my SharePoint 2013 list. I have specified the default value for the field to be =Today. The problem is this only specifies the date part of the field. The time part is left as 00:00. How can I get the time information in there as well.
I want to present the user with the current time as default, which will be used most of the time. However, it should be possible for the user to be able to specify a different time if required.
=Today() gives only the current date, =Now() gives the current date and time.
yourDefaultDate = DateTime.Now();
instead of DateTime.Today();

How to get Date and current time from the Date/Time column in SharePoint Custom list

I have column called "Date Submitted" as Date/time in one of the Custom list in sharepoint 2007.
it always set to today's date and 12AM time instead of that I want to display today's date with current time hh:mm:ss.
I tried creating calculated column TestDate and formula is : =TEXT(([Date Submitted]),"mm dd yyyy h:MM:SS")
result is 04 28 2010 0:00:00 I wanted to be 04/28/2010 10:50:34
Is it possible to achive this?
Thank you
kanta
try to put =NOW() in the default value field in the column properties
In the definition of Date Submitted, is the Date and Time Format currently Date Only? If so, try changing it to Date & Time.
For something like this, I ended up creating a custom action which is called from a workflow.
A few ideas to get you started :-
Can you use the Created date instead - it will always be set to the exact time the record was created?
When entering a new item on the Calendar list it defaults to the current time - could you make your list from a modified Calendar list rather than the Custom list?
I've written a blog post about how you can use a JavaScript hack to set the default duration for a new calendar record - you could modify this to use with a Custom list and to set the field to the current time.
Setting a default duration for new calendar events
You could write your own custom field type
SharePoint 2007 Custom Date Time Field to default the time to the current time
Codeplex - custom date time field
=TEXT([DateFiled Here],"mm/dd/YYYY hh:mm:ss ")
Here's how to do what you want:
=TEXT(Today,"mm/dd/YYYY")&" "&TEXT(Now(),"h:mm:ss")

Resources