2022 Developer Survey is open! Take survey.
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
23 votes
Accepted

JSON: Cannot deserialize instance of date from VALUE_STRING

Three problems here: (a) Date receiving DateTime value. The value should be "YYYY-MM-DD" only. (b) Decimal receiving String value. The value should not be in quotes. (c) You must not have a ...
user avatar
  • 430k
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

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
16 votes

System.TypeException: Invalid date/time: 2019-01-03T00:26:01.711-02:00

To parse DateTimes in this ISO format, you need to use JSON deserialization. String data = '2019-01-03T00:26:01.711-02:00'; Datetime dt = (DateTime)JSON.deserialize('"' + data + '"', DateTime.class); ...
user avatar
  • 86.2k
15 votes
Accepted

Datetime.now.getTime() not returning correct value

The timestamp that salesforce returns are the number of millisecons from January 1 1970, not the number of seconds.
user avatar
  • 1,498
15 votes
Accepted

DateTime.day() returning inconsistent value when created from system.today()

The problem was with initializing the DateTime variable by casting from a Date. As the Date had no Timezone information, the resulting DateTime was created at UTC+0. For Pacific Standard Time at the ...
user avatar
15 votes
Accepted

How to convert GMT to locale dateTime?

Your question is not very clear to me, but my two possible interpretations are you want to format the current Datetime according to your locale, or you want to adjust the System.now() result to ...
user avatar
  • 142k
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
13 votes
Accepted

Parse date time string to Apex DateTime

JSON.deserialize('"2018-10-01T00:00:00Z"', DateTime.class); Note the extra set of double quotes - they allow the JSON parser to interpret that value.
user avatar
  • 86.2k
12 votes

Is there a default TimeZone in which Date fields are stored in Salesforce?

I know this question is very old, but it comes up as one of the top results on google for Salesforce date timezone questions. DateTime fields store the time information in UTC and display the ...
user avatar
  • 285
12 votes
Accepted

Convert Datetime to string in GMT format?

Use DateTime's formatGMT to format the time according to your specs in GMT. String formatedDt = dt.formatGMT('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
user avatar
  • 430k
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
11 votes
Accepted

What's the correct format to set a default value to a DateTime aura:attribute?

Very interesting question. I did some research on it and found this gitHub repository, someone has tried to play with default values of all kinds of basic types. Scene 1: Simply putting the below ...
user avatar
11 votes

How can I get my correct current local time?

This code will get you the local time for the logged in user, by leveraging the timezone offset to subtract/add from GMT. It's even smart enough to account for Daylight Saving. TimeZone tz = UserInfo....
user avatar
10 votes
Accepted

JSON.serialize is mucking with my datetime objects. How do I fix this?

I think the time wasn't in the correct format DateTime testDate = DateTime.now(); System.assertEquals('"' + testDate.formatGMT('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'' + '"'), JSON.serialize(testDate));...
user avatar
  • 1,467
10 votes

Clarification about Timezone Conversion in DateTime

As a baseline Salesforce.com stores all data in the service as UTC. There are a couple of ways to get to data .. REST API - When you use the REST API Salesforce returns UTC SOAP API- When you use ...
user avatar
  • 191
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
9 votes
Accepted

Deserializing a string from JSON to DateTime in Apex yields "Invalid format" error

You must use an ISO 8601 date, specifically something like: 2016-10-30T15:27:02.000Z Which is October 30th, 2016, at 3:27 pm (+2 seconds) in Greenwich Mean Time. To parse dates in the user's locale,...
user avatar
  • 430k
9 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
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
8 votes

Create a new date

This looks like poor error reporting by the compiler; the signature required is: DateTime.newInstance(Integer, Integer, Integer, Integer, Integer, Integer) so your local variables need to change to ...
user avatar
  • 129k
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
7 votes
Accepted

24 hours format from date/time field

You'll want to change your formula to look like: "Date/time field".format('kk:mm:ss'); See the Java Simple Date Time Format Page for more.
user avatar
  • 40.3k
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 ...
user avatar
  • 142k

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