I'm using parsehub to extract data. There is a date in the format thursday 22 december 2022
but I need it in a date string dd/mm/yyyy
. Is there a way to do that with regex javascript?
I can't find info about a way to solve this.
I'm using parsehub to extract data. There is a date in the format thursday 22 december 2022
but I need it in a date string dd/mm/yyyy
. Is there a way to do that with regex javascript?
I can't find info about a way to solve this.
You could solve it without regex
using only JavaScript
1: Parse your string into a date
2: Extract and concatenate needed values using JS
built-in date methods
function formatDate(date) {
function checkZero(num){return (num < 10)? '0'+num : num}
let parsed = new Date(date);
return `${checkZero(parsed.getDate())}/${checkZero(parsed.getMonth() + 1)}/${parsed.getFullYear()}`;
}
console.log('Case1: '+formatDate('Thursday 22 december 2022'));
console.log('Case2: '+formatDate('Monday 4 July 2022'));
Or simply, you can use this line of code
let foratedDate = new Date('Thursday 22 december 2022').toLocaleDateString('FR-fr');
Note: The 2 solutions return the same result but, the first one has more flexibility in formatting to many other shapes
You can do it without regex as well
function myDateFormat(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
return [day, month, year].join('/');
}
console.log(myDateFormat('Thursday 22 december 2022'));
The above will not add '0' to the month and date so you need to add a condition to get the date and month as 'dd' and 'mm' instead of d and m.
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
Simply :
please note the addition of zeros on months, days when these are less than 10
const strDate2dmy = dteStr => new Date(dteStr).toLocaleDateString('fr-FR');
console.log( strDate2dmy( 'Thursday 22 december 2022' ) ) // 22/12/2022
console.log( strDate2dmy( 'Monday 4 July 2022' ) ) // 04/07/2022
Thanks for the answers but that doesn't work with parsehub.
a alternative is to do it with PHP code. Found a option in one of the plugins to work with custom php to change value of data.
https://www.wpallimport.com/documentation/inline-php/
any suggestions
One hacky way is:
new Date('thursday 22 december 2022').toJSON().slice(0, 10).split('-').reverse().join('/');
Result: "22/12/2022"
Or perhaps try this:
new Date('thursday 22 december 2022').toLocaleDateString()