Skip to content
the poor coder

add date in javascript

Poor Man
Published
Reading time
1 min read

Adding a date in Javascript is a fairly simple process. All you need to do is create a Date object and use the .getDate() method to get the day of the month. This method will return the day of the month as an integer from 1-31.

Text
let currentDate = new Date();
let dayOfMonth = currentDate.getDate();
console.log("Day of the month is " + dayOfMonth);

You can also use the .getMonth() and .getFullYear() methods to get the month and year respectively. This method will return the month as an integer from 0-11 and the year as an integer from 0-9999.

Text
let currentDate = new Date();
let month = currentDate.getMonth();
let year = currentDate.getFullYear();
console.log("Month is " + month + " and year is " + year);

You can also use the .toDateString() method to get a string representation of a date. This method will return the date in the format of "Day, Month Date, Year".

Text
let currentDate = new Date();
let dateString = currentDate.toDateString();
console.log("Current date is " + dateString);

Finally, you can use the .toLocaleDateString() method to get a localized representation of a date. This method will return the date in the format specified by the user's locale settings.

Text
let currentDate = new Date();
let localizedDateString = currentDate.toLocaleDateString();
console.log("Localized date is " + localizedDateString);

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC