0

Hello I want to convert

March 2018 to 032018 

in jQuery.I used

var  d = new Date($('.selected_month').find("td:first").text());

But it is giving result is:

Thu Mar 01 2018 00:00:00 GMT+0530 (IST)
Shahzad Intersoft
  • 762
  • 2
  • 14
  • 35

4 Answers4

1

You need to use getMonth() and getFullYear() on returned date object to format date as per requirement. Also, you need to add 1 to returned month as getMonth method is 0 index based:

(d.getMonth()+1).toString() + d.getFullYear().toString()
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

Try this

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

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

    return [month,year].join('');
}

Call this function : formatDate('March 2018') // output : 032018

Chintan
  • 133
  • 8
0

Try converting the date object you end up with to a string: Try the following snippet, just change var d = new Date() to var d = new Date($('.selected_month').find("td:first").text()).

var  d = new Date();
var twoDigitMonth = (d.getUTCMonth() + 1).toString().padStart(2, "0");
var year = d.getUTCFullYear();
var result = twoDigitMonth + year;
console.log(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
vahdet
  • 6,357
  • 9
  • 51
  • 106
0

...or try this...

console.log(
    new Date("March 2018")
    .toLocaleDateString('en-EN', {month: '2-digit',year: 'numeric'})
    .replace('/','')
)
bbsimonbb
  • 27,056
  • 15
  • 80
  • 110