Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

Hot answers tagged

40 votes
Accepted

How to calculate the time between two DateTimes?

You can use the getTime method to get the milliseconds between them and then convert to whatever unit you need: Long dt1Long = DateTime.now().addDays(-1).getTime(); Long dt2Long = DateTime.now()....
user avatar
  • 12.1k
27 votes

Convert DateTime to Date

Datetime objects have this date method: date() Returns the Date component of a Datetime in the local time zone of the context user. so your code can use it like this: Datetime dt = (Datetime) ...
user avatar
  • 129k
27 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
user avatar
25 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 ...
user avatar
  • 142k
25 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 ...
user avatar
  • 142k
19 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 ...
user avatar
  • 1,415
18 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 ...
user avatar
  • 26.7k
17 votes
Accepted

Converting Date to DateTime

I think the simplest method for you to use will be something like the following: Integer d = Due_Date__c.day(); Integer mo = Due_Date__c.month(); Integer yr = Due_Date__c.year(); DateTime DT = ...
user avatar
  • 40.3k
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 ...
user avatar
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 ...
user avatar
  • 5,213
16 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(...
user avatar
  • 22.3k
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 ...
user avatar
  • 430k
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....
user avatar
  • 430k
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 ...
user avatar
  • 129k
13 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 ...
user avatar
  • 141
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....
user avatar
  • 142k
11 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 ...
user avatar
  • 1,698
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, ...
user avatar
  • 142k
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 ...
user avatar
  • 142k
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 ...
user avatar
  • 10.5k
9 votes
Accepted

Variable doesn't exist :Date

Date.newInstance does not accept strings, it accepts integers according to the documentation. You are passing in Strings. You could possibly try the Date.parse method.
user avatar
9 votes
Accepted

How to Get the Current System Time using SOQL

You don't need to use SOQL. You can just create a new DateTime and set it to the current date and time like below DateTime myDateTime = system.now(); Or if you only want the date, then Date ...
user avatar
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 ...
user avatar
  • 5,459
9 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 ...
user avatar
  • 430k
9 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 ...
user avatar
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) ...
user avatar
  • 142k
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 ...
user avatar
  • 13.2k
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 ...
user avatar
  • 142k
8 votes

How to calculate the time between two DateTimes?

I know this is kind of late, but I had a similar need for elapsed time which may help in your case. So I created the following utility: public static Time GetElapsedTime(Time startTime, Time endTime)...
user avatar
8 votes
Accepted

What is the purpose of Datetime.dateGmt() function?

First, I need to correct your question. There is no Date.DateGMT() function; the Date class has no member DateGMT. I believe you meant DateTime.DateGMT. From Apex Code Developer's Guide's Primitive ...
user avatar
  • 10.9k

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