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,019
  • 1
  • 33
  • 47
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,552
  • 2
  • 15
  • 35
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
  • 38,402
  • 15
  • 99
  • 123
Rameshbabu
  • 76
  • 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)
Shubh
  • 8,368
  • 4
  • 18
  • 39
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
  • 12,701
  • 3
  • 23
  • 44