How to show DateTime in Visualforce page for current and last year in Salesforce?

How to show DateTime in Visualforce page for current and last year in Salesforce?

Sample Code:
Visualforce Page:

<apex:page controller=”SamplePageController”>  
    Current Year Datetime is {!currentYearDT}<br/><br/>
    Last Year Datetime is {!lastYearDT}  
</apex:page>  

Apex Class:
public class SamplePageController {
    
    public DateTime currentYearDT { get; set; }
    public DateTime lastYearDT { get; set; }
    
    public SamplePageController() {
        
        currentYearDT = System.now();
        lastYearDT = currentYearDT.addMonths( -12 );
        
    }

}

Output:

Leave a Reply