Popular Tags

How to display date in Javascript?

programix · ·
var dateObject = new Date();

The dateObject now contains the current date that can be used in various ways. 

How to get day, month and year from the Date object?

Simply by using the predefined method like getDay(), getMonth()  and getFullYear(). For example, if you want to output the standard international date format YYYY-MM-DD (defined in ISO 8601 standard), just use something like:

var year = dateObject.getFullYear();
var month = dateObject.getMonth();
var day = dateObject.getDay();

if (day < 10) day = '0' + day;
if (month < 10) month = '0' + month;

var output =  year + '-' + month + '-' + day; 

Looks really to much code for a standard date format but you get the point. There is also a quicker way by just using a substring of the toISOString() method:

var output = dateObject.toISOString().substring(0,10);

How to format a JS date in locale format?

I most cases you should display a date format that the user understands and not your preferred format. There is a cool method called toLocaleDateString().

var output = dateObject.toLocaleDateString();


You can also check out this nicely formatted date example code.
Pinned Post
[Snippets] Pure JS AJAX Request
Simple GET request 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 var xmlHttp = new XMLHttpRequest(); //define request xmlHttp...