Home > Enterprise >  How to convert timestamp to date in an array created with a for-loop?
How to convert timestamp to date in an array created with a for-loop?

Time:09-14

I created an array with a for-loop which can show me all days in a certain timespan every week or every two weeks, it worked, but now i would like to convert these timestamps to local german time (i used .toLocaleDateString("de-DE") dd.mm.yyyy). I am not able to solve the problem above and seek for your help. I tried it, as mentioned, with .toLocaleDateString("de-DE") but it was no success.

My Code for both days:

function datenSpuckerErsterTag(){

  let bTagEins = Date.parse(document.getElementById("firstDatum").value);

  let bTagzwei = Date.parse(document.getElementById("secondDatum").value);

  let startDatum1 = Date.parse(document.getElementById("startDatum").value );

  let endDatum1 = Date.parse(document.getElementById("endDatum").value);



  const siebenTage = 86400 * 1000*7;
  const vierzehnTage = 86400 * 1000*14 ;


  if (valueGetter1() == 7) {
    let datenListe = [];

    for (let i = bTagEins; i <= endDatum1; i = i   siebenTage) {

      datenListe.push(i);
      console.log(datenListe);
      document.getElementById("arrOnePrint").innerHTML = datenListe;
    }
  } else if (valueGetter1() == 14){

    let datenListe = [];
    for(let i = bTagEins;i <= endDatum1;i=i   vierzehnTage) {

      datenListe.push (i);
      console.log(datenListe);
      document.getElementById("arrOnePrint").innerHTML = datenListe;
    }
  } else {}
}

function datenSpuckerZweiterTag(){

  let bTagEins = Date.parse(document.getElementById("firstDatum").value);

  let bTagZwei = Date.parse(document.getElementById("secondDatum").value);

  let startDatum1 = Date.parse(document.getElementById("startDatum").value );

  let endDatum1 = Date.parse(document.getElementById("endDatum").value);



  const siebenTage = 86400 * 1000*7;
  const vierzehnTage = 86400 * 1000*14 ;


  if (valueGetter2() == 7) {
    let datenListe = [];

    for (let i = bTagZwei; i <= endDatum1; i = i   siebenTage) {

      datenListe.push(i);
      console.log(datenListe);
      document.getElementById("arrTwoPrint").innerHTML = datenListe;
    }
  } else if (valueGetter2() == 14){

    let datenListe = [];
    for(let i = bTagZwei;i <= endDatum1;i=i   vierzehnTage) {

      datenListe.push (i);
      console.log(datenListe);
      document.getElementById("arrTwoPrint").innerHTML = datenListe;
    }
  } else {}
}

Thank you for your help! I really appreciate any advice! <3

CodePudding user response:

Hope this helps:

const date = new Date();

// ✅ Get a string according to a provided Time zone
console.log(
  date.toLocaleString('en-US', {
    timeZone: 'America/Los_Angeles',
  }),
); //            
  • Related