0

Convert Date to DDMMYYYY from any other formats

var date = new Date("2019/12/27"),
yr = date.getFullYear(),
month = date.getMonth() < 10 ? '0' + date.getMonth() : date.getMonth(),
day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(),
newDate = yr + '-' + month + '-' + day;

I tried like this but it is not accepting '27/12/2019'

Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
  • Does this answer your question? [Extending JavaScript's Date.parse to allow for DD/MM/YYYY (non-US formatted dates)?](https://stackoverflow.com/questions/3003355/extending-javascripts-date-parse-to-allow-for-dd-mm-yyyy-non-us-formatted-date) – Peter B Dec 27 '19 at 13:51
  • 1
    There is no way to do that from "any other format". You would need to specify the supported formats. – str Dec 27 '19 at 13:52
  • 1
    `new Date(Date.parse("2019/12/27"))`? – kevinSpaceyIsKeyserSöze Dec 27 '19 at 13:52

2 Answers2

2

Use the Moment.js library http://momentjs.com/ It will save you a LOT of trouble.

moment().format('DDMMMYYYY');
BukhariBaBa
  • 171
  • 5
0

Split date string

var dateArr = String('27/12/2019').split('/');
var date = new Date (dateArr[2], dateArr[1] - 1, dateArr[0]);

this is a part from another answer Convert dd-mm-yyyy string to date

Community
  • 1
  • 1
Maximilian Fixl
  • 670
  • 6
  • 23