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
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
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));
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());
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>