While working as a Salesforce developer, I got a requirement from the sales team for our organization regarding the data type conversion of an object field. The sales team wants to track opportunities that are closing soon. The Opportunity Close Date is stored as a DateTime field, but the team only wants the date (without time) to send out reminders for daily tracking purposes.
To solve this, I used Apex programming to convert the date value to a Datetime in Salesforce. In this Salesforce tutorial, I will explain the various possible methods to convert Date value to Datetime in Salesforce Apex.
Convert Date to DateTime Format in Salesforce Apex
In the steps below, we will explore the various methods for converting a Date to a DateTime Format in Salesforce Apex.
Convert Date to DateTime Format using newInstance() method in Salesforce Apex
To convert Dates to DateTime format in Salesforce Apex, navigate to the Salesforce developer console and execute the code below in the Apex anonymous window.
Using the Datetime.newInstance method, create a Datetime from a Date object by setting the time to 00:00:00.
Date newDate = Date.today();
Datetime newDateTime = Datetime.newInstance(newDate, Time.newInstance(0, 0, 0, 0));
System.debug('Datetime: ' + newDateTime);After executing the apex anonymous code, we can see that the date value is converted to DateTime format in the debug of the execution log window.

This way, we can convert the date values to a datetime format in Salesforce Apex using the newInstance() method.
Convert Date to DateTime Format using vaueOf() method in Salesforce Apex
This method of Date to DateTime conversion is useful when you have Date and time information as a string. The Datetime.valueOf method converts it directly into a Datetime.
Now, execute the code below in the Salesforce Apex anonymous window to convert the date value to DateTime format.
String dateString = '2024-10-15 12:30:45';
Datetime newDateTime = Datetime.valueOf(dateString);
System.debug('Date to Datetime formatted value : ' + newDateTime);After the code execution, you will see the debug of the execution log window, where the date value is converted to the datetime value in Salesforce Apex.

This way, we can convert the Date values to DateTime format in Salesforce Apex using the valueOf() method.
Convert Date to DateTime Format using DateTime.now() method in Salesforce Apex
We can use the DateTime.now() method to convert custom dates in Salesforce Apex from date to datetime format.
To change the format of the custom dates from date to datetime, open the Salesforce Apex anonymous window and execute the code below.
Date newDate = Date.newInstance(2024, 10, 15);
Datetime newDateTime = Datetime.now();
newDateTime = newDateTime.addDays(newDate.daysBetween(Date.today()));
System.debug('Datetime: ' + newDateTime);In the execution log window, we will see that the custom date value is formatted into DateTime, and it has returned the time according to the GMT timezone.

This way, we can convert a custom date value into a Datetime format in Salesforce Apex using the DateTime.now() method.
Convert Date to DateTime Format According to User’s Timezone in Salesforce Apex
To convert a Date to a DateTime format according to the user’s timezone, we can use the Timezone class to get the user’s local timezone.
Now, open the Salesforce Apex anonymous code window and execute the code below to convert the date to a datetime according to the user’s timezone.
TimeZone tz = UserInfo.getTimeZone();
Date userDate = Date.today();
Time userCurrentTime = Time.newInstance(Datetime.now().hour(), Datetime.now().minute(), Datetime.now().second(), 0);
Datetime utcDateTime = Datetime.newInstance(userDate, userCurrentTime);
Integer offsetMillis = tz.getOffset(utcDateTime);
Datetime localDateTime = utcDateTime.addSeconds(offsetMillis / 1000);
System.debug('UTC Datetime: ' + utcDateTime);
System.debug('Local Datetime: ' + localDateTime);In the above code, UserInfo.getTimeZone() will get the user’s timezone. Then, to get the current date and time, we have used Today.date() for date, and (Datetime.now().hour(), Datetime.now().minute(), Datetime.now().second() to get the time.
Then, we combined the date with the current time to get the UTC datetime. After this, using the tz.getOffset(utcDateTime), we adjusted the time according to the user’s local time zone.
Now, after executing the above code, you will see the value of UTC and the user’s timezone converted from date to datetime format.

This way, we can convert the Date value to Datetime in Salesforce Apex according to the user’s timezone in Salesforce Apex.
Conclusion
In this Salesforce tutorial, we have learned about the various methods to convert the date values from Date to DateTime format in Salesforce Apex. For this date to datetime conversion, we have used several methods such as newInstance(), valueOf(), and Datetime.now().
We also learned how to convert the date value to a datetime according to the user’s timezone using the function UserInfo.getTimeZone().
You may also like to read.
- Convert DateTime to Date Format in Salesforce Apex
- Convert String to Datetime in Salesforce Apex
- Convert String to Datetime in Salesforce Apex
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.