Notes
newInstance(Year
,Month
,Day
,hour
,Minute
,Second
):
Constructs a Datetime from Integer representations of the specified year, month (1=Jan), and day at midnight in the local time zone.
DateTime myDateTime = DateTime.newInstance(Year,Month,Day,Hour,Minute,Second);
Year = 2006
Month = 11
Day = 19
Hour = 8
Minute = 24
Second = 44
DateTime d1 = DateTime.newInstance(2006, 11, 19, 8, 24, 44);
System.debug(d1);
DEBUG|2006-11-19 13:24:44
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.
DateTime myDateTime = DateTime.newInstance(Year,Month,Day);
Year = 2006
Month = 11
Day = 19
DateTime d1 = DateTime.newInstance(2006, 11, 19);
System.debug(d1);
DEBUG|2006-11-19 00:00:00
newInstance(Date
,Time
):
DateTime myDateTime = DateTime.newInstance(Date,Time);
Date d = Date.newInstance(2022, 10, 16);
Time t = Time.newInstance(12, 45, 55, 100);
DateTime dt = DateTime.newInstance(d, t);
System.debug(dt);
DEBUG|2022-10-16 16:45:55
year():
Returns the year component of a Datetime in the local time zone of the context user.
DateTime myDateTime = DateTime.newInstance(2022, 12, 31, 7, 8, 16);
System.debug(myDateTime.Year());
DEBUG|12
month():
Returns the month component of a Datetime in the local time zone of the context user (1=Jan).
DateTime myDateTime = DateTime.newInstance(2022, 12, 31, 7, 8, 16);
System.debug(myDateTime.Month());
DEBUG|2022
day():
Returns the day-of-month component of a Datetime in the local time zone of the context user.
DateTime myDateTime = DateTime.newInstance(2022, 1, 31, 7, 8, 16);
System.debug(myDateTime.Day());
DEBUG|31
hour():
Returns the hour component of a Datetime in the local time zone of the context user.
DateTime myDateTime = DateTime.newInstance(2022, 1, 31, 11, 8, 16);
System.debug(myDateTime.Hour());
DEBUG|11
minute():
Returns the minute component of a Datetime in the local time zone of the context user.
DateTime myDateTime = DateTime.newInstance(2022, 1, 31, 11, 25, 16);
System.debug(myDateTime.Minute());
DEBUG|25
second():
Returns the second component of a Datetime in the local time zone of the context user.
DateTime myDateTime = DateTime.newInstance(2022, 1, 31, 11, 25, 54);
System.debug(myDateTime.Second());
DEBUG|54
millisecond():
Returns the second component of a Datetime in the local time zone of the context user.
DateTime myDateTime = DateTime.newInstance(2022, 1, 31, 11, 25, 54);
System.debug(myDateTime.Second());
DEBUG|54
addYears(3):
Adds 3 years to a Datetime.
DateTime d1 = DateTime.newInstance(2022, 1, 13, 7, 8, 16);
d1.addYears(3);
System.debug(d1.Year());
DEBUG | 2025
addMonths(4):
Adds 4 months to a Datetime.
DateTime d1 = DateTime.newInstance(2022, 1, 13, 7, 8, 16);
d1.addMonths(4);
System.debug(d1.Month());
DEBUG | 5
addDays(3):
Adds 3 days to a Datetime.
DateTime d1 = DateTime.newInstance(2022, 1, 13, 7, 8, 16);
d1.addDays(4);
System.debug(d1.Day());
DEBUG | 17
addHours(5):
Adds 5 hours to a Datetime.
DateTime d1 = DateTime.newInstance(2022, 1, 31, 7, 8, 16);
d1.addHours(5);
System.debug(d1.Hour());
DEBUG | 12 (Outcome will come 7 due to GMT time zone difference
addMinutes(10):
Adds 10 minutes to a Datetime.
DateTime d1 = DateTime.newInstance(2022, 1, 31, 7, 8, 16);
d1.addMinutes(10);
System.debug(d1.Minute());
DEBUG | 18
addSeconds(25):
Adds 25 seconds to a Datetime.
DateTime d1 = DateTime.newInstance(2022, 1, 31, 7, 8, 16);
d1.addSeconds(25);
System.debug(d1.Second());
DEBUG | 16
date():
Returns the Date component of a Datetime in the local time zone of the context user.
DateTime myDateTime = DateTime.newInstance(2022, 1, 31, 7, 8, 16);
System.debug(myDateTime.Date());
DEBUG | DEBUG|2022-01-31 00:00:00
dateGMT():
Return the Date component of a Datetime in the GMT time zone.
DateTime myDateTime = DateTime.newInstance(2022, 1, 31, 7, 8, 16);
System.debug(myDateTime.DateGMT());
DEBUG|2022-01-31 00:00:00
time():
Returns the time component of a Datetime in the local time zone of the context user.
DateTime d1 = DateTime.newInstance(2022, 1, 31, 11, 25, 54);
System.debug(d1.time());
DEBUG|11:25:54.000Z
timeGMT():
Returns the time component of a Datetime in the GMT time zone.
DateTime d1 = DateTime.newInstance(2022, 1, 31, 11, 25, 54);
System.debug(d1.time());
DEBUG|16:25:54.000Z
dayGmt():
Returns the day-of-month component of a Datetime in the GMT time zone.
DateTime myDateTime = DateTime.newInstance(2022, 1, 31, 7, 8, 16);
System.debug(myDateTime.DayGMT());
DEBUG|31
dayOfYear()
Returns the day-of-year component of a Datetime in the local time zone of the context user.
Datetime myDate = Datetime.newInstance(2008, 2, 5, 8, 30, 12);
system.debug(myDate.dayOfYear());
DEBUG|36
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.
DateTime d1 = DateTime.newInstance(2022, 1, 31, 11, 25, 54);
System.debug(d1.format());
DEBUG|1/31/2022, 11:25 AM
format('EEEE'):
Returns the day of the datetime in words.
System.debug(DateTime.now().format('EEEE'));
DEBUG | Saturday
format('EEE'):
Returns the day of the datetime in words.
System.debug(DateTime.now().format('EEE'));
DEBUG | Sat
YouTube Video Codes
Integer x =12;
String txt ='Engin';
Datetime d1 = Datetime.newInstance(2022, 11, 13, 18, 59, 45);
System.debug(d1);
Date d = Date.newInstance(2022, 11, 13);
Time t = Time.newInstance(18, 59, 45, 000);
Datetime dt = Datetime.newInstance(d, t);
System.debug(dt);
System.debug(d1.year());
System.debug(d1.month());
System.debug(d1.day());
System.debug(d1.hour());
System.debug(d1.minute());
System.debug(d1.second());
System.debug(d1.addYears(2));
System.debug(d1.addMonths(5));
System.debug(d1.addDays(20));
System.debug(d1.addHours(7));
System.debug(d1.addMinutes(30));
System.debug(d1.addSeconds(60));
Date onlydate = d1.date();
System.debug(onlydate);
Time onlytime= d1.time();
System.debug(onlytime);
System.debug(d1.dateGMT());
Datetime d2= Datetime.newInstance(2022,11,13,20,45,58);
System.debug(d2);
System.debug(d2.dateGMT());
System.debug(d2.date());
System.debug(d2.dayofYear());
System.debug(d2.format());
System.debug(d2.format('EEEE'));
System.debug(d2.format('EE'));