Skip to content
the poor coder

javascript get date

Poor Man
Published
Reading time
1 min read
JavaScript

// Get today's date in Javascript
var today = new Date();

// Get year
var year = today.getFullYear();

// Get month
var month = today.getMonth();

// Get day
var day = today.getDate();

// Get milliseconds since 1 January 1970
var milliseconds = today.getTime();

// Get timezone offset (in minutes)
var timezoneOffset = today.getTimezoneOffset();

// Get day of week (0 to 6)
var dayOfWeek = today.getDay();

// Format date
// Day/Month/Year
var date = day + "/"+ (month + 1) + "/" + year;

// Output
console.log(date);

The above code will output the current date in the format of Day/Month/Year. For example, if today is December 21, 2020, it will output 21/12/2020.

You can also use the toDateString() method to get a more readable date. For example, the following code will output Monday, December 21, 2020:

JavaScript

// Get date
var date = today.toDateString();

// Output
console.log(date);

You can also get the hour, minute, and second of the current time in Javascript by using the getHours(), getMinutes(), and getSeconds() methods. For example, the following code will output the current hour, minute, and second:

JavaScript

// Get hour
var hour = today.getHours();

// Get minute
var minute = today.getMinutes();

// Get second
var second = today.getSeconds();

// Output
console.log(hour + ":" + minute + ":" + second);

You can also use the toTimeString() method to get a more readable time. For example, the following code will output 3:37:21 PM:

JavaScript

// Get time
var time = today.toTimeString();

// Output
console.log(time);

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