0

I am trying to make a kind of resource planning manager basically kind of a calendar. And I need to loop through a full year.

I have the following code:

for (var x = 0; x < 365; x++){
    var today = new Date(2015, 01, x);
    document.write(today + "<br />");
}

Its kind of what I want but then I just want to show the day, the date, the month and the year, now it also shows the time. How to hide the time. Check this link to see my result (http://gyazo.com/3df91f5292072a54415d13b2ccca4922)

AstroCB
  • 11,800
  • 20
  • 54
  • 68
GY22
  • 695
  • 1
  • 13
  • 43
  • 1
    Think you're looking for this: [How to format a JavaScript date](http://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – Matthew Davis Jun 28 '15 at 18:31

2 Answers2

1

You can get the part of the date you need. For example:

var weekday = new Array(7);
weekday[0]=  "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";

function formatDate(date) {
 var month = date.getUTCMonth() +1;
 var dayNumber = date.getUTCDate();
 var year = date.getUTCFullYear();
 var day = date.getUTCDay();
 return year + "-" + month + "-" + dayNumber + " " + weekday[day];
}

So you code becomes:

for (var x = 0; x < 365; x++){
    var today = formatDate(new Date(2015, 01, x));
    document.write(today + "<br />");
}

P.S. note the +1 when calculating the month.

Link: https://jsfiddle.net/ajsv5ge4/

Giuseppe Pes
  • 7,070
  • 3
  • 41
  • 80
  • thanks for the code it works but i would also like to show the day like Mo, Tu, We, etc... – GY22 Jun 28 '15 at 18:39
  • hi thanks for the quick response. i updated the code with the array of weekday but when i run it i get an error in console saying that : Uncaught TypeError: date.getUTCFullYear is not a function ... – GY22 Jun 28 '15 at 18:56
  • @GY22 fixed. I accidentally use the same name for the number of date – Giuseppe Pes Jun 28 '15 at 19:02
0

You can use some lib like MomentJS:

moment().format('MMMM Do YYYY')
stdob--
  • 25,371
  • 5
  • 48
  • 60