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
28
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 ...
26
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
...
24
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 ...
24
votes
Accepted
How to convert string to Date in apex?
This error is coming as system is not able to understand Test123 while parsing into date.
The format of the String depends on the local date format, like mm/dd/yyyy. If the parameter is not of this ...
19
votes
How to convert string to Date in apex?
In case this is helpful to anyone, here is the Apex code to convert a string containing a date into an instance of the standard Date class (without going through the Datetime class or doing anything ...
17
votes
Accepted
DateTime.format() unexpected behavior
Actually... the datetime format is right.
The capital Y merge field is the WEEK YEAR - which from the 27th December 2015 is IN 2016!
The lower case y merge field is the CALENDAR YEAR - which will be ...
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(...
16
votes
Accepted
SOQL How to select WHERE = 2 months ago
This should work for records from two months ago (i.e. - Today is 25th September, that means 1st July - 31st July):
WHERE CreatedDate = LAST_N_MONTHS:2 AND CreatedDate < LAST_N_MONTHS:1
And the ...
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 ...
13
votes
Accepted
Cleaner way to get last day of previous month?
You can make it a fair bit more elegant and clear (not to mention one-lining it) by leveraging the Date.toStartOfMonth method.
public static Date getEndOfLastMonth(Date input)
{
return input....
12
votes
Accepted
Process Builder - Add Minutes,Hours to Event End Date
Use the Formula like
Now() + (60/24/60)
Give your required minutes in first 60.
If you want for 10 minutes to be added, Give like
Now() + (10/24/60)
Then you will get the 60 minutes added to the ...
12
votes
Accepted
How to calculate duration between two dates(in months and days) using formula field?
Neither formula will be perfectly correct. Depending on your tolerance, you might consider:
(date1 - date2) / 30.43685
This gives you the number of days divided by the number of average days in a ...
10
votes
Accepted
DateTime.Format() Documentation example causes exception
Attribution
Use:
system.debug(Datetime.now().formatGMT('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\''));
The esteemed Mr Ballinger also recommends the following, but it is wrapped in double quotes and slower, ...
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
Accepted
Is a TODAY() value in a formula field updated every day?
All formula fields are calculated at run time. No value is stored on the record for a formula field.
This means that any time you view a record, either through the UI or a report, the formula is ...
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 ...
8
votes
Accepted
Using a specific dynamic date in a SOQL query where clause
For a formatting solution, try something like:
public static String getSoqlDate(Integer daysFromNow)
{
return Datetime.now().addDays(daysFromNow).formatGmt('yyyy-MM-dd');
}
public static String ...
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
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
date × 695apex × 250
datetime × 165
formula × 98
soql × 66
formula-field × 59
marketing-cloud × 57
visualforce × 50
lightning-web-components × 25
javascript × 25
ampscript × 24
formatting × 22
query × 21
lightning-aura-components × 20
workflow × 19
lightning × 17
reporting × 16
time × 16
trigger × 15
timezone × 15
format × 15
visual-workflow × 14
field-update × 14
opportunity × 13
sql × 13