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 renders as emphasized text, displaying the text oblique?
<strong> <pre> <em>
<p>Web development courses: <em>CoursesWeb.net</em></p>
Which CSS property defines the space between the element border and its content?
margin padding position
h3 {
  padding: 2px 0.2em;
}
Click on the method which returns the first element that matches a specified group of selectors.
getElementsByName() querySelector() querySelectorAll()
// gets first Div with class="cls", and shows its content
var elm = document.querySelector("div.cls");
alert(elm.innerHTML);
Indicate the PHP variable that contains data from a form sent with method="post".
$_SESSION $_GET $_POST
if(isset($_POST["field"])) {
  echo $_POST["field"];
}
Methods of the Date object

Last accessed pages

  1. Adding text with ActionScript 3 (5638)
  2. CSS cursor property - Custom Cursors (6372)
  3. Zodiac Signs PHP code (7271)
  4. Using openssl_encrypt and openssl_decrypt in PHP (1312)
  5. CSS Box Model (768)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (458)
  2. CSS cursor property - Custom Cursors (68)
  3. Read Excel file data in PHP - PhpExcelReader (46)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (44)
  5. PHP Unzipper - Extract Zip, Rar Archives (41)