You need to sign in to do that
Don't have an account?

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 );
}
}
}
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 );
}
}
}
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
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
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
filmplus app (http://filmplusapk.info/)
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:
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")