Javascript: Basic Date Object for Quick Reference

Here’s an exercise to create a new date object and convert today’s parts into the more human-readable month/day/year format. It’s not built into the Javascript language, what can you do?! Here’s a breakdown of some of the main methods built into the Date class, just the main ones

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January starts at ZERO, don't ask why
var yyyy = today.getFullYear();

if(dd<10) {   dd='0'+dd} 
if(mm<10) {   mm='0'+mm} 

today = mm+'/'+dd+'/'+yyyy;
document.write(today);

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *