4

Hello I'm trying to do opposite stuff to this, because now it change from 2015/Sau/01 to 2015/01/01 I don't know how to change back to 2015/Sau/01, how can I do this with this script, because I need to keep 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];
cнŝdk
  • 28,676
  • 7
  • 47
  • 67
McLaren
  • 766
  • 1
  • 9
  • 21
  • 2
    FYI `months.indexOf(months[j]) === j` – Hacketo Jul 16 '15 at 09:04
  • no need to loop, just use array: `date[1]=['Sau', 'Vas', 'Kov', 'Bal', 'Geg', 'Bir','Lie', 'Rgp', 'Rgs', 'Spa', 'Lap', 'Grd'][ f.task_end_date.value.split("/")[1]-1]` – dandavis Jul 16 '15 at 09:08

3 Answers3

3

Here's a simple way of doing it, just split your date and change the month part according to the path number:

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);
}

I used "2015/01/01" as a date example you just need to change it.

cнŝdk
  • 28,676
  • 7
  • 47
  • 67
  • @RobG No he did this, but now want it back to `2015/Sau/01`, it's saying *I don't know how to change back to 2015/Sau/01*. – cнŝdk Jul 16 '15 at 09:17
  • 1
    BTW, there's no need for parseInt, the `-` operator coerces strings to number anyway, so just `months[dateArray[1] - 1]`. ;-) – RobG Jul 16 '15 at 09:24
2

Just avoid regex altogether:

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'

I'm usually not a fan of regular expressions, but this one gets the job done nicely:

/            # 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 capturing group (year) plus (1 + months.indexOf(month) (month) plus the third capturing 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
    })
}

Going back is just a matter of adjusting the regex to use (\d+) in all three capturing 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
    })
}
royhowie
  • 10,605
  • 14
  • 45
  • 66
1

You should just be able to 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];

The parseInt will take your string 01 through 12 and give you the integral value, then you subtract one to get the index and look up the equivalent month name (I guess, though I'm not familiar with that specific language).

In other words, the final code would be:

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];

You can also avoid parseInt() if you wish since Javascript will treat a string as a number (via the ToNumber operation) when you subtract one:

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

It probably won't reduce the actual work being done since the conversion from string to numeric has to be done whether explicit or implicit (it may even be slower since the implicit conversion has to handle the possibility of hex values but it's unlikely to matter a lot either way).

So, if you value succinctness of code, it's certainly an option. I prefer the explicit version myself but you may have other priorities.

paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841