Wednesday, 15 April 2015

how to convert month name october to 10 in javascript -



how to convert month name october to 10 in javascript -

this question has reply here:

easiest way convert month name month number in js ? (jan = 01) 8 answers

in java script given month "october"

how convert month number format?

output should "10", example.

i have variable called monthname giving value "october",

now how can convert monthname value 10

you can this

function getmonthname() { getmonthname.mnths = getmonthname.mnths || ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]; homecoming (getmonthname.mnths.indexof(monthname.tolowercase()) + 1); } console.log(getmonthname("october")); // 10

just create array of months, , add together 1 index of month you're trying find.

method lot more efficient using date object, array of month names cached , done, instead of creating whole new date object unnecessary properties.

this solution 34% faster using date object. solution not allow terms such oct , doesn't care locales, isn't asked for, anyway. if need them, go date object.

also, among solutions new date("october 1").getmonth() + 1 give nan in ie10 & 11(the latest of ies) , in latest mozilla firefox.

this solution cross-browser compatible, more efficient, , not bad way of solving problem. js perf

javascript

No comments:

Post a Comment