Newer Version Available

This content describes an older version of this product. View Latest

Datetime Class

Contains methods for the Datetime primitive data type.

Namespace

System

Usage

For more information about the Datetime, see Datetime Data Type.

Datetime Methods

The following are methods for Datetime.

addDays(additionalDays)

Adds the specified number of days to a Datetime.

Signature

public Datetime addDays(Integer additionalDays)

Parameters

additionalDays
Type: Integer

Return Value

Type: Datetime

Example

addHours(additionalHours)

Adds the specified number of hours to a Datetime.

Signature

public Datetime addHours(Integer additionalHours)

Parameters

additionalHours
Type: Integer

Return Value

Type: Datetime

Example

addMinutes(additionalMinutes)

Adds the specified number of minutes to a Datetime.

Signature

public Datetime addMinutes(Integer additionalMinutes)

Parameters

additionalMinutes
Type: Integer

Return Value

Type: Datetime

Example

addMonths(additionalMonths)

Adds the specified number of months to a Datetime.

Signature

public Datetime addMonths(Integer additionalMonths)

Parameters

additionalMonths
Type: Integer

Return Value

Type: Datetime

Example

addSeconds(additionalSeconds)

Adds the specified number of seconds to a Datetime.

Signature

public Datetime addSeconds(Integer additionalSeconds)

Parameters

additionalSeconds
Type: Integer

Return Value

Type: Datetime

Example

addYears(additionalYears)

Adds the specified number of years to a Datetime.

Signature

public Datetime addYears(Integer additionalYears)

Parameters

additionalYears
Type: Integer

Return Value

Type: Datetime

Example

date()

Returns the Date component of a Datetime in the local time zone of the context user.

Signature

public Date date()

Return Value

Type: Date

Example

dateGMT()

Return the Date component of a Datetime in the GMT time zone.

Signature

public Date dateGMT()

Return Value

Type: Date

Example

day()

Returns the day-of-month component of a Datetime in the local time zone of the context user.

Signature

public Integer day()

Return Value

Type: Integer

Example

dayGmt()

Returns the day-of-month component of a Datetime in the GMT time zone.

Signature

public Integer dayGmt()

Return Value

Type: Integer

Example

dayOfYear()

Returns the day-of-year component of a Datetime in the local time zone of the context user.

Signature

public Integer dayOfYear()

Return Value

Type: Integer

Example

For example, February 5, 2008 08:30:12 would be day 36.

dayOfYearGmt()

Returns the day-of-year component of a Datetime in the GMT time zone.

Signature

public Integer dayOfYearGmt()

Return Value

Type: Integer

Example

format()

Converts the date to the local time zone and returns the converted date as a formatted string using the locale of the context user. If the time zone cannot be determined, GMT is used.

Signature

public String format()

Return Value

Type: String

Example

The sample is executed in an org where the “Enable ICU Locale Formats” crucial update is enabled. See https://releasenotes.docs.salesforce.com/en-us/spring20/release-notes/rn_forcecom_globalization_enable_icu_cruc.htm.

Note

format(dateFormatString)

Converts the date to the local time zone and returns the converted date as a string using the supplied Java simple date format. If the time zone cannot be determined, GMT is used.

Signature

public String format(String dateFormatString)

Parameters

dateFormatString
Type: String

Return Value

Type: String

Usage

For more information on the Java simple date format, see Java SimpleDateFormat.

Example

format(dateFormatString, timezone)

Converts the date to the specified time zone and returns the converted date as a string using the supplied Java simple date format. If the supplied time zone is not in the correct format, GMT is used.

Signature

public String format(String dateFormatString, String timezone)

Parameters

dateFormatString
Type: String
timezone
Type: String
Valid time zone values for the timezone argument are the time zones of the Java TimeZone class that correspond to the time zones returned by the TimeZone.getAvailableIDs method in Java. We recommend you use full time zone names, not the three-letter abbreviations.

Return Value

Type: String

Usage

For more information on the Java simple date format, see Java SimpleDateFormat.

Example

This example uses format to convert a GMT date to the America/New_York time zone and formats the date using the specified date format.

formatGmt(dateFormatString)

Returns a Datetime as a string using the supplied Java simple date format and the GMT time zone.

Signature

public String formatGmt(String dateFormatString)

Parameters

dateFormatString
Type: String

Return Value

Type: String

Usage

For more information on the Java simple date format, see Java SimpleDateFormat.

Example

formatLong()

Converts the date to the local time zone and returns the converted date in long date format.

Signature

public String formatLong()

Return Value

Type: String

Example

getTime()

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this DateTime object.

Signature

public Long getTime()

Return Value

Type: Long

Example

hour()

Returns the hour component of a Datetime in the local time zone of the context user.

Signature

public Integer hour()

Return Value

Type: Integer

Example

hourGmt()

Returns the hour component of a Datetime in the GMT time zone.

Signature

public Integer hourGmt()

Return Value

Type: Integer

Example

isSameDay(dateToCompare)

Returns true if the Datetime that called the method is the same as the specified Datetime in the local time zone of the context user.

Signature

public Boolean isSameDay(Datetime dateToCompare)

Parameters

dateToCompare
Type: Datetime

Return Value

Type: Boolean

Example

millisecond()

Return the millisecond component of a Datetime in the local time zone of the context user.

Signature

public Integer millisecond()

Return Value

Type: Integer

Example

millisecondGmt()

Return the millisecond component of a Datetime in the GMT time zone.

Signature

public Integer millisecondGmt()

Return Value

Type: Integer

Example

minute()

Returns the minute component of a Datetime in the local time zone of the context user.

Signature

public Integer minute()

Return Value

Type: Integer

Example

minuteGmt()

Returns the minute component of a Datetime in the GMT time zone.

Signature

public Integer minuteGmt()

Return Value

Type: Integer

Example

month()

Returns the month component of a Datetime in the local time zone of the context user (1=Jan).

Signature

public Integer month()

Return Value

Type: Integer

Example

monthGmt()

Returns the month component of a Datetime in the GMT time zone (1=Jan).

Signature

public Integer monthGmt()

Return Value

Type: Integer

Example

newInstance(milliseconds)

Constructs a Datetime and initializes it to represent the specified number of milliseconds since January 1, 1970, 00:00:00 GMT.

Signature

public static Datetime newInstance(Long milliseconds)

Parameters

milliseconds
Type: Long

Return Value

Type: Datetime

The returned date is in the GMT time zone.

Example

newInstance(date, time)

Constructs a DateTime from the specified date and time in the local time zone.

Signature

public static Datetime newInstance(Date date, Time time)

Parameters

date
Type: Date
time
Type: Time

Return Value

Type: Datetime

The returned date is in the GMT time zone.

Example

newInstance(year, month, day)

Constructs a Datetime from Integer representations of the specified year, month (1=Jan), and day at midnight in the local time zone.

Signature

public static Datetime newInstance(Integer year, Integer month, Integer day)

Parameters

year
Type: Integer
month
Type: Integer
day
Type: Integer

Return Value

Type: Datetime

The returned date is in the GMT time zone.

Example

newInstance(year, month, day, hour, minute, second)

Constructs a Datetime from Integer representations of the specified year, month (1=Jan), day, hour, minute, and second in the local time zone.

Signature

public static Datetime newInstance(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer second)

Parameters

year
Type: Integer
month
Type: Integer
day
Type: Integer
hour
Type: Integer
minute
Type: Integer
second
Type: Integer

Return Value

Type: Datetime

The returned date is in the GMT time zone.

Example

newInstanceGmt(date, time)

Constructs a DateTime from the specified date and time in the GMT time zone.

Signature

public static Datetime newInstanceGmt(Date date, Time time)

Parameters

date
Type: Date
time
Type: Time

Return Value

Type: Datetime

Example

newInstanceGmt(year, month, date)

Constructs a Datetime from Integer representations of the specified year, month (1=Jan), and day at midnight in the GMT time zone

Signature

public static Datetime newInstanceGmt(Integer year, Integer month, Integer date)

Parameters

year
Type: Integer
month
Type: Integer
date
Type: Integer

Return Value

Type: Datetime

Example

newInstanceGmt(year, month, date, hour, minute, second)

Constructs a Datetime from Integer representations of the specified year, month (1=Jan), day, hour, minute, and second in the GMT time zone

Signature

public static Datetime newInstanceGmt(Integer year, Integer month, Integer date, Integer hour, Integer minute, Integer second)

Parameters

year
Type: Integer
month
Type: Integer
date
Type: Integer
hour
Type: Integer
minute
Type: Integer
second
Type: Integer

Return Value

Type: Datetime

Example

now()

Returns the current Datetime based on a GMT calendar.

Signature

public static Datetime now()

Return Value

Type: Datetime

The format of the returned datetime is: 'MM/DD/YYYY HH:MM PERIOD'

Example

parse(datetimeString)

Constructs a Datetime from the given String in the local time zone and in the format of the user locale.

Signature

public static Datetime parse(String datetimeString)

Parameters

datetimeString
Type: String

Return Value

Type: Datetime

The returned date is in the GMT time zone.

Example

This example uses parse to create a Datetime from a date passed in as a string and that is formatted for the English (United States) locale. You may need to change the format of the date string if you have a different locale.

This sample is executed in an org where the “Enable ICU Locale Formats” crucial update is enabled. See https://releasenotes.docs.salesforce.com/en-us/spring20/release-notes/rn_forcecom_globalization_enable_icu_cruc.htm.

Note

second()

Returns the second component of a Datetime in the local time zone of the context user.

Signature

public Integer second()

Return Value

Type: Integer

Example

secondGmt()

Returns the second component of a Datetime in the GMT time zone.

Signature

public Integer secondGmt()

Return Value

Type: Integer

Example

time()

Returns the time component of a Datetime in the local time zone of the context user.

Signature

public Time time()

Return Value

Type: Time

Example

timeGmt()

Returns the time component of a Datetime in the GMT time zone.

Signature

public Time timeGmt()

Return Value

Type: Time

Example

valueOf(dateTimeString)

Returns a Datetime that contains the value of the specified string.

Signature

public static Datetime valueOf(String dateTimeString)

Parameters

dateTimeString
Type: String

Return Value

Type: Datetime

The returned date is in the GMT time zone.

Usage

The specified string should use the standard date format “yyyy-MM-dd HH:mm:ss” in the local time zone.

Example

valueOf(fieldValue)

Converts the specified object to a Datetime. Use this method to convert a history tracking field value or an object that represents a Datetime value.

Signature

public static Datetime valueOf(Object fieldValue)

Parameters

fieldValue
Type: Object

Return Value

Type: Datetime

Usage

Use this method with the OldValue or NewValue fields of history sObjects, such as AccountHistory, when the field is a Date/Time field.

Example

valueOfGmt(dateTimeString)

Returns a Datetime that contains the value of the specified String.

Signature

public static Datetime valueOfGmt(String dateTimeString)

Parameters

dateTimeString
Type: String

Return Value

Type: Datetime

Usage

The specified string should use the standard date format “yyyy-MM-dd HH:mm:ss” in the GMT time zone.

Example

year()

Returns the year component of a Datetime in the local time zone of the context user.

Signature

public Integer year()

Return Value

Type: Integer

Example

yearGmt()

Returns the year component of a Datetime in the GMT time zone.

Signature

public Integer yearGmt()

Return Value

Type: Integer

Example