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
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 ...
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 ...
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
...
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(...
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 ...
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....
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 ...
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 ...
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 ...
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 ...
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) ...
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 ...
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 ...
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);
}
...
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 ...
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;
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',
...
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 ...
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
...
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'));
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. ...
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();
...
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 ...
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 ...
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 ...
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
, ...
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)&...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
date × 708apex × 253
datetime × 166
formula × 101
soql × 66
formula-field × 60
marketing-cloud × 57
visualforce × 50
lightning-web-components × 28
javascript × 25
ampscript × 24
formatting × 24
query × 21
lightning-aura-components × 20
workflow × 19
reporting × 17
trigger × 16
lightning × 16
time × 16
visual-workflow × 15
timezone × 15
format × 15
field-update × 14
opportunity × 13
sql × 13