Salesforce Datetime Methods You Must Get to Know

With the help of custom settings which is a product of Salesforce, users can customize, save and use date settings if they have an understanding of Salesforce Datetime Methods. Apex gives an avenue for users to manipulate dates and time and allow the formats of their choice to be displayed. With simple codes for the various formulas you can use it to change the time and date displayed to being in either local time or GMT.

These are available in the Apex language reference manual making it trouble free as it only needs a simple function to turn into datetime variable the present time. Formatting then involves the user picking the sequence for the characters that you want displayed for the date time in string description. 

Advantages of using Salesforce Datetime Methods makes it possible to format dates as well as time. One can also get to know dates to come or go back to past date or even the time between the dates by either addition or subtraction enabled by this platform. One can also add to the dates and time numbers they want to.

These features are vital especially in businesses where significant dates and times need to be retrieved or used. It makes dealing with time and date much simpler for instance instead of having to remember that you have 300 hours to complete a task, you can use in converting it to the exact days, hours minutes and seconds you have. The user can then choose which ever format they would like for the date and time.

The following however, are the Salesforce datetime methods you must get to know as they are one of a kind and come in handy in a range of business related situations.

There are numerous methods for datetime:

  • System static methods
  • Instance methods

System Static Methods Include

  • New instance (long). Other than constructing a datetime, it also initializes it to produce a representations of the precise milliseconds from the time January 1, 1970, 00:00:00 GMT
  • The NewInstance Date Time on the other hand uses the exact date and the time in Local time zone to create a Date Time.
  • New instance integer integer (month) integer (day).
  • For every integer representation, it creates a datetime. This is at the exact year, month where 1=Jan and day local time zone at midnight.
  • New instance (year) integer (month where 1=Jan) integer (day) integer (hour) integer (minute) integer (second). Where for every integer representation for the year, month, day, hour and second at that specific time in the local timezone, date time is constructed.
  • For the GMT zone, the newinstanceGmt (Date,Time) Whereby the date time is constructed from the particular time and date as per the GMT time zone.
  • The newInstanceGmt method is similar to the newinstance (integer,integer,integer) except for that it constructs daytime from integer representations in the GMT zone. This is also the same with newinstanceGmt(integer, integer, integer, integer, integer, integer).
  • The Now function will have the available datetime value displayed. It comes in handy especially when the user wants to find out the current time and date.
  • Phrase (string).Phrase is used to generate datetime from datetime in string form in the locale format preferable to the user and in the local time zone.
  • ValueOf (string). This is for returning a date time with values that have the exact string local time zone. The string should also be in the format “yyyy – MM – dd HH:mm:ss”.
  • ValueOfGmt (String) is similar including the date format for the string needing to be in standard set-up but in this case, the string has to be in GMT time zone.
  • ValueOf (object). This changes into a datetime, the exact value of the history tracking field.

Instance Methods Include

  • AddDays(integer), AddMonths (integer), AddMinutes (integer), AddHours (integer), AddYears (integer), AddSeconds(integer) Add the exact figure of days, months, minutes, hours, years and seconds to a datetime respectively.
  • Date converts back the day factor of a datetime, local time zone. DateGMT is similar but the component is returned in the context user’s GMT time zone.
  • Instead of returning to just the date, you can choose to return to the exact day of the month from the datetime.
  • The year, time, month, second, millisecond and minute functions return the year, time, month, second, millisecond and minute element respectively of a datetime in the context user’s local time zone. If you prefer the component returned in GMT, you can use yearGmt, timeGmt, monthGmt millisecondGmt and minuteGmt formulas as an alternative.

The Day Function Includes:

  • Day method which returns in the context users local time zone the particular day of the month of the datetime. If in the Gmt time zone, then it would be more preferable to use DayGmt.
  • Dayof Year and DayofYear basically work the same way but with the year component and the latter being for Gmt Time zones while the former for local time zone.
  • The Day function is also used to find out how many days have gone by since a particular day or even how many days to a particular day or even display the present day.

When using these methods, it is vital to keep in mind that the values are usually in GMT time zone before being changed to the user’s time zone. This includes conversions in daytime fields as well as the values. Converting the dates should not be problematic and neither should be doing datetime standard subtractions.

What to Do When Confused About the Methods

Some of the methods for daytime will be quite simple while some of the formulas on the other hand may leave you a bit confused. provides a platform to help give you some guidance. This includes, how to use the formulas as well as what to do when certain issues arise for instance handling dates that are invalid.

Online help from the company’s experts is also available and uncomplicated to get. It will what’s more enable you to view past questions, answers and broaden your knowledge on how to maneuver through the formulas mush easily. One can in addition get sample codes for the Salesforce datetime methods and their output which could be really helpful.

Amanda McDonnald
Amanda is the Lead Author & Editor of Rainforce Blog. Amanda established the Rainforce blog to create a source for news and discussion about some of the issues, challenges, news, and ideas relating to Salesforce usage.
Amanda McDonnald on sabtwitterAmanda McDonnald on sabgoogle