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
Naga Balji BNaga Balji B 

how to separate date and time fields of createddate in page block table

VF page:
<apex:page controller="Time_date"  sidebar="false">
 <apex:form >
  <apex:pageBlock title="Displaying all records of s object">
   <apex:pageBlockSection columns="1">
    <apex:pageBlockTable var="b" value="{!bk}" cellpadding="4" cellspacing="4">
     
     <apex:column headervalue="Name" value="{!b.name}"/>
     <apex:column headervalue="EDITION" value="{!b.Edition__c}"/>
     <apex:column headervalue="Price" value="{!b.price__c}"/>
     <apex:column headervalue="Created Date">
     Date:<apex:outputText value="{!dt}"/>
     Time:<apex:outputText value="{!tm}"/>
     </apex:column>
     <apex:column headervalue="modified Date" value="{!b.LastModifiedDate}"/>
     
    </apex:pageBlockTable>
   </apex:pageBlockSection>
  </apex:pageBlock>
 </apex:form>
</apex:page>

Apex Class:

public with sharing class Time_date {

    public time tm { get; set; }

    public date dt { get; set; }

    public List<book__c> bk { get; set; }
    public Time_date(){
    bk=[select id,name,edition__c,price__c,Createddate,lastmodifieddate from book__c];
    //dt=DATEVALUE(CreatedDate);+
    //tm=MID(TEXT(CreatedDate - 0.1667), 12, 5);
   datetime Createddate;
    
    //dt=Createddate.date();  //display Error
    //tm=Createddate.Time();


 
    }
}

Output:
createddate field in out I want to display separate date and time

 
Best Answer chosen by Naga Balji B
Magesh Mani YadavMagesh Mani Yadav
Hi Naga,

I have updated you class and VF page. This should work for you.
Class
public with sharing class Time_date {
    public List<dateTimeWrapper> bk { get; set; }
    public Time_date(){
        bk = new List<dateTimeWrapper>();
        for(book__c aa : [select id,name,edition__c,price__c,Createddate,lastmodifieddate from book__c]){
            bk.add(new dateTimeWrapper(aa,aa.lastmodifieddate.date().format(),aa.lastmodifieddate.time()));
        } 
    }
    public class dateTimeWrapper{
        public book__c book {get;set;}
        public String LastDt {get;set;}
        public Time LastTm {get;set;}
        dateTimeWrapper(book__c book,String dt,Time tm){
            this.book = book;
            this.LastDt = dt;
            this.LastTm = tm;
        }
    }
}

vf Page
<apex:page controller="Time_date"  sidebar="false">
 <apex:form >
  <apex:pageBlock title="Displaying all records of s object">
   <apex:pageBlockSection columns="1">
    <apex:pageBlockTable var="b" value="{!bk}" cellpadding="4" cellspacing="4">
     
     <apex:column headervalue="Name" value="{!b.book.name}"/>
     <apex:column headervalue="EDITION" value="{!b.book.Edition__c}"/>
     <apex:column headervalue="Price" value="{!b.book.price__c}"/>
     <apex:column headervalue="Created Date">
     Date:<apex:outputText value="{!b.LastDt}"/>
     Time:<apex:outputText value="{!b.LastTm}"/>
     </apex:column>
     <apex:column headervalue="modified Date" value="{!b.book.LastModifiedDate}"/>
     
    </apex:pageBlockTable>
   </apex:pageBlockSection>
  </apex:pageBlock>
 </apex:form>
</apex:page>

Hope your problem is resolved.

All Answers

badibadi
Try this,
 
Date: <apex:outputText value="{0, date, MM'/'dd'/'yyyy}">
 <apex:param value="{!b.LastModifiedDate}" />
</apex:outputText>

Time: <apex:outputText value="{0, date, HH':'mm':'ss}">
<apex:param value="{!b.LastModifiedDate}" />
</apex:outputText>

 
Naga Balji BNaga Balji B
Thank you Bhavana Adiga....but its not working proper way in pageblock table.its showing mismatches of lastmodifiedDate & Time with respected records.User-added image
Magesh Mani YadavMagesh Mani Yadav
Hi Naga,

I have updated you class and VF page. This should work for you.
Class
public with sharing class Time_date {
    public List<dateTimeWrapper> bk { get; set; }
    public Time_date(){
        bk = new List<dateTimeWrapper>();
        for(book__c aa : [select id,name,edition__c,price__c,Createddate,lastmodifieddate from book__c]){
            bk.add(new dateTimeWrapper(aa,aa.lastmodifieddate.date().format(),aa.lastmodifieddate.time()));
        } 
    }
    public class dateTimeWrapper{
        public book__c book {get;set;}
        public String LastDt {get;set;}
        public Time LastTm {get;set;}
        dateTimeWrapper(book__c book,String dt,Time tm){
            this.book = book;
            this.LastDt = dt;
            this.LastTm = tm;
        }
    }
}

vf Page
<apex:page controller="Time_date"  sidebar="false">
 <apex:form >
  <apex:pageBlock title="Displaying all records of s object">
   <apex:pageBlockSection columns="1">
    <apex:pageBlockTable var="b" value="{!bk}" cellpadding="4" cellspacing="4">
     
     <apex:column headervalue="Name" value="{!b.book.name}"/>
     <apex:column headervalue="EDITION" value="{!b.book.Edition__c}"/>
     <apex:column headervalue="Price" value="{!b.book.price__c}"/>
     <apex:column headervalue="Created Date">
     Date:<apex:outputText value="{!b.LastDt}"/>
     Time:<apex:outputText value="{!b.LastTm}"/>
     </apex:column>
     <apex:column headervalue="modified Date" value="{!b.book.LastModifiedDate}"/>
     
    </apex:pageBlockTable>
   </apex:pageBlockSection>
  </apex:pageBlock>
 </apex:form>
</apex:page>

Hope your problem is resolved.
This was selected as the best answer
Naga Balji BNaga Balji B
Hi Magesh,

if i am use above code...getting Below error .
Error: Time_date Compile Error: The property Date dt is referenced by Visualforce Page (Task40_Time_date111) in salesforce.com. Remove the usage and try again. at line 5 column 17
Magesh Mani YadavMagesh Mani Yadav
Hi Naga,
First try to comment your entire vf page and then save your class. Once its saved then uncomment the vf ppage and save it. should work
Naga Balji BNaga Balji B
Hi Magesh,
Thanks for your fast response...ur coding works good but in time zone output shows excess...unwanted "000Z".how to remove it. is there any simple way to time shows in 12 hours mode(am,pm). and alsodate dispalay like jan,feb etc(like ..12th jan 2016)Display unwanted data  or excess
 
Magesh Mani YadavMagesh Mani Yadav

Hi Naga,
here is the link to the time class and its method. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_time.htm
you can do some thing like this : 
Time tt = aa.lastmodifieddate.time();
String timeFormat = tt.hour()+':'+tt.time().minute()+':'+tt.time().second();
System.debug('timeFormat: '+timeFormat);

OR you can simply remove the last "000Z" by
String time = ''+aa.lastmodifieddate.time();
String formattedTm = time.mid(0,8);
Result will be : 00:02:02

In either way you have to then update the datatype of
public Time LastTm {get;set;}
to 
public String LastTm {get;set;}