Write a function normalize, that replaces '-' with '/' in a date string.
Example: normalize('20-05-2017') should return '20/05/2017'.
I have tried this:
let d = new Date('27-11-2021');
function normalize(session){
let normal = d.replace('-','/');
return (session);
}
As you are being asked to pass a string representation of a date to the normalize function, you can use String.prototype.replaceAll() to replace each - with / in the string.
For this problem, there does not appear to be a need to parse the string as an actual javascript Date object.
// Write a function normalize, that replaces '-' with '/' in a date string.
// Example: normalize('20-05-2017') should return '20/05/2017'.
function normalize(stringDate) {
const normal = stringDate.replaceAll('-', '/');
return normal;
}
const result = normalize('20-05-2017')
console.log(result)
Try This or Use replaceAll instead of replace function because in new ES replaceAll is working.
let date = normalize('20-05-2017');
console.log('date: ',date);
function normalize(date){
return (date.replace(/-/g,'/'));
}
First off, the date is invalid for some reason. In the constructor, you cannot pass in 27-8-2021. So for example, you can go this way.
let d = new Date();
d.toLocaleDateString() // returns '8/27/2021'
d.toLocaleDateString('en-in') // returns '27/8/2021'