Javascript Date Methods and Objects Tutorial With Example is today’s topic. Working with date and time in JavaScript can be complicated. JavaScript offers us the date handling functionality through a sturdy object, and it is Date. By default, JavaScript will use the browser’s time zone and display a date as the full-text string. The Date object instance represents the single point in time. Despite being named Date, it also handles time. Although, we will not talk about moment.js in this tutorial.
Javascript Date Methods and Objects
Let’s start the article by initializing the Date object.
Date objects are created with a new Date() constructor.
let d = new Date();
The new Date() creates a new date object with the current date and time. Date objects are static and computer time is ticking, but the date objects are not. Now, let’s print the current date and time.
// app.js
let d = new Date();
console.log(d);
Here, I am not running the javascript on the browser, but on the server in the form of Node.js. But the JS methods are the same for both client and server. The output is following.
There are four ways to create a new date object which are following.
new Date()
new Date(year, month, day, hours, minutes, seconds, milliseconds)
new Date(milliseconds)
new Date(date string)
Now, we have seen the first way, let’s see the second way.
// app.js
let dd = new Date(2015, 04, 14, 11, 30, 30, 300);
console.log(dd);
See the below output.
JavaScript counts months from 0 to 11. 6 numbers specify year, month, day, hour, minute, second. Seventh is milliseconds.
You cannot omit the month. If you supply only one parameter to the function, it will be treated as milliseconds. See the below example.
let dd = new Date(2015);
console.log(dd);
See the below output.
Internally, the dates are expressed in milliseconds since Jan 1st, 1970 (UTC). That date is important because as far as computers are concerned, that’s where it all began.
If we pass 0, we will get a Date object that represents the time at Jan 1st, 1970.
let s = new Date(0);
console.log(s);
The output is following.
JavaScript Get Date Methods
The following are the Javascript methods that can return the time, day, year, month, etc. Let’s see one by one.
The getDate() Method
The getDate() method returns the day of a date as a number (1-31).
// app.js
let dt = new Date();
console.log(dt.getDate());
It will return the today’s day, which is 13.
The getDay() Method
Javascript getDay() method returns the weekday of a date as a number (0-6).
// app.js
let day = new Date();
console.log(day.getDay());
See the output.
The getMonth() Method
The getMonth() method returns the month of a date as a number (0-11).
// app.js
let da = new Date();
console.log(da.getMonth());
See the output.
The getHours() Method
The getHours() method returns the hours of a date as a number (0-23).
// app.js
let daH = new Date();
console.log(daH.getHours());
See the below output.
Now, same for the getMinutes() Method, the getSeconds() Method, and the getMilliseconds() Method.
Other Examples of Javascript Date
If we pass the string rather than a number, then a Date object uses the parse method to determine which date you are moving. See the following examples.
// app.js
console.log(new Date('2018-07-22'))
console.log(new Date('2018-07'))
console.log(new Date('2018'))
console.log(new Date('07/22/2018'))
console.log(new Date('2018/07/22'))
console.log(new Date('2018/7/22'))
console.log(new Date('July 22, 2018'))
console.log(new Date('July 22, 2018 07:22:13'))
console.log(new Date('2018-07-22 07:22:13'))
console.log(new Date('2018-07-22T07:22:13'))
console.log(new Date('25 March 2018'))
console.log(new Date('25 Mar 2018'))
console.log(new Date('25 March, 2018'))
console.log(new Date('March 25, 2018'))
console.log(new Date('March 25 2018'))
console.log(new Date('March 2018'))
console.log(new Date('2018 March'))
console.log(new Date('2018 MARCH'))
console.log(new Date('2018 march'))
See the following output.
You can also use the Date.parse() method. The Date.parse() method returns a timestamp (in milliseconds) rather than a Date object. See the below example.
// app.js
console.log( Date.parse('2018-07-22'))
console.log( Date.parse('2018-07'))
console.log( Date.parse('2018'))
console.log( Date.parse('07/22/2018'))
console.log( Date.parse('2018/07/22'))
console.log( Date.parse('2018/7/22'))
console.log( Date.parse('July 22, 2018'))
console.log( Date.parse('July 22, 2018 07:22:13'))
console.log( Date.parse('2018-07-22 07:22:13'))
console.log( Date.parse('2018-07-22T07:22:13'))
console.log( Date.parse('25 March 2018'))
console.log( Date.parse('25 Mar 2018'))
console.log( Date.parse('25 March, 2018'))
console.log( Date.parse('March 25, 2018'))
console.log( Date.parse('March 25 2018'))
console.log( Date.parse('March 2018'))
console.log( Date.parse('2018 March'))
console.log( Date.parse('2018 MARCH'))
console.log( Date.parse('2018 march'))
The output is following.
Finally, Javascript Date Methods and Objects Tutorial With Example is over.
The post Javascript Date Methods and Objects Tutorial With Example appeared first on AppDividend.