Javascript Course


List with JavaScript methods of the Date object, useful to work with date and time in JS scripts.
- The Getter gets /reads data from object; the Setter methods sets /redefines datas in object.


Getter Methods

getDate() - returns the day of the month based on the viewer's local time (1-31).
var dt = new Date('Jul 11, 2018 09:25:30');
console.log(dt.getDate()); // 11
getDay() - returns the day of the week (0-6).
const days =['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

var dt = new Date('Jul 11, 2018 09:25:30');
let wday = days[dt.getDay()];

console.log(wday); // Wednesday
getFullYear() - the full year based on the viewer's local time (four digits).
var dt = new Date('Jul 11, 2018 09:25:30');
console.log(dt.getFullYear()); // 2018
getHours() - returns the number of hour, based on the viewer's local time (0-23).
var dt = new Date('Jul 11, 2018 09:25:30');
console.log(dt.getHours()); // 9
getMilliseconds() - the number of milliseconds into the current second in Coordinated Universal Time (0-999).
var dt = new Date('Jul 11, 2018 09:25:30');
console.log(dt.getMilliseconds()); // 0
getMinutes() - returns the minutes (from 0-59).
var dt = new Date('Jul 11, 2018 09:25:30');
console.log(dt.getMinutes()); // 25
getMonth() - returns the month (0-11).
var dt = new Date('Jul 11, 2018 09:25:30');
console.log(dt.getMonth()); // 6
getSeconds() - returns the seconds (from 0 to 59).
var dt = new Date('Jul 11, 2018 09:25:30');
console.log(dt.getSeconds()); // 30
getTime() - the number of milliseconds since 1/1/1970.
var dt = new Date('Jul 11, 2018 09:25:30');
console.log(dt.getTime()); // 1531290330000
getTimezoneOffset() - returns the time difference between GMT (Greenwich Mean Time) and local time (in minutes).
var dt = new Date('Jul 11, 2018 09:25:30');
console.log(dt.getTimezoneOffset()); // -180 (-3 ore)
getUTCDate() - the day of the month in UTC (Coordinated Universal Time) (1-31).
var dt = new Date('Jul 11, 2018 09:25:30');
console.log(dt.getUTCDate()); // 11
getUTCDay() - returns the day of the week, according to universal time (0-6).
const days =['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

var dt = new Date('Jul 11, 2018 09:25:30');
let wday = days[dt.getUTCDay()];

console.log(wday); // Wednesday
getUTCFullYear() - the full year in UTC (Coordinated Universal Time) (four digits).
var dt = new Date('Jul 11, 2018 09:25:30');
console.log(dt.getUTCFullYear()); // 2018
getUTCHours() - returns the hour, according to universal time (0-23).
var dt = new Date('Jul 11, 2018 09:25:30');
console.log(dt.getUTCHours()); // 6
getUTCMilliseconds() - the number of milliseconds into the current second in Coordinated Universal Time (0-999).
var dt = new Date('Jul 11, 2018 09:25:30');
console.log(dt.getUTCMilliseconds()); // 0
getUTCMinutes() - the number of minutes, in UTC (Coordinated Universal Time) (0-59).
var dt = new Date('Jul 11, 2018 09:25:30');
console.log(dt.getUTCMinutes()); // 25
getUTCMonth() - the number of months into the current year in UTC (Coordinated Universal Time) (0-11).
var dt = new Date('Jul 11, 2018 09:25:30');
console.log(dt.getUTCMonth()); // 6
getUTCSeconds() - the number of seconds in Coordinated Universal Time (0-59).
var dt = new Date('Jul 11, 2018 09:25:30');
console.log(dt.getUTCSeconds()); // 30

Setter Methods

setDate() - sets the day of the month for an instance of the Date object (1-31).
var dt = new Date('Jul 11, 2018 09:25:30');
dt.setDate(15);
console.log(dt.getDate()); // 15
setFullYear() - sets the year, according to local time, and returns the new time in milliseconds.
var dt = new Date('Aug 15, 2018 09:25:30');
dt.setFullYear(2019);
console.log(dt.getFullYear()); // 2019
setHours() - sets the hours for an instance of the Date object (0-23) .
var dt = new Date('Aug 15, 2018 09:25:30');
dt.setHours(16);
console.log(dt.getHours()); // 16
setMilliseconds() - sets milliseconds (0-999).
var dt = new Date('Aug 15, 2018 09:25:30');
dt.setMilliseconds(234);
console.log(dt.getMilliseconds()); // 234
setMinutes() - sets minutes (0-59).
var dt = new Date('Aug 15, 2018 09:25:30');
dt.setMinutes(55);
console.log(dt.getMinutes()); // 55
setMonth() - sets the month for an instance of the Date object (from 0-11; 0=January, 1=February).
var dt = new Date('Aug 15, 2018 09:25:30');
dt.setMonth(4);
console.log(dt.getMonth()); // 4
setSeconds() - sets seconds (0-59).
var dt = new Date('Aug 15, 2018 09:25:30');
dt.setSeconds(23);
console.log(dt.getSeconds()); // 23
setTime() - sets the time (in milliseconds since January 1, 1970, at midnight).
var dt = new Date('Aug 15, 2018 09:25:30');
dt.setTime(1541808500 *1000);
document.write(dt);
// Sat Nov 10 2018 02:08:20 GMT+0200 (GTB Standard Time)
setUTCDate() - sets the day of the month, according to universal time (1-31).
var dt = new Date('Aug 15, 2018 09:25:30');
dt.setUTCDate(18);
console.log(dt.getUTCDate()); // 18
setUTCFullYear() - sets the full year in Coordinated Universal Time (four digits).
var dt = new Date('Aug 15, 2018 09:25:30');
dt.setUTCFullYear(2019);
console.log(dt.getUTCFullYear()); // 2019
setUTCHours() - sets the hour, according to universal time (0-23).
var dt = new Date('Aug 15, 2018 09:25:30');
dt.setUTCHours(16);
console.log(dt.getUTCHours()); // 16
setUTCMilliseconds() - sets the milliseconds, according to universal time (0-999).
var dt = new Date('Aug 15, 2018 09:25:30');
dt.setUTCMilliseconds(345);
console.log(dt.getUTCMilliseconds()); // 345
setUTCMinutes() - sets the minutes, according to universal time (0-59).
var dt = new Date('Aug 15, 2018 09:25:30');
dt.setUTCMinutes(22);
console.log(dt.getUTCMinutes()); // 22
setUTCMonth() - sets the month, according to universal time (0-11).
var dt = new Date('Aug 15, 2018 09:25:30');
dt.setUTCMonth(8);
console.log(dt.getUTCMonth()); // 8
setUTCSeconds() - sets the seconds, according to universal time (0-59).
var dt = new Date('Aug 15, 2018 09:25:30');
dt.setUTCSeconds(45);
console.log(dt.getUTCSeconds()); // 45

Methods for Conversion from Date Object

toDateString() - returns the date portion of a Date object in human readable form in American English: 'Day_name Month Nr_day Year'.
var dt = new Date('Jun 20, 2018 13:30:45');
console.log(dt.toDateString()); // Wed Jun 20 2018
toJSON() - returns a string with the specified Date object.
var dt = new Date('Jun 20, 2018 13:30:45');
console.log(dt.toJSON()); // 2018-06-20T10:30:45.000Z
toString() - returns a string that represents the specified Date object.
var dt = new Date('Jun 20, 2018 13:30:45');
console.log(dt.toString());
// Wed Jun 20 2018 13:30:45 GMT+0300 (GTB Daylight Time)
toTimeString() - returns the time portion of a Date object.
var dt = new Date('Jun 20, 2018 13:30:45');
console.log(dt.toTimeString()); // 13:30:45 GMT+0300 (GTB Daylight Time)

- A list with Date object methods to MDN: Date JavaScript.


Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Methods of the Date object

Last accessed pages

  1. Using the Bone Tool (4253)
  2. Node.js Move and Copy Directory (20134)
  3. Working with getElementById (12613)
  4. Select in MySQL, Output results in HTML Table (19513)
  5. Splat Operator in PHP (4514)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (522)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (62)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)