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
Ratheven SivarajahRatheven Sivarajah 

How do you use a date in apex

I am writing a trigger where I am bringing in a Date/Field field from case. It has a letter in it 2023-02-10T16:15:35.000+0000 and when I try to subtract it with "DateTime.now()" I get an error "Date/time expressions must use Integer or Double or Decimal ".

This is my code 
trigger CaseTrigger on Case (before update) {
     for(Case cases: Trigger.new){ 
         if(cases.stopped_time__c != null){
             cases.Total_amount_of_time_in_Hours__c= (DateTime.now()-cases.stopped_time__c );
         } 
    }
}
Best Answer chosen by Ratheven Sivarajah
AISHIK BANERJEE 15AISHIK BANERJEE 15
Hello Ratheven,
                          
cases.Total_amount_of_time_in_Hours__c= (DateTime.now().getTime()-cases.ClosedDate.getTime() )/(1000.0 * 60.0 * 60.0);

In this code, we use the getTime method to convert the DateTime objects to the number of milliseconds, and then divide that number by the number of milliseconds in an hour to get the total number of hours.

I hope this will help.
Thanks

All Answers

Abdul KhatriAbdul Khatri
Hi Ratheven,

I hope the following article will help you how to move forward, referencing the issue you are getting
https://www.xgeek.net/salesforce/calculate-the-difference-between-two-datetimes-in-salesforce/

For More about DateTime Date functionality in Salesforce here is a trailhead
https://trailhead.salesforce.com/content/learn/modules/advanced_formulas/date_formulas

I hope this will help
AISHIK BANERJEE 15AISHIK BANERJEE 15
Hello Ratheven,
                          
cases.Total_amount_of_time_in_Hours__c= (DateTime.now().getTime()-cases.ClosedDate.getTime() )/(1000.0 * 60.0 * 60.0);

In this code, we use the getTime method to convert the DateTime objects to the number of milliseconds, and then divide that number by the number of milliseconds in an hour to get the total number of hours.

I hope this will help.
Thanks
This was selected as the best answer
kiwis brownkiwis brown
yes this is very helpful for me.
filmplus app (http://filmplusapk.info/)
alex hmaalex hma
To use a Date/Time field in Apex, you need to first convert it to a DateTime data type using the DateTime.valueOf() method. This method takes a string as input and returns a DateTime value. Source (https://migrationunity.com/)

In your case, you can use the DateTime.valueOf() method to convert the string date from the Case record to a DateTime value, like this:
DateTime stoppedTime = DateTime.valueOf(cases.stopped_time__c);

Then, you can subtract this DateTime value from DateTime.now() to get the time difference in milliseconds, like this:

Long timeDifferenceInMillis = DateTime.now().getTime() - stoppedTime.getTime();

To convert the time difference from milliseconds to hours, you can divide it by 3600000 (the number of milliseconds in an hour), like this:

Double timeDifferenceInHours = timeDifferenceInMillis / 3600000.0;

So your final code should look something like this:

trigger CaseTrigger on Case (before update) {
    for(Case cases : Trigger.new) { 
        if(cases.stopped_time__c != null) {
            DateTime stoppedTime = DateTime.valueOf(cases.stopped_time__c);
            Long timeDifferenceInMillis = DateTime.now().getTime() - stoppedTime.getTime();
            Double timeDifferenceInHours = timeDifferenceInMillis / 3600000.0;
            cases.Total_amount_of_time_in_Hours__c = timeDifferenceInHours;
        } 
    }
}

 
jidin maryajidin marya
It's very useful to me thanks for sharing. Michaels Worksmart ETM Login (https://tractorsinfo.com/michaels-worksmart-login/" target="_blank)
hyden matt007hyden matt007

In Salesforce Apex, you can work with dates using the Date data type and various date-related functions and methods. Here are some common tasks related to working with dates in Apex:
Declaring Date Variables: You can declare date variables like this:
apexCopy code
Date myDate = Date.today();
This initializes myDate with the current date.
Date Literals: You can use date literals to specify a date in a specific format:
apexCopy code
Date specificDate = Date  how to get temporary email address  (https://tempmailpro.app/)  .valueOf('2023-09-07');
This sets specificDate to September 7, 2023.
Date Methods: Apex provides several methods to work with dates. Here are some examples:
apexCopy code
Date today = Date.today(); // Current date // Adding days to a date Date futureDate = today.addDays(7); // Subtracting days from a date Date pastDate = today.addDays(-7); // Comparing two dates if (today > specificDate) { // Do something }
Date Formatting: You can format dates as strings using the format() method:
apexCopy code
Date myDate = Date.today(); String formattedDate = myDate.format(); System.debug(formattedDate); // Outputs in the default format (e.g., "2023-09-07")
You can also specify a custom date format using the format() method:
apexCopy code
String customFormattedDate = myDate.format('MM/dd/yyyy'); System.debug(customFormattedDate); // Outputs in the custom format (e.g., "09/07/2023")