Convert string containing monthName to Int of MonthDigit

I have a line in which there is a short month name. \

string month = "Jun"; 

I need to get a month in the figure from this month.

Let's say I do this:

 int monthInDigit = getMonth(month); monthInDigit <-- 6 

How can i achieve this. If you cannot get my question, please, I will explain it correctly.

Thanxx in advance

+6
source share
3 answers
 int monthInDigit = DateTime.ParseExact(month, "MMM", CultureInfo.InvariantCulture).Month; 
+13
source

First you can parse it before a DateTime :

 DateTime dt = DateTime.ParseExact("Jun", "MMM", CultureInfo.InvariantCulture); int month = dt.Month; if(month < 6) { // do something... } 
+7
source

another way:

 string [] arr= {jun, fab,...}; public int getMonth(string month){ for(int i = 0 ; i< arr.length ; i++){ if(string[i].Contains(month) || month.Contains(arr[i])) return i+1; } return -1;// if month is invalid , return -1 } 
0
source

Source: https://habr.com/ru/post/955393/


All Articles