Javascript Course


The Date object is used to set and to get certain time values that you can use in your JS scripts.
There are two important things you should know before using this object:
1. The initial reference date is 1.1.1970. For previous date-time it uses negative values.
2. When you create a Date object, it uses the client computer (the visitor) date and time.


Defining Date object

To use this object, you need to create an instance of the Date object, with the new keyword.

Syntax:
var dt = new Date();
- 'dt' can be any variable name.
- This instruction stores the current date in the 'dt' variable, as shown in the result of the following example.
var dt = new Date();
document.write(dt);
//similar with: Mon Jul 09 2018 17:54:20 GMT+0300 (GTB Daylight Time)
Once the Date object has been created, you can use its methods to work with its components related to year, month, day, hour, minutes, seconds and milliseconds.
• You can find a list with methods of the Date object to this address: coursesweb.net/javascript/methods-date-object
Here is three examples:

1. Gets the number of milliseconds (with getTime()) of the current date and displays the Timestamp (the number of seconds from 1-01-1970).
//defines a Date object with current time
const dt = new Date();

//gets the milliseconds, then the seconds
let mils = dt.getTime();
let sec = Math.trunc(mils /1000);

//writes the Timestamp
document.write('<p>Number of seconds since 1-01-1970: '+ sec +'</p>');
2. Gets the year (with getFullYear() ), the month (with getMonth() ) and the current day number (with getDate() ), then adds them to an HTML element in the page (the first month has the value 0, the last month 11).
<p id='dv1'>YeaR, montrh and day number.</p>

<script>
//array with the names of the months
const ar_month =['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

//defines a Date object with current time
var dt = new Date();

//gets the year, number of the month, and the day in month
let year = dt.getFullYear();
let month = dt.getMonth();
let day = dt.getDate();

//adds in #dv1 current month from 'ar_month' and day number
document.getElementById('dv1').innerHTML ='Current date: '+ year +'-'+ ar_month[month] +'-'+ day;
</script>
3. Adds an hour and 30 minutes to the current date, and then displays in an HTML element the name of the day in week and the hour.
<p id='p1'>Example Date objet in JavaScript</p>

<script>
//array with days of the week
const days =['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var ad_m = 90*60*1000; //an hour and 30 minutes in milliseconds

const dt = new Date();

//gets the milliseconds from current date-time, and adds ad_m
let mils = dt.getTime();
dt.setTime(mils +ad_m);

//gets the day of the week, hour, minutes, seconds
let wday = dt.getDay();
let hour = dt.getHours();
let mins = dt.getMinutes();
let sec = dt.getSeconds();

//displays the hour in #p1
document.getElementById('p1').innerHTML ='The day and time in an hour and 30 minutes: <b>'+ days[wday] +', '+ hour +':'+ mins +':'+ sec +'</b>';
</script>

Date Object with specified date and time

For other specific (fixed) dates and time, a Date object can be created in 3 ways:

new Date(year, month, day, hours, minutes, seconds, milliseconds)
new Date(milliseconds)
new Date('date string')
Now it is presented with examples each of these modes.

new Date(year, month, ...)

new Date(year, month, ...) creates a Date object with fixed date and time. Can get 7 arguments (integer numbers), the last 5 are optionals.
Syntax:
new Date(year, month, day, hours, minutes, seconds, milliseconds)

Examples:
var dt = new Date(2018, 7, 21, 11, 35, 20); //Represents: August 21, 2018, 11:35:20
var dt = new Date(98, 0, 8, 9); //Represents: 8 January 1998, 9:00:00
var dt = new Date(2019, 11, 25); //Represents: December 25, 2019

- Example, gets the number of milliseconds from a Date object with fixed date-time, and displays the Timestamp (the number of seconds from 1-01-1970 to that date and time).
//defines a Date object for: 23 March 2018, 14:35
const dt = new Date(2018, 2, 23, 14, 35);

//gets the number of milliseconds, then the seconds
let mils = dt.getTime();
let sec = Math.trunc(mils /1000); // 1521808500

//writes the Timestamp
document.write('<p>The number of seconds from 1-01-1970 till 23-March-2018, 14:35 is: '+ sec +'</p>');

new Date(milliseconds)

new Date(milliseconds) creates a new date object as zero time plus milliseconds.
If the number of milliseconds is negative, the date is before 1970.
- Example:
//Date object with seconds * 1000 (for milliseconds)
const dt = new Date(1521808500 *1000);
document.write('<p>'+ dt +'</p>');
// Fri Mar 23 2018 14:35:00 GMT+0200 (GTB Standard Time)
With negative value:
//Date object with seconds * 1000 (for milliseconds)
const dt = new Date(-152180850 *1000);
document.write('<p>'+ dt +'</p>');
// Sat Mar 06 1965 17:32:30 GMT+0200 (GTB Standard Time)

new Date('date string')

new Date('date string') creates a new date object from a date string.
The 'date string' can have one of these formats:
new Date('Month DD, YYYY hh:mm:ss'); // Aug 05, 2018 09:25:30
new Date('Month DD YYYY'); // Aug 05 2018
new Date('DD Month YYYY'); // 05 Aug 2018
new Date('YYYY-MM-DD'); // 2018-08-05
new Date('YYYY-MM-DDThh:mm:ss'); // 2018-08-05T09:25:30

- Example, gets and displays the number of seconds between the current date and a string with another date and time.
//object with current date-time
const dt1 = new Date();

//define a Date object for 23 August 2018, 14:35
const dt2 = new Date('2018-08-25T14:35:00');

//gets the difference of milliseconds between dt1 and dt2
let mils = dt1.getTime() - dt2.getTime();

//gets seconds
let sec = Math.trunc(mils /1000);

//scrie numarul Timestamp
document.write('<p>The number of seconds from 23 August 2018, 14:35 till now: '+ sec +'</p>');

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
JS Date Object

Last accessed pages

  1. Mysql SELECT JOIN tables on two different Databases (4498)
  2. jQuery UI draggable - Drag elements (11448)
  3. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142520)
  4. Using the Bone Tool (4253)
  5. Node.js Move and Copy Directory (20134)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (523)
  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)