Javascript Date objects complete reference. Besides the Math object, the Javascript has a date object that is often used in building web applications. Date Object has functions that can manipulate the date and time in the browser. In this article, we will discuss the functions in date object, and the most frequently tricks used to manage Date and time.
Table of Contents
- 1 How to create Date in Javascript
- 2 The function of displaying the date object Javascript
- 3 The Most Frequently used Date object Function.
- 4 A collection of tricks for manipulating date objects
- 4.1 How to get the first date of current month in Javascript
- 4.2 How to get first date of month in specific year and month Javascript
- 4.3 How to get last date of current month in Javascript
- 4.4 How to get last date of month in specific year and month Javascript
- 4.5 How to convert date object Javascript to MySQL format dates
- 5 Conclusion
How to create Date in Javascript
Using Date() object to create date and time in Javascript. There are several ways to declare the date object as follows:
new Date()
In general, to create Javascript date and time use new Date(). New date object will display the current time in format string text.
Example:
1 2 3 4 | let d = new Date() console.log(d) //result Wed Jul 17 2019 23:10:13 GMT+0700 (Indochina Time) |
By default, Javascript will calculate the month from 0 as of January and 11 as of December.
Also, Javascript date object has several constructors that can be used to showing the time with the specific conditions.
new Date(year, month, day, hour, minute, second, millisecond)
This way will show the date and time using year, month, day, hour, minute, second, millisecond parameters
Example
1 2 | new Date(2019,07,15,20,23,30,0) //Thu Aug 15 2019 20:23:30 GMT+0700 (Indochina Time) |
Beside to declare the certain Date completely (7 parameters from year to the millisecond), Object date also declare the dates until the remaining 2 parameters (year, month)
Example :
1 2 3 4 5 | new Date(2019,07,15,20,23,30) new Date(2019,07,15,20,23) new Date(2019,07,15,20) new Date(2019,07,15) new Date(2019,07) |
If you only use 1 parameter (an example new Date(2019)), it will create a new date object in millisecond format.
Example:
1 2 3 4 | new Date(0) //Thu Jan 01 1970 07:00:00 GMT+0700 (Indochina Time) new Date (2000000000) //Sat Jan 24 1970 10:33:20 GMT+0700 (Indochina Time) |
new Date(date string)
Used to create new date object with format string parameter
Example
1 2 | new Date("April 12, 2019 9:16:20"); //Fri Apr 12 2019 09:16:20 GMT+0700 (Indochina Time) |
The function of displaying the date object Javascript
By default, new Date() will showing date and time in full format string text
Several functions to showing the date in Javascript.
toUTCString()
Used to showing current date in UTC timezones format
Example:
1 2 3 | let d = new Date() console.log(d.toUTCString()) //"Wed, 17 Jul 2019 16:18:20 GMT" |
toDateString()
Used to showing date and time in readable format
Example:
1 2 3 | let d = new Date() console.log(d.toDateString()) //"Wed Jul 17 2019" |
toLocaleString()
Used to showing the date formats in locale string
Example:
1 2 3 | let d = new Date() console.log(d.toLocaleString()); //"7/17/2019, 11:18:20 PM" |
toLocaleDateString()
The function to displaying the date formats in string data type.
1 2 3 | let d = new Date() console.log(d.toLocaleDateString()); //"7/17/2019" |
toLocaleTimeString()
Used to showing local time format
Example:
1 2 3 | let d = new Date() console.log(d.toLocaleTimeString()); //"11:18:20 PM" |
The following are some of the functions most often used in making web applications.
The Most Frequently used Date object Function.
getFullYear()
The function used to get year info from date() object
Example:
1 2 3 | let d = new Date() console.log(d.getFullYear()) //2019 |
getMonth()
Function used to get month info in date object (0 as January – 11 as December)
Example
1 2 3 | let d = new Date() console.log(d.getMonth()) //7 |
getDate()
Function to showing day from 1 – 31
Example
1 2 3 | let d = new Date() console.log(d.getDate()) //16 |
getHours()
Function to displaying time in hours (0 – 23)
Example
1 2 3 | let d = new Date() console.log(d.getHours()) //22 |
getMinutes()
Function used to showing minutes (0 – 59)
Example
1 2 3 | let d = new Date() console.log(d.getMinutes()) //32 |
getSeconds()
Date() object function to showing seconds (0 – 59)
Example
1 2 3 | let d = new Date() console.log(d.getSeconds()) //23 |
getMilliseconds()
Used to showing milliseconds (0 until 999 millisecond)
Example
1 2 3 | let d = new Date() console.log(d.getMilliseconds()) //457 |
getTime()
Used to get the time
Example
1 2 3 | let d = new Date() console.log(d.getTime()) //1563380300476 |
getDay()
Javascript function to get day in number format(0 – 6)
Example
1 2 3 | let d = new Date() console.log(d.getDay()) //3 |
Date.now()
Used to get the current time in millisecond like getTIme() ( ECMAScript 5).
A collection of tricks for manipulating date objects
How to get the first date of current month in Javascript
1 2 3 4 5 | function start_date(){ let date = new Date(), y = date.getFullYear(), m = date.getMonth(); let firstDay = new Date(y, m, 1); return firstDay; } |
Above function used to get the first date of month, see the example below:
1 2 | console.log(start_date()); // result : Mon Jul 01 2019 00:00:00 GMT+0700 (Western Indonesia Time) |
How to get first date of month in specific year and month Javascript
1 2 3 4 | function start_date_by_month_year(m,y){ let firstDay = new Date(y, m-1, 1); return firstDay; } |
How to use:
1 | start_date_by_month_year(4,2019) |
How to get last date of current month in Javascript
1 2 3 4 5 | function end_date(){ let date = new Date(), y = date.getFullYear(), m = date.getMonth(); let lastDay = new Date(y, m + 1, 0); return lastDay; } |
The above function will return the last date of current month
1 2 | console.log(end_date()) // result Wed Jul 31 2019 00:00:00 GMT+0700 (Western Indonesia Time) |
How to get last date of month in specific year and month Javascript
1 2 3 4 | function end_date_by_month_year(m,y){ let lastDay = new Date(y, m, 0); return lastDay; } |
How to use:
1 | end_date_by_month_year(8,2019) |
How to convert date object Javascript to MySQL format dates
1 2 3 4 5 6 7 8 | function formatMysqlDate(objDate) { month = '' + (objDate.getMonth() + 1), day = '' + objDate.getDate(), year = objDate.getFullYear(); if (month.length < 2) month = '0' + month; if (day.length < 2) day = '0' + day; return [year, month, day].join('-'); } |
Example:
1 2 3 | let objDate = new Date(); console.log( formatMysqlDate(objDate) ) //result : 2019-07-16 |
Conclusion
Javascript provides several objects used to speed up building web apps. Showing date in Javascript with date object is support all browser.
Thus my tutorial about javascript date object, Look forward to the next tutorial javascript here
Reference : https://developer.mozilla.org/id/docs/Web/JavaScript/Reference/Global_Objects/Date
Leave a Reply