0

I've this code below:

var ValorData = document.getElementById("RetiraValorDate").innerHTML.replace(/\s/g,'');

enter image description here

Without the innerHTML and Replace the result is this:

enter image description here

How can I convert the result to a date format?

James Coyle
  • 9,922
  • 1
  • 40
  • 48
KmDroid
  • 131
  • 4

4 Answers4

0

You can do something like this:

const dateParts = '18/09/2019'.split('/');
const newDate = new Date(dateParts[2], dateParts[1], dateParts[0]);

console.log(newDate.toDateString());
amedina
  • 2,838
  • 3
  • 19
  • 40
0

Split the data (in your case "18/02/2019") with slash and pass in correct format that is ISO

var dtArr = ValorData.split("/"); var requriedDate = new Date(dtArr[2]+"-"+dtArr[1]+"-"+dtArr[0]);

in above example requriedDate will contain your date object and you can compare with any other date object.

str
  • 42,689
  • 17
  • 109
  • 127
Rameshbabu
  • 96
  • 7
0

You can first reverse the date and then ,use Date object to retrieve js date

let str="18/02/2019";

function reverseString(str) {
  
    var splitString = str.split("/");
 
  
    var reverseArray = splitString.reverse(); 
 
 
    var joinArray = reverseArray.join("/"); 
   
    return joinArray; 
}

console.log(reverseString(str))

let date =new Date(reverseString(str));
console.log(date)
Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46
0

If you want to get a date, split the string on the slashes, convert each part to a number, then feed the parts in the Date constructor. Do not forget to substract 1 from the month since month are 0-based in JavaScript dates:

const dateParts = document.getElementById("RetiraValorDate")
  .innerHTML
  .replace(/\s/g,'')
  .split('/')
  .map(Number);
  
console.log(new Date(dateParts[2], dateParts[1]-1, dateParts[0]));
<span id="RetiraValorDate">
    18/02/2019
    </span>
jo_va
  • 13,504
  • 3
  • 23
  • 47