I've this code below:
var ValorData = document.getElementById("RetiraValorDate").innerHTML.replace(/\s/g,'');
Without the innerHTML and Replace the result is this:
How can I convert the result to a date format?
I've this code below:
var ValorData = document.getElementById("RetiraValorDate").innerHTML.replace(/\s/g,'');
Without the innerHTML and Replace the result is this:
How can I convert the result to a date format?
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());
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.
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)
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>