0

PROBLEM: I converted a string to Date format. Eg: String dateToParse = year+'-'+month1+'-'+day; Date ds = Date.parse(dateToParse); OUTPUT: 2020-07-24 00:00:00 (output is a Date type variable)

Expected: OUTPUT: 2020-07-24 (without the time 00:00:00, still maintaining the Date type variable) Note: I know that using ds.format() would give me the expected output, but this converts it into a string. I want to maintain the date type format.

2 Answers 2

0

Date types are intrinsically DateTime types with the time factor set to 00:00:00. You can't have a Date that does not have a time factor. What you're asking for is impossible.

7
  • Thank you for clearing that! Can i use the current date format in ds date variable to query something else. For e.g. Select Name, OrderNumber__c from Opportunity where CloseDate =: ds
    – Raj
    Jul 24, 2020 at 18:56
  • @Raj Yes, that's perfectly fine.
    – sfdcfox
    Jul 24, 2020 at 18:57
  • Thank you again
    – Raj
    Jul 24, 2020 at 19:01
  • @sfdcfox - Salesforce contradict your statement here in this documentation: developer.salesforce.com/docs/component-library/bundle/… I have always understood your statement to be very correct. But this documentation is suggesting otherwise. Even though they then contradict themselves by say you must use a UTC stamp to ensure it stays on the same date. :) Jun 25, 2021 at 11:11
  • They say: "Date and Time Stored in Salesforce Salesforce uses the ISO8601 format YYYY-MM-DD to store date fields, which store a date without time, and includes no time zone information. When formatting dates without time, include time-zone="UTC" to ensure the correct date displays." Jun 25, 2021 at 11:11
0

This is actually a comment to previous answer. Given the content size , I am adding this as an answer.

@sfdcfox, thank you for your input as always. There seems to be an issue when converting Date fields into DateTime and then formatting the value. Take a look at below code. "parsedDateTime.date()" is showing the value off by 1 day. The reason for this complex code is I would like to show Date and DateTime fields either by UserLocale or the Format provided by the user (dateFormat and timeFormat)

sObject sObj = [SELECT Id, LastModifiedDate, CreatedDate, CloseDate FROM Opportunity WHERE Name = 'Test 2' LIMIT 1];
Object val = sObj.get('CloseDate');
if(val instanceOf Date) {        
    val = Date.valueOf(val).format();
} else if(val instanceOf DateTime) {        
    val = DateTime.valueOf(val).format();
}
system.debug('val ' + val);

String unformattedValue = String.valueOf(val);
system.debug('unformattedValue ' + unformattedValue); // Output: 03.12.2019

DateTime parsedDateTime = unformattedValue.contains(' ') ? DateTime.parse(unformattedValue) : Date.parse(unformattedValue);
system.debug('parsedDateTime ' + parsedDateTime); // Output: 2019-12-03 00:00:00

system.debug('parsedDateTime.date() ' + parsedDateTime.date()); // Output: 2019-12-02 00:00:00

String dateFormat = 'M/d/yyyy';
String timeFormat = 'hh:mm a';
system.debug('with dateFormat: ' + parsedDateTime.format(dateFormat)); // Output: 12/2/2019
system.debug('without dateFormat: ' + parsedDateTime.date().format()); // Output: 02.12.2019
1
  • Instead of converting a Date field into DateTime, I am constructing DateTime using below logic which seems to be working. ----- if(unformattedValue.contains(' ')) { parsedDateTime = DateTime.parse(unformattedValue); } // Handle Date fields else { Date parsedDate = Date.parse(unformattedValue); parsedDateTime = Datetime.newInstance(parsedDate.year(), parsedDate.month(), parsedDate.day()); } .... Once I have accurate "parsedDateTime" value, I can continue with rest of the logic. Nov 1, 2023 at 16:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .