-2

There is an input of this format:

2021-04-02T16:06:09.61

How can it be formatted to be in DD-MMM-YYYY form?

For example, for the above input it should return 02-Apr-2021

Jean Pierre
  • 281
  • 8
  • 21
  • Does this answer your question? [Format JavaScript date as yyyy-mm-dd](https://stackoverflow.com/questions/23593052/format-javascript-date-as-yyyy-mm-dd) – Mehan Alavi Aug 02 '21 at 14:00

3 Answers3

1

You tagged your question with , so you can use the format method, like this:

console.log(moment("2021-04-02T16:06:09.61").format("DD-MMM-YYYY"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

More info: https://momentjs.com/docs/#/displaying/format/

sdgluck
  • 24,894
  • 8
  • 75
  • 90
1

in vanilla javascript

const d = new Date();

let day = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(d);
let month = new Intl.DateTimeFormat('en', { month: 'short' }).format(d);
let year = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(d);

console.log(`${day} ${month} ${year}`);
AlexPixel
  • 389
  • 7
  • 17
0

you can use this

function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) 
        month = '0' + month;
    if (day.length < 2) 
        day = '0' + day;

    return [year, month, day].join('-');
}
 
console.log(formatDate('Sun May 11,2014'));

copied from Format JavaScript date as yyyy-mm-dd

Mehan Alavi
  • 278
  • 3
  • 17
  • 1
    If you're copying the answer to another question, then likely this is a duplicate so mark the question as a duplicate. :-) – RobG Aug 02 '21 at 22:03