• Support Manager 2
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Hi, I am trying to add hours in a datetime in apex. It seems that it is not working, am I missing something?
Below is the code:
@istest

class DateFormatTest {

   static testmethod void test() {
        String myDtString;
        String timestart = '2016-09-02 18:15:26';
        Datetime dt = Datetime.valueOf(timestart);
        
        myDtString = dt.format('MM/dd/yyyy hh:mm a');
        System.debug('1: '+ myDtString); //result: 1: 09/02/2016 06:15 PM
        
        dt.addHours(3);
        myDtString = dt.format('MM/dd/yyyy hh:mm a');
        System.debug('2: '+ myDtString); //result: 2: 09/02/2016 06:15 PM

   }
}

If you look at it, the results are the same after adding 3 hours.
Hi, I am trying to add hours in a datetime in apex. It seems that it is not working, am I missing something?
Below is the code:
@istest

class DateFormatTest {

   static testmethod void test() {
        String myDtString;
        String timestart = '2016-09-02 18:15:26';
        Datetime dt = Datetime.valueOf(timestart);
        
        myDtString = dt.format('MM/dd/yyyy hh:mm a');
        System.debug('1: '+ myDtString); //result: 1: 09/02/2016 06:15 PM
        
        dt.addHours(3);
        myDtString = dt.format('MM/dd/yyyy hh:mm a');
        System.debug('2: '+ myDtString); //result: 2: 09/02/2016 06:15 PM

   }
}

If you look at it, the results are the same after adding 3 hours.