Skip to main content
31 votes
Accepted

How to set a Date in apex test class?

You can do this using Apex' Date class: acc.effectiveDate__c = Date.newInstance(2016, 12, 9); For more information check out the documentation: Apex Developer Guide - Date Class
Markus Dang - formerly Slabina's user avatar
29 votes
Accepted

System vs Date/DateTime class

They give you the same data. Their descriptions are almost verbatim as well: System.now() Returns the current date and time in the GMT time zone. Datetime.now() Returns the current Datetime based on ...
Adrian Larson's user avatar
  • 151k
27 votes

Converting Date to DateTime

The most simple way to convert a date to a date time, with a time component of 00:00:00.0, is to simply assign a Date to a Datetime variable, or to cast a Date to a Datetime. For example, the ...
Phil W's user avatar
  • 38.1k
27 votes
Accepted

Does LAST_N_DAYS:1 Include TODAY?

In contradiction to what the current documentation claims, you can observe that the LAST_N_DAYS date literal includes today's data. To be sure, I ran this query: SELECT CreatedDate FROM MyObject__c ...
Adrian Larson's user avatar
  • 151k
17 votes
Accepted

How To Format Datetime like 2017-01-10T14:53:55.000+0000?

Using the example given in the DateTime class and calling the formatGMT(string) method with a formatting string found in the Java SimpleDateFormat examples: DateTime myDateTime = DateTime.newInstance(...
Mark Pond's user avatar
  • 23.1k
14 votes

System vs Date/DateTime class

Historically, only the System methods were originally available (System.now and System.today). The newer methods were added later, presumably because people felt it was logical that you'd find the ...
sfdcfox's user avatar
  • 502k
14 votes
Accepted

Date.newInstance() bug?

Date.newInstance actually seems to behave as follows: public class Date { public static Date newInstance(Integer year, Integer month, Integer day) { Date d = new Date(1, 1, 1); d....
sfdcfox's user avatar
  • 502k
14 votes
Accepted

Apex - Getting list of dates in month - CPU timeout

The addDays method does not modify the value but instead returns the modified date. So you need: currentDay = currentDay.addDays(1); Your current code hits the CPU limit because currentDay never ...
Keith C's user avatar
  • 137k
10 votes
Accepted

Illegal assignment from Datetime to Date

You can get the Date value from a Datetime instance by calling the date() method: obj2.Date__c = obj1.CreatedDate.date(); If you're calling this method on a Datetime instance where you are not sure ...
Adrian Larson's user avatar
  • 151k
10 votes
Accepted

How to pass Date from Lightning to Apex

I'm afraid you won't be able to do it. As @itzmukeshy7 said, the way to go is to pass the Date as a String and then cast it inside your Apex method. There are several issues like this currently in the ...
Fabien Taillon's user avatar
10 votes

Issue in Finding Difference between Date's in LWC

The event.target.value is a string, not a date. You need to convert the value to date first then you need to subtract it. import { LightningElement, track } from 'lwc'; export default class ...
Rahul Gawale's user avatar
  • 12.3k
9 votes

How to change date format depends on user locale?

There is a Date method for that: format() Returns the Date as a string using the locale of the context user Here are what those formats would look like: LocaleSidKey toLabel(LocaleSidKey) ...
Adrian Larson's user avatar
  • 151k
9 votes

What are the minimum and maximum valid dates in Salesforce?

Initially I have found an answer here, but the link provided in that answer, was broken. Finally found a corresponding valid link in the documentation Minimum and Maximum Dates Only dates ...
Patlatus's user avatar
  • 17.8k
9 votes
Accepted

Format date as two digit month and day

Only the Datetime type has a format method that lets you specify what format to use (instead of just the locale default). Likely that is because SimpleDateFormat supports many time components in ...
Adrian Larson's user avatar
  • 151k
7 votes

How Can I Tell the Day of the Week of a Date?

For reference, an Apex code version of the modular division approach is: public static Integer dayOfWeekNumber(Date aDate) { return Math.mod(Date.newInstance(1900, 1, 7).daysBetween(aDate),7); } ...
Rick Coates's user avatar
7 votes
Accepted

Datetime to Date to String

If you want to specify a particular format (not just the default for your locale), leave it as Datetime. That type has a format method signature which accepts a String. You can supply any supported ...
Adrian Larson's user avatar
  • 151k
7 votes

How to set a Date in apex test class?

Another option is using system.today() (or system.now() if it's datetime field): acc.effectiveDate__c = System.today() + 5;
Liron C's user avatar
  • 1,818
7 votes

Date in RFC2822 to Apex DateTime?

I'm not aware of any standard way to parse this, but here's a quick method to do it: Map<String, String> months = new Map<String, String> { 'Jan' => '01', 'Feb' => '02', ...
IllusiveBrian's user avatar
7 votes

Test.setCreatedDate not working in SOQL Query with condition date set in future

It seems that you can set a CreatedDate on a record in the future, but when querying for that record you cannot use a date in the future as condition Although it is not likely that you will want to ...
Wim's user avatar
  • 584
7 votes
Accepted

Is there a limitation of date value while parsing in apex

Yes, there is a limitation. It is documented on https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm at the bottom of the page ...
Derek F's user avatar
  • 64.1k
7 votes

How to remove time from date?

only way I found is to keep the date format and remove the 00 from end System.debug('Current date: ' + String.valueOf(Date.today()).removeEnd(' 00:00:00'));
Ratan Paul's user avatar
  • 22.8k
7 votes
Accepted

How to retrieve the correct Account records from SOQL query?

I recommend you read up on Date Formats and Date Literals in the SOQL and SOSL Reference. This resource covers several ways you can format date fields that you may find useful in the future. ...
Adrian Larson's user avatar
  • 151k
6 votes
Accepted

How to get the last monday of every month

You just need to do some back-and-forth calculation to end up where you want. while(enddate >= startdate) { // Remember current month Integer currentMonth = startDate.month(); ...
sfdcfox's user avatar
  • 502k
6 votes
Accepted

replacing LAST_MONTH

You might consider some other SOQL Date Literals such as LAST_N_MONTHS. Date Literal LAST_N_MONTHS:n Rage For the number n provided, starts 00:00:00 of the last day of the previous month and ...
Adrian Larson's user avatar
  • 151k
6 votes
Accepted

Lightning Component - pass variables from component to apex & get result back

You're encountering a little-known bug in Lightning that occurs when your JS function has the same name as your controller's Apex method name (in you case, format_date). The code goes into recursion ...
Sebastian Kessel's user avatar
6 votes
Accepted

Get Difference Between 2 Dates in Months Without Exceeding 5000 Characters

Because my comment appears to have been well-received (and for sake of having an answer on this question), I'll throw in my two cents. I don't think using a formula field is appropriate here. I know ...
Derek F's user avatar
  • 64.1k
6 votes
Accepted

Old style JOIN (ANSI JOINs) syntax is not allowed. Split DATE

The SQL parser in SFMC is really dumb sometimes. I'd recommend something like what @Bo Hu suggested, but just add brackets around it: SELECT CUST_CODE , [EXCLUDED_FROM_NPS] , BIRTH_DATE , ...
Adam Spriggs's user avatar
  • 31.1k
6 votes

Can we convert Picklist value into Date format?

DATEVALUE requires the format to be "YYYY-MM-DD". Since your picklist doesn't match that format, you'd need some extensive code: DATEVALUE(RIGHT(TEXT(picklist),4)&"-"&MID(TEXT(picklist),4,2)&...
sfdcfox's user avatar
  • 502k
6 votes
Accepted

FORMATDATE and format pattern to add year when using format pattern "M"

To provide it as answer (even though i might not be worth it): You have to do the following FORMATDATE(@START_DT,"D. MMMM YYYY","","da-DK") You can set delimeters like you want in these format ...
Johannes Schapdick's user avatar
6 votes
Accepted

Ouput date in ISO8601 date-time format

Do it in AMPscript using Format Date function: https://ampscript.guide/formatdate/ Example: set @dateISO = FormatDate(@dateString,"iso") You can pass the date variable form SSJS to AMPscript using ...
zuzannamj's user avatar
  • 14.5k

Only top scored, non community-wiki answers of a minimum length are eligible