I have a date in following format:
13 Dec,2021 12:59:58
and I want to change it into:
13-12-2021
pls suggest some fix
CodePudding user response:
Convert it do a Date
, get the make a string out of the components.
The month needs to add 1 since for some reason, months are zero-indexed (January == 0, February == 1, ..., December == 11)
const dateString = "13 Dec,2021 12:59:58";
const getNewString = (dateString) => {
const date = new Date(dateString);
return `${date.getDate()}-${date.getMonth() 1}-${date.getFullYear()}`;
}
console.log(getNewString(dateString));
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
You can do something like this
const oldDate = "13 Dec,2021 12:59:58";
const newDate = new Date(oldDate);
console.log(newDate.getDate() "-" (newDate.getMonth() 1) "-" newDate.getFullYear());
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
You can use moment.js to do this.
console.log(moment('13 Dec,2021 12:59:58').format('DD-MM-YYYY'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>