function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sandy GaliSandy Gali 

Getting hour from a datetime field

   I am trying to retrieve hour from a datetime field. I am running a SOQL query and retrieving a set of values in my apex class and looping through them in my visualforce something like
 <apex:repeat var="arr" value="{!arrivals}">
 {!arr.Pickup_Time__c}' where arrivals is my soql querry in apex which retrieves a set of values.
Pickup_Time__c is a datetime field and I want to get only hour out of it.

I dont want to use formula fields as they are other issues of DST, timezone etc.

Can any help me with this.


Provide formatting on page :  Assuming arr.Pickup_Time__c is date time field
   <apex:outputText value="The unformatted time right now is: {!arr.Pickup_Time__c}" />
   <apex:outputText value="Hour in day (0-23): {0, date, HH}">
       <apex:param value="{!arr.Pickup_Time__c}" />
   <apex:outputText value="Hour in day (1-24): {0, date, kk}"> 
      <apex:param value="{!arr.Pickup_Time__c}" />   

Sandy GaliSandy Gali
Hi Vineet,
Thanks for the response. I acutally want to use the hour value for comparision.
Say for ex: if hour value is in between 12 to 20 hours, do something.
That you may not be able to go with the above provided way.
I would suggest you, to create a wrapper class and using List<WrapperObject> to iterate rather than directly using the List<records> from your SOQL query.

Wrapper class would be something like this :
public class ArrivalWrapper{
    public Arrival__c arrivalRecord{get; set;}
    public Integer hour{get; set;}
    public ArrivalWrapper(Arrival__c arrivalRecord, Integer hour)
        arrivalRecord = new arrivalRecord();
        this.arrivalRecord = arrivalRecord;
        this.hour = hour;

List<ArrivalWrapper> awObject = new List<ArrivalWrapper>();

Sandy GaliSandy Gali
Thanks Vineet
Happy to help.
Please help the community by marking the answer as "Best Answer" right below the comment, it would help if some faces similar problem.