While I am working on a node project from nodeschool.io, one of the things I need to do is construct a date format, and make sure that is correctly formatted in accordance to how it is displayed. Here is the format: "YYYY-MM-DD hh:mm" I use the date object to grab the date info I need, then I gather it all into an array to create an easy for loop that will convert it to string to make certain formatting practices easier. One specific formatting practice I've been attempting to do is to append a "0" to month and day since the format requires two digits in month and day, however, the two numbers only have 1 digit since that is the current date. For some odd reason, the 0 will not be appended.
var date = new Date();
// Date Format: "YYYY-MM-DD hh:mm"
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDay();
var hour = date.getHours();
var minute = date.getMinutes();
var dates = [year, month, day, hour, minute];
// Conversion and Formatting
for (var i = 0; i < dates.length; i++) {
dates[i] = dates[i].toString();
if (dates[i].length < 2) {
dates[i] = "0" + dates[i];
}
}
var format = year + "-" + month + "-" + day + " " + hour + ":" +
minute;
console.log(format);