Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

For loop Datetime and push to the array

Hey guys!!! I was wondering how to push the datetime item into an array? The format like this:

This is a number item: 1567221430. First I need to convert this number to Datetime like this: var d = new Date(1567221430); The method is succeed.

But thats not what i want, i want to push the datetime item into an array, and also the format should like this:"yyyy/mm/dd". This is the format that i want to push to array. But i dont know how to achieve this. And also, i dont want to push single item, i want to push a lot of datetime item. I know i should use for loop, but i dont know how to use......

So, please someone can help me out and thank you so much!

1 Answer

Thom Benjamin
Thom Benjamin
10,494 Points

So, you have a lot of numbers and you have to convert them all into dates to put into an array? I would do it like this.

First, make an array with all your numbers (e.g.), and an array for the final dates.

var numberArr = [42142412, 1251235523, 12341253, 1235123513];
var dateArr = [];

Then loop through that array. for (i = 0; i < numberArr; i++) {};

Then there's 3 steps:

  1. Within this loop, you're gonna have to convert the numberArr[i] to a Date format
  2. Then convert this to the markup you want to use
  3. Then add that new variable to the dateArr

Hope this helps you!

Thank you so much!!! But i want to convert these datetime format to this: "yyyy/mm/dd" format. Please help me one question for me, thank you so much!