Just want to covert Jan to 01 (date format)
I can use array()
but looking for another way...
Any suggestion?
var monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
then just call monthNames[1] that will be Feb
So you can always make something like
monthNumber = "5";
jQuery('#element').text(monthNames[monthNumber])
function getMonthDays(MonthYear) {
var months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
var Value=MonthYear.split(" ");
var month = (months.indexOf(Value[0]) + 1);
return new Date(Value[1], month, 0).getDate();
}
console.log(getMonthDays("March 2011"));
I usually used to make a function:
function getMonth(monthStr){
return new Date(monthStr+'-1-01').getMonth()+1
}
And call it like :
getMonth('Jan');
getMonth('Feb');
getMonth('Dec');
Here is a simple one liner function
//ECHMA5
function GetMonth(anyDate) {
return 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'.split(',')[anyDate.getMonth()];
}
//
// ECMA6
var GetMonth = (anyDate) => 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'.split(',')[anyDate.getMonth()];
Another way;
alert( "JanFebMarAprMayJunJulAugSepOctNovDec".indexOf("Jun") / 3 + 1 );
Here is another way :
var currentMonth = 1
var months = ["ENE", "FEB", "MAR", "APR", "MAY", "JUN",
"JUL", "AGO", "SEP", "OCT", "NOV", "DIC"];
console.log(months[currentMonth - 1]);