V
V
VANY2018-03-15 00:23:44
JavaScript
VANY, 2018-03-15 00:23:44

How to get month ordinal from string in JS?

There is a date "January 1, 1970", there is a variable that always contains the string of the month (trimmed with spaces), you need to get the ordinal number of the month into the month_number variable (in the case of "January" month_number = 1).
===
solved the problem like this
5aa9a87c8dea5225582642.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
VoidVolker, 2018-03-15
@vanyproduction

var m = {
    "январь": 1,
    "февраль": 2,
    "март": 3,
    "апрель": 4,
    "май": 5,
    "июнь": 6,
    "июль": 7,
    "август": 8,
    "сентябрь": 9,
    "октябрь": 10,
    "ноябрь": 11,
    "декабрь": 12
};

< m["январь"]
> 1

D
Daniel, 2018-03-15
@daniil14056

That's how it will be, although it would be better to immediately decide on the months, they will end in "ь" or "я".

/// Быстро и сразу изменить строку на лету
var str="1 января1844";
var newStr= str.replace (/\d+\s+([^ ]+)\s+\d+/, function(str,s1,s2){
     var sN="";
     if(s1[s1.length-1]==='я') 
              sN=s1.substring(0,s1.length-1)+"ь";
     else sN=s1;
    if(sN && m[sN]!="undefined")    return str.replace(s1,m[sN]); 
     else return str;
});
//  Получить номер месяца
var number;
var mouth="1 января 1844" .match(/\d+\s+([^ ]+)\s+\d+/)[1]; 
number=m[mouth];
 if(number==undefined) 
           number=m[mouth.substring(0,mouth.length-1)+"ь"];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question