0

I converted a unix timestamp from an API to a more readable date using this code:

var newTimeStamp = new Date(); newTimeStamp.setTime(date*1000); date = newTimeStamp.toUTCString();

(date is a variable I set for the date data coming in from the API which I reassigned to this new format).

That generated the date like this:

Wed, 11 May 2016 16:00:00 GMT
Thu, 12 May 2016 16:00:00 GMT
Fri, 13 May 2016 16:00:00 GMT
Sat, 14 May 2016 16:00:00 GMT

I really only need the day of the week, day and month. Is there an easy way to limit this data?

colmol29
  • 47
  • 6

1 Answers1

0

If you just want the first parts from the date like Wed, 11 May from the UTC string, then you can use the regular expression to do that.

var regex = /(\w{3}, \d{2} \w{3})(?:.+)/;
var date = new Date();
var outputStr = "";
for(var i = 0; i < 7; i++) {
  date.setDate(date.getDate() + 1 )
  var dateStr = date.toUTCString();
  dateStr = dateStr.replace(regex, "$1");
  
  outputStr += dateStr + '\n';
}

document.querySelector("#output").innerHTML = outputStr;
<pre id="output">
  </pre>
11thdimension
  • 9,084
  • 1
  • 20
  • 60
  • How can I edit that regex code so that it automatically generates the day of the week, right now when I put that code through my loop, every day is getting displayed as Wednesday, 11 May – colmol29 May 11 '16 at 22:45
  • You need complete day name `Wednesday` instead of `Wed` ? – 11thdimension May 12 '16 at 15:55
  • No, sorry. The code your wrote prints the same day over and over again. I already have it in a for loop, but it's not working the way I want it to. It's looping the same date, I want it to increment by a day each time. – colmol29 May 12 '16 at 17:11
  • Updated the code, check it. – 11thdimension May 12 '16 at 20:00