For more information about the Datetime, see Primitive Data Types.
The following are methods for Datetime.
public Datetime addDays(Integer additionalDays)
Type: Datetime
Datetime myDateTime = Datetime.newInstance(1960, 2, 17); Datetime newDateTime = myDateTime.addDays(2); Datetime expected = Datetime.newInstance(1960, 2, 19); System.assertEquals(expected, newDateTime);
public Datetime addHours(Integer additionalHours)
Type: Datetime
DateTime myDateTime = DateTime.newInstance(1997, 1, 31, 7, 8, 16); DateTime newDateTime = myDateTime.addHours(3); DateTime expected = DateTime.newInstance(1997, 1, 31, 10, 8, 16); System.assertEquals(expected, newDateTime);
public Datetime addMinutes(Integer additionalMinutes)
Type: Datetime
DateTime myDateTime = DateTime.newInstance(1999, 2, 11, 8, 6, 16); DateTime newDateTime = myDateTime.addMinutes(7); DateTime expected = DateTime.newInstance(1999, 2, 11, 8, 13, 16); System.assertEquals(expected, newDateTime);
public Datetime addMonths(Integer additionalMonths)
Type: Datetime
DateTime myDateTime = DateTime.newInstance(2000, 7, 7, 7, 8, 12); DateTime newDateTime = myDateTime.addMonths(1); DateTime expected = DateTime.newInstance(2000, 8, 7, 7, 8, 12); System.assertEquals(expected, newDateTime);
public Datetime addSeconds(Integer additionalSeconds)
Type: Datetime
DateTime myDateTime = DateTime.newInstance(2001, 7, 19, 10, 7, 12); DateTime newDateTime = myDateTime.addSeconds(4); DateTime expected = DateTime.newInstance(2001, 7, 19, 10, 7, 16); System.assertEquals(expected, newDateTime);
public Datetime addYears(Integer additionalYears)
Type: Datetime
DateTime myDateTime = DateTime.newInstance(2009, 12, 17, 13, 6, 6); DateTime newDateTime = myDateTime.addYears(1); DateTime expected = DateTime.newInstance(2010, 12, 17, 13, 6, 6); System.assertEquals(expected, newDateTime);
public Date date()
Type: Date
DateTime myDateTime = DateTime.newInstance(2006, 3, 16, 12, 6, 13); Date myDate = myDateTime.date(); Date expected = Date.newInstance(2006, 3, 16); System.assertEquals(expected, myDate);
public Date dateGMT()
Type: Date
// California local time, PST DateTime myDateTime = DateTime.newInstance(2006, 3, 16, 23, 0, 0); Date myDate = myDateTime.dateGMT(); Date expected = Date.newInstance(2006, 3, 17); System.assertEquals(expected, myDate);
public Integer day()
Type: Integer
DateTime myDateTime = DateTime.newInstance(1986, 2, 21, 23, 0, 0); System.assertEquals(21, myDateTime.day());
public Integer dayGmt()
Type: Integer
// California local time, PST
DateTime myDateTime = DateTime.newInstance(1987, 1, 14, 23, 0, 3);
System.assertEquals(15, myDateTime.dayGMT());
public Integer dayOfYear()
Type: Integer
For example, February 5, 2008 08:30:12 would be day 36.
Datetime myDate = Datetime.newInstance(2008, 2, 5, 8, 30, 12); system.assertEquals(myDate.dayOfYear(), 36);
public Integer dayOfYearGmt()
Type: Integer
// This sample assumes we are in the PST timezone DateTime myDateTime = DateTime.newInstance(1999, 2, 5, 23, 0, 3); // January has 31 days + 5 days in February = 36 days // dayOfYearGmt() adjusts the time zone from the current time zone to GMT // by adding 8 hours to the PST time zone, so it's 37 days and not 36 days System.assertEquals(37, myDateTime.dayOfYearGmt());
public String format()
Type: String
DateTime myDateTime = DateTime.newInstance(1993, 6, 6, 3, 3, 3);
system.assertEquals('6/6/1993 3:03 AM', mydatetime.format());
public String format(String dateFormatString)
Type: String
For more information on the Java simple date format, see Java SimpleDateFormat.
Datetime myDT = Datetime.now(); String myDate = myDT.format('h:mm a');
public String format(String dateFormatString, String timezone)
Type: String
For more information on the Java simple date format, see Java SimpleDateFormat.
Datetime GMTDate = Datetime.newInstanceGmt(2011,6,1,12,1,5); String strConvertedDate = GMTDate.format('MM/dd/yyyy HH:mm:ss', 'America/New_York'); // Date is converted to // the new time zone and is adjusted // for daylight saving time. System.assertEquals( '06/01/2011 08:01:05', strConvertedDate);
public String formatGmt(String dateFormatString)
Type: String
For more information on the Java simple date format, see Java SimpleDateFormat.
DateTime myDateTime = DateTime.newInstance(1993, 6, 6, 3, 3, 3); String formatted = myDateTime.formatGMT('EEE, MMM d yyyy HH:mm:ss'); String expected = 'Sun, Jun 6 1993 10:03:03'; System.assertEquals(expected, formatted);
public String formatLong()
Type: String
// Passing local date based on the PST time zone Datetime dt = DateTime.newInstance(2012,12,28,10,0,0); // Writes 12/28/2012 10:00:00 AM PST System.debug('dt.formatLong()=' + dt.formatLong());
public Long getTime()
Type: Long
DateTime dt = DateTime.newInstance(2007, 6, 23, 3, 3, 3); Long gettime = dt.getTime(); Long expected = 1182592983000L; System.assertEquals(expected, gettime);
public Integer hour()
Type: Integer
DateTime myDateTime = DateTime.newInstance(1998, 11, 21, 3, 3, 3); System.assertEquals(3 , myDateTime.hour());
public Integer hourGmt()
Type: Integer
// California local time
DateTime myDateTime = DateTime.newInstance(2000, 4, 27, 3, 3, 3);
System.assertEquals(10 , myDateTime.hourGMT());
public Boolean isSameDay(Datetime dateToCompare)
Type: Boolean
datetime myDate = datetime.now(); datetime dueDate = datetime.newInstance(2008, 1, 30); boolean dueNow = myDate.isSameDay(dueDate);
public Integer millisecond()
Type: Integer
DateTime myDateTime = DateTime.now(); system.debug(myDateTime.millisecond());
public Integer millisecondGmt()
Type: Integer
DateTime myDateTime = DateTime.now(); system.debug(myDateTime.millisecondGMT());
public Integer minute()
Type: Integer
DateTime myDateTime = DateTime.newInstance(2001, 2, 27, 3, 3, 3); system.assertEquals(3, myDateTime.minute());
public Integer minuteGmt()
Type: Integer
DateTime myDateTime = DateTime.newInstance(2002, 12, 3, 3, 3, 3); system.assertEquals(3, myDateTime.minuteGMT());
public Integer month()
Type: Integer
DateTime myDateTime = DateTime.newInstance(2004, 11, 4, 3, 3, 3); system.assertEquals(11, myDateTime.month());
public Integer monthGmt()
Type: Integer
DateTime myDateTime = DateTime.newInstance(2006, 11, 19, 3, 3, 3); system.assertEquals(11, myDateTime.monthGMT());
public static Datetime newInstance(Long milliseconds)
Long longtime = 1341828183000L;
DateTime dt = DateTime.newInstance(longtime);
DateTime expected = DateTime.newInstance(2012, 7, 09, 3, 3, 3);
System.assertEquals(expected, dt);
public static Datetime newInstance(Date date, Time time)
Date myDate = Date.newInstance(2011, 11, 18); Time myTime = Time.newInstance(3, 3, 3, 0); DateTime dt = DateTime.newInstance(myDate, myTime); DateTime expected = DateTime.newInstance(2011, 11, 18, 3, 3, 3); System.assertEquals(expected, dt);
public static Datetime newInstance(Integer year, Integer month, Integer day)
datetime myDate = datetime.newInstance(2008, 12, 1);
public static Datetime newInstance(Integer year, Integer month, Integer day, Integer hour, Integer minute, Integer second)
Datetime myDate = Datetime.newInstance(2008, 12, 1, 12, 30, 2);
public static Datetime newInstanceGmt(Date date, Time time)
Type: Datetime
Date myDate = Date.newInstance(2013, 11, 12); Time myTime = Time.newInstance(3, 3, 3, 0); DateTime dt = DateTime.newInstanceGMT(myDate, myTime); DateTime expected = DateTime.newInstanceGMT(2013, 11, 12, 3, 3, 3); System.assertEquals(expected, dt);
public static Datetime newInstanceGmt(Integer year, Integer month, Integer date)
Type: Datetime
DateTime dt = DateTime.newInstanceGMT(1996, 3, 22);
public static Datetime newInstanceGmt(Integer year, Integer month, Integer date, Integer hour, Integer minute, Integer second)
Type: Datetime
//California local time
DateTime dt = DateTime.newInstanceGMT(1998, 1, 29, 2, 2, 3);
DateTime expected = DateTime.newInstance(1998, 1, 28, 18, 2, 3);
System.assertEquals(expected, dt);
public static Datetime now()
datetime myDateTime = datetime.now();
public static Datetime parse(String datetimeString)
Datetime dt = DateTime.parse('10/14/2011 11:46 AM'); String myDtString = dt.format(); system.assertEquals(myDtString, '10/14/2011 11:46 AM');
public Integer second()
Type: Integer
DateTime dt = DateTime.newInstanceGMT(1999, 9, 22, 3, 1, 2); System.assertEquals(2, dt.second());
public Integer secondGmt()
Type: Integer
DateTime dt = DateTime.newInstance(2000, 2, 3, 3, 1, 5); System.assertEquals(5, dt.secondGMT());
public Time time()
Type: Time
DateTime dt = DateTime.newInstance(2002, 11, 21, 0, 2, 2); Time expected = Time.newInstance(0, 2, 2, 0); System.assertEquals(expected, dt.time());
public Time timeGmt()
Type: Time
// This sample is based on the PST time zone DateTime dt = DateTime.newInstance(2004, 1, 27, 4, 1, 2); Time expected = Time.newInstance(12, 1, 2, 0); // 8 hours are added to the time to convert it from // PST to GMT System.assertEquals(expected, dt.timeGMT());
public static Datetime valueOf(String dateTimeString)
The specified string should use the standard date format “yyyy-MM-dd HH:mm:ss” in the local time zone.
string year = '2008'; string month = '10'; string day = '5'; string hour = '12'; string minute = '20'; string second = '20'; string stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second; Datetime myDate = Datetime.valueOf(stringDate);
public static Datetime valueOf(Object fieldValue)
Type: Datetime
Use this method with the OldValue or NewValue fields of history sObjects, such as AccountHistory, when the field is a Date/Time field.
List<AccountHistory> ahlist = [SELECT Field,OldValue,NewValue FROM AccountHistory]; for(AccountHistory ah : ahlist) { System.debug('Field: ' + ah.Field); if (ah.field == 'MyDatetime__c') { Datetime oldValue = Datetime.valueOf(ah.OldValue); Datetime newValue = Datetime.valueOf(ah.NewValue); } }
public static Datetime valueOfGmt(String dateTimeString)
Type: Datetime
The specified string should use the standard date format “yyyy-MM-dd HH:mm:ss” in the GMT time zone.
// California locale time string year = '2009'; string month = '3'; string day = '5'; string hour = '5'; string minute = '2'; string second = '2'; string stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second; Datetime myDate = Datetime.valueOfGMT(stringDate); DateTime expected = DateTime.newInstance(2009, 3, 4, 21, 2, 2); System.assertEquals(expected, myDate);
public Integer year()
Type: Integer
DateTime dt = DateTime.newInstance(2012, 1, 26, 5, 2, 4); System.assertEquals(2012, dt.year());
public Integer yearGmt()
Type: Integer
DateTime dt = DateTime.newInstance(2012, 10, 4, 6, 4, 6); System.assertEquals(2012, dt.yearGMT());