1

I have a for loop and I want to display the date for each of the results of this loop. I can do this now but it displays like this:

2018-04-19 12:00:00

And I want it to display like this:

19-04-2018

I am able to target substrings and get the layout I want that way:

    forecastDayEl= sessionStorage.getItem("forecastDay");

    var objDay = JSON.parse(forecastDayEl);

objDay.forEach(function(entry) {

            var time = entry.substring(11, 13);
            var days = entry.substring(8, 10);
            var month = entry.substring(5, 7);
            var year = entry.substring(0, 4);

});

But I am not sure how to add this to the existing for loop:

var forecastLength;
var forecastText;
var g;

    forecastLength = objTemp.length;
    forecastText = "<div class='forecastList'>";
    for (g = 0; g < forecastLength; g++) {

        forecastText += "<ul>" + "<li class='forecastDay'>" + objDay[g] + "</li>" + "<li class='forecastTemp'>" + objTemp[g] + "</li>" + "<li class='forecastIcon'>" + "<img id='icon'" + "src='/weather/icons/" + objIcon[g] + ".png'" + "alt='Weather icon'>" + "</li>" + "</ul>";
    }
    forecastText += "</div>";

    //outputs result as list
    document.getElementById("forecastList").innerHTML = forecastText;

forecastDayEl is outputting:

["2018-04-19 12:00:00","2018-04-20 12:00:00","2018-04-21 12:00:00","2018-04-22 12:00:00","2018-04-23 12:00:00"]

objDay is outputting:

(5) ["2018-04-19 12:00:00", "2018-04-20 12:00:00", "2018-04-21 12:00:00", "2018-04-22 12:00:00", "2018-04-23 12:00:00"]
0
:
"2018-04-19 12:00:00"
1
:
"2018-04-20 12:00:00"
2
:
"2018-04-21 12:00:00"
3
:
"2018-04-22 12:00:00"
4
:
"2018-04-23 12:00:00"

var days is outputting:

19
20
21
22
23

It would be ideal to avoid this hassle if I could do something like:

objDay[g].toDateString();

But this does not work as:

objDay[g].toDateString is not a function

2
  • I don't think this has anything to do with jQuery. Commented Apr 19, 2018 at 2:37
  • There are lots of questions already about reformatting date strings. The simplest way is to split into values, then format as required. Don't parse to Dates then format from there. Commented Apr 19, 2018 at 2:39

2 Answers 2

1

Use .map to transform objDay into an array of strings in the desired format, and then use the transformed array instead:

const input = JSON.parse('["2018-04-19 12:00:00","2018-04-20 12:00:00","2018-04-21 12:00:00","2018-04-22 12:00:00","2018-04-23 12:00:00"]');
const output = input.map((dateStr) => {
  const [year, month, day] = dateStr.match(/\d+/g);
  return [day, month, year].join('-');
});
console.log(output);

Sign up to request clarification or add additional context in comments.

Comments

0

The simplest way to convert 2018-04-19 12:00:00 to 19-04-2018 is to get the values and reorder them. There are many ways to do that, .e.g.

function reformatDate0(s) {
  return s.split(' ')[0].split('-').reverse().join('-')
}

function reformatDate1(s) {
  return s.split(/\D/).splice(0,3).reverse().join('-');
}

function reformatDate2(s) {
  var b = s.split(/\D/);
  return b[2]+'-'+b[1]+'-'+b[0];
}

var s = '2018-04-19 12:00:00';
console.log(reformatDate0(s));
console.log(reformatDate1(s));
console.log(reformatDate2(s));

And so on…

Comments

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.