Creating Dates In JavaScript: Hello everyone, welcome to the nkcoderz.com website. In this article we will going to discuss about the Creating Dates In JavaScript.
Creating Dates In JavaScript
In JavaScript, you can create dates using the Date
object. The basic syntax to create a new date is:
Syntax For Creating Date Object
let date = new Date();
This will create a new date object with the current date and time. You can also create a date object with a specific date and time by passing arguments to the Date
object’s constructor:
let date = new Date(year, month, day, hours, minutes, seconds, milliseconds);
Here, year
is a four-digit number representing the year, month
is an integer from 0 to 11 representing the month (0 is January, 1 is February, and so on), day
is an integer from 1 to 31 representing the day of the month, hours
is an integer from 0 to 23 representing the hour, minutes
is an integer from 0 to 59 representing the minute, seconds
is an integer from 0 to 59 representing the second, and milliseconds
is an integer from 0 to 999 representing the millisecond.
For example:
let date = new Date(2022, 0, 1, 0, 0, 0, 0);
This will create a date object representing January 1st, 2022 at 00:00:00.
You can also create a date from a string using the Date.parse()
method:
let date = Date.parse("2022-01-01T00:00:00");
This will parse the string and return the number of milliseconds between the parsed date and January 1, 1970, 00:00:00 UTC. You can then pass this number to the Date
constructor to create a date object.
let date = new Date(Date.parse("2022-01-01T00:00:00"));
Once you have a Date
object, you can use its various methods to perform operations on the date and time, such as getting the year, month, day, hour, minute, and second, or setting them.
For example:
let date = new Date();
let year = date.getFullYear();
let month = date.getMonth();
let day = date.getDate();
let hours = date.getHours();
let minutes = date.getMinutes();
let seconds = date.getSeconds();
This will get the year, month, day, hour, minute, and second of the current date and time and store them in the corresponding variables.
You can also format a date into a string using the toDateString()
and toTimeString()
methods:
let date = new Date();
let dateString = date.toDateString();
let timeString = date.toTimeString();
This will convert the date and time into a human-readable string and store them in the dateString
and timeString
variables, respectively.
Additionally, you can use the toISOString()
method to convert the date into an ISO 8601-formatted string:
let date = new Date();
let isoString = date.toISOString();
This will convert the date into an ISO-formatted string and store it in the isoString
variable.
Finally, you can use the setFullYear()
, setMonth()
, setDate()
, setHours()
, setMinutes()
, and setSeconds()
methods to set the year, month, day, hour, minute, and second of the date, respectively:
let date = new Date();
date.setFullYear(2022);
date.setMonth(0);
date.setDate(1);
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
This will set the date to January 1st, 2022 at 00:00:00.
Code To Get Current Date
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
console.log(today);
This code creates a new Date
object, which represents the current date and time. The getDate()
method is used to get the day of the month (1-31), the getMonth()
method is used to get the month (0-11), and the getFullYear()
method is used to get the year (e.g. 2021).
The values obtained from getDate()
and getMonth()
are converted to strings and padded with leading zeros if needed (using the padStart
method) so that the resulting date string always has the format mm/dd/yyyy
.
Conclusion
If you liked this post Creating Dates In JavaScript, then please share this with your friends and make sure to bookmark this website for more awesome content.