1

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);

3 Answers 3

1

You need to include the assignment to format within the for loop and attach the appropriate separator to each element. Here is the modified code:

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];

let format = "";
const separators = ["", "-", "-", " ", ":"];
// 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];
    }
    format += separators[i] + dates[i];
}
console.log(format);
Sign up to request clarification or add additional context in comments.

Comments

1

Maybe you can try something like this

function td(n) {
    return ('' + n).length == 2 ? n : '0' + n;
}

let date = new Date();
let format = date.getFullYear() + '-' +
    td(date.getMonth() + 1) + '-' +
    td(date.getDate()) + ' ' +
    td(date.getHours()) + ':' +
    td(date.getMinutes());

console.log(format);

Comments

0

Your array contains copies of the year, month, etc variables. Changing the values in the array will have no effect on the original variables.

What you could do is use a second array to hold the separator characters that go between the component values, and build your result string during the iteration:

var seps = ["-", "-", " ", ":"];
format = ""; // initialize the result formatted date
for (var i = 0; i < dates.length; ++i) {
  if (i > 0)
    format += seps[i - 1]; // add separator
  var d = dates[i].toString();
  if (d.length < 2)
    d = "0" + d;
  format += d;
}

1 Comment

Thanks for the quick reply, any suggestions on what I can do for my situation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.