Get current date in DD-Mon-YYY format in JavaScript/Jquery

后端 未结 15 1729
攒了一身酷
攒了一身酷 2020-12-01 03:06

I need to get the date format as \'DD-Mon-YYYY\' in javascript. I had asked a question, and it got marked duplicate to jQuery date formatting

But, the answers provi

15条回答
  •  情歌与酒
    2020-12-01 03:22

    Here's a simple solution, using TypeScript:

      convertDateStringToDate(dateStr) {
        //  Convert a string like '2020-10-04T00:00:00' into '4/Oct/2020'
        let months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
        let date = new Date(dateStr);
        let str = date.getDate()
                    + '/' + months[date.getMonth()]
                    + '/' + date.getFullYear()
        return str;
      }
    

    (Yeah, I know the question was about JavaScript, but I'm sure I won't be the only Angular developer coming across this article !)

提交回复
热议问题