No Results
Search Tips:
- Please consider misspellings
- Try different search keywords
Newer Version Available
Datetime Methods
Namespace
Usage
For more information about the Datetime, see Primitive Data Types.
Datetime Methods
The following are methods for Datetime.
date()
Signature
public Date date()
Return Value
Type: Date
dateGMT()
Signature
public Date dateGMT()
Return Value
Type: Date
day()
Signature
public Integer day()
Return Value
Type: Integer
Example
For example, February 5, 1999 08:30:12 would be day 5.
dayGmt()
Signature
public Integer dayGmt()
Return Value
Type: Integer
Example
For example, February 5, 1999 08:30:12 would be day 5.
dayOfYear()
Signature
public Integer dayOfYear()
Return Value
Type: Integer
Example
For example, February 5, 2008 08:30:12 would be day 36.
1Datetime myDate =
2 datetime.newInstance
3 (2008, 2, 5, 8, 30, 12);
4system.assertEquals
5 (myDate.dayOfYear(), 36);dayOfYearGmt()
Signature
public Integer dayOfYearGmt()
Return Value
Type: Integer
Example
For example, February 5, 1999 08:30:12 would be day 36.
format()
Signature
public String format()
Return Value
Type: String
format(String)
Signature
public String format(String dateFormat)
Parameters
- dateFormat
- Type: String
Return Value
Type: String
Usage
For more information on the Java simple date format, see Java SimpleDateFormat.
Example
1Datetime myDT = Datetime.now();
2String myDate = myDT.format('h:mm a');format(String, String)
Signature
public String format(String dateFormat, String timezone)
Parameters
- dateFormat
- 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
1Datetime GMTDate =
2 Datetime.newInstanceGmt(2011,6,1,12,1,5);
3String strConvertedDate =
4 GMTDate.format('MM/dd/yyyy HH:mm:ss',
5 'America/New_York');
6// Date is converted to
7// the new time zone and is adjusted
8// for daylight saving time.
9System.assertEquals(
10 '06/01/2011 08:01:05', strConvertedDate);formatGmt(String)
Signature
public String formatGmt(String dateFormat)
Parameters
- dateFormat
- Type: String
Return Value
Type: String
Usage
For more information on the Java simple date format, see Java SimpleDateFormat.
formatLong()
Signature
public String formatLong()
Return Value
Type: String
Example
1// Passing local date based on the PST
2// time zone
3Datetime dt = DateTime.newInstance(
4 2012,12,28,10,0,0);
5// Writes 12/28/2012 10:00:00 AM PST
6System.debug('dt.formatLong()='
7 + dt.formatLong());getTime()
Signature
public Long getTime()
Return Value
Type: Long
hour()
Signature
public Integer hour()
Return Value
Type: Integer
hourGmt()
Signature
public Integer hourGmt()
Return Value
Type: Integer
isSameDay(Datetime)
Signature
public Boolean isSameDay(Datetime compDt)
Parameters
- compDt
- Type: Datetime
Return Value
Type: Boolean
Example
1datetime myDate = datetime.now();
2datetime dueDate =
3 datetime.newInstance(2008, 1, 30);
4boolean dueNow = myDate.isSameDay(dueDate);millisecond()
Signature
public Integer millisecond()
Return Value
Type: Integer
millisecondGmt()
Signature
public Integer millisecondGmt()
Return Value
Type: Integer
minute()
Signature
public Integer minute()
Return Value
Type: Integer
minuteGmt()
Signature
public Integer minuteGmt()
Return Value
Type: Integer
month()
Signature
public Integer month()
Return Value
Type: Integer
monthGmt()
Signature
public Integer monthGmt()
Return Value
Type: Integer
newInstance(Integer, Integer, Integer)
Signature
public static Datetime newInstance(Integer year, Integer month, Integer day)
Example
1datetime myDate =
2 datetime.newInstance(2008, 12, 1);newInstance(Integer, Integer, Integer, Integer, Integer, Integer)
Signature
public static Datetime newInstance(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer second)
Parameters
Example
1Datetime myDate =
2datetime.newInstance(2008, 12, 1, 12, 30, 2);newInstanceGmt(Integer, Integer, Integer)
Signature
public static Datetime newInstanceGmt(Integer year, Integer month, Integer date)
Return Value
Type: Datetime
newInstanceGmt(Integer, Integer, Integer, Integer, Integer, Integer)
Signature
public static Datetime newInstanceGmt(Integer year, Integer month, Integer date, Integer hour, Integer minute, Integer second)
Parameters
Return Value
Type: Datetime
now()
Signature
public static Datetime now()
Example
1datetime myDateTime = datetime.now();parse(String)
Signature
public static Datetime parse(String datetime)
Parameters
- datetime
- Type: String
Example
1Datetime dt = DateTime.parse(
2 '10/14/2011 11:46 AM');
3String myDtString = dt.format();
4system.assertEquals(
5 myDtString,
6 '10/14/2011 11:46 AM');second()
Signature
public Integer second()
Return Value
Type: Integer
secondGmt()
Signature
public Integer secondGmt()
Return Value
Type: Integer
time()
Signature
public Time time()
Return Value
Type: Time
timeGmt()
Signature
public Time timeGmt()
Return Value
Type: Time
valueOf(String)
Signature
public static Datetime valueOf(String toDateTime)
Parameters
- toDateTime
- Type: String
Usage
The specified string should use the standard date format “yyyy-MM-dd HH:mm:ss” in the local time zone.
Example
1string year = '2008';
2string month = '10';
3string day = '5';
4string hour = '12';
5string minute = '20';
6string second = '20';
7string stringDate = year + '-' + month
8 + '-' + day + ' ' + hour + ':' +
9minute + ':' + second;
10
11Datetime myDate =
12 datetime.valueOf(stringDate);valueOf(Object)
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
1List<AccountHistory> ahlist =
2 [SELECT Field,OldValue,NewValue
3 FROM AccountHistory];
4for(AccountHistory ah : ahlist) {
5 System.debug('Field: ' + ah.Field);
6 if (ah.field == 'MyDatetime__c') {
7 Datetime oldValue =
8 Datetime.valueOf(ah.OldValue);
9 Datetime newValue =
10 Datetime.valueOf(ah.NewValue);
11}valueOfGmt(String)
Signature
public static Datetime valueOfGmt(String toDateTime)
Parameters
- toDateTime
- 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.
year()
Signature
public Integer year()
Return Value
Type: Integer
yearGmt()
Signature
public Integer yearGmt()
Return Value
Type: Integer