Convert month to month numbers

Hello, I am trying to do the opposite, because now it is changing from 2015/Sau/01to 2015/01/01I don’t know how to get back to 2015/Sau/01how I can do this with this script, because I need to save the locale language, please help me.

var date = f.task_end_date.value.split("/");
var months = ['Sau', 'Vas', 'Kov', 'Bal', 'Geg', 'Bir','Lie', 'Rgp', 'Rgs', 'Spa', 'Lap', 'Grd'];
for(var j=0;j<months.length;j++){
    if(date[1]==months[j]){
         date[1]=months.indexOf(months[j])+1;
     }                      
} 
if(date[1]<10){
    date[1]='0'+date[1];
}                        
var formattedDate = date[0]+date[1]+date[2];
+4
source share
3 answers

You should simply replace:

for(var j=0;j<months.length;j++){
    if(date[1]==months[j]){
         date[1]=months.indexOf(months[j])+1;
     }                      
} 
if(date[1]<10){
    date[1]='0'+date[1];
}           

with:

date[1] = months[parseInt(date[1],10)-1];

parseIntwill take your string 01through 12and give you the integral value, then you subtract it to get the index, and find the equivalent month name (I think, although I am not familiar with this particular language).

In other words, the final code is:

var date = f.task_end_date.value.split("/");
var months = ['Sau', 'Vas', 'Kov', 'Bal', 'Geg', 'Bir','Lie', 'Rgp', 'Rgs', 'Spa', 'Lap', 'Grd'];
date[1] = months[parseInt(date[1],10)-1];
var formattedDate = date[0] + date[1] + date[2];

parseInt(), , Javascript ( ToNumber), :

date[1] = months[date[1]-1];

, , , ( , , ).

, , , , . , .

+1

, :

var months = ['Sau', 'Vas', 'Kov', 'Bal', 'Geg', 'Bir', 'Lie', 'Rgp', 'Rgs', 'Spa', 'Lap', 'Grd'];

var dateArray = "2015/01/01".split("/");
console.log(dateArray);
var newDate="";

if (dateArray[1] <= 12) {
  newDate = dateArray[0] + "/" + months[dateArray[1] - 1] + "/" + dateArray[2];
  alert(newDate);
}
Hide result

"2015/01/01" , .

+3

:

var months = ['Sau', 'Vas', 'Kov', 'Bal', 'Geg', 'Bir',
                'Lie', 'Rgp', 'Rgs', 'Spa', 'Lap', 'Grd']
function transformMonth (str, toText) {
    str = str.split('/')
    str[1] = toText ? months[str[1] - 1] : 1 + months.indexOf(str[i])
    return str
}

console.log(transformMonth('2015/Sau/01', false)) // --> '2015/01/01'
console.log(transformMonth('2015/01/01', true))   // --> '2015/Sau/01'

, :

/            # start regex
(\d+)        # capture as many digits as possible
\/           # until hitting a forward slash
(\w+)        # capture as many characters as possible
\/           # until a forward slash
(\d+)        # capture the remaining numbers
/            # end regex

Then all you have to do is return the first capture group (year) plus (1 + months.indexOf(month)(month) plus the third capture group (day).

var months = ['Sau', 'Vas', 'Kov', 'Bal', 'Geg', 'Bir',
                'Lie', 'Rgp', 'Rgs', 'Spa', 'Lap', 'Grd']

// this transforms '2015/Sau/01 to '2015/01/01'
function toNumberMonth (str) {
    return str.replace(/(\d+)\/(\w+)\/(\d+)/, function (date, year, month, day) {
        return year + '/' + (1 + months.indexOf(month)) + '/' + day
    })
}

Returning is a matter of setting up a regular expression for use (\d+)in all three capture groups:

// this transforms '2015/01/01' to '2015/Sau/01'
function toTextMonth (str) {
    return str.replace(/(\d+)\/(\d+)\/(\d+)/, function (date, year, month, day) {
        return year + '/' + months[month-1] + '/' + day
    })
}
+2
source

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


All Articles