jquery - Converting a dynamic 4-digit year to a 2 digit year using Javascript - Stack Overflow

admin2025-04-03  0

I need to convert a dynamically generated date from something like 20-Apr-2013 to 20.04.13. So far I managed to convert the month and change the spacers. But converting the year still escapes me. here is what I came up with so far. How to move forward?

$(document).ready(function() {


                $('.date').each( function() {


    var oldDate = $(this).text();

    var month;

    if( oldDate.indexOf('-') > 0 ){

        var dateSplit = oldDate.split('-');

        var year = dateSplit[2];

        if( year.length == 2){
            year = year;
        }

        switch(dateSplit[1])
        {
            case 'Jan': month = "01";
            break;
            case 'Feb': month = "02";
            break;
            case 'Mar': month = "03";
            break;
            case 'Apr': month = "04";
            break;
            case 'May': month = "05";
            break;
            case 'Jun': month = "06";
            break;
            case 'Jul': month = "07";
            break;
            case 'Aug': month = "08";
            break;
            case 'Sep': month = "09";
            break;
            case 'Oct': month = "10";
            break;
            case 'Nov': month = "11";
            break;
            case 'Dec': month = "12";
            break;
        }

        $(this).text(dateSplit[0] + '.' + month  + '.' + year);
    }
    else if( oldDate.indexOf(('/') > 0 ) ){

        var dateSplit = oldDate.split('/');

        var year = dateSplit[2];

        if( year.length == 2){
            year = year;
        }

    }


    });

  });

I need to convert a dynamically generated date from something like 20-Apr-2013 to 20.04.13. So far I managed to convert the month and change the spacers. But converting the year still escapes me. here is what I came up with so far. How to move forward?

$(document).ready(function() {


                $('.date').each( function() {


    var oldDate = $(this).text();

    var month;

    if( oldDate.indexOf('-') > 0 ){

        var dateSplit = oldDate.split('-');

        var year = dateSplit[2];

        if( year.length == 2){
            year = year;
        }

        switch(dateSplit[1])
        {
            case 'Jan': month = "01";
            break;
            case 'Feb': month = "02";
            break;
            case 'Mar': month = "03";
            break;
            case 'Apr': month = "04";
            break;
            case 'May': month = "05";
            break;
            case 'Jun': month = "06";
            break;
            case 'Jul': month = "07";
            break;
            case 'Aug': month = "08";
            break;
            case 'Sep': month = "09";
            break;
            case 'Oct': month = "10";
            break;
            case 'Nov': month = "11";
            break;
            case 'Dec': month = "12";
            break;
        }

        $(this).text(dateSplit[0] + '.' + month  + '.' + year);
    }
    else if( oldDate.indexOf(('/') > 0 ) ){

        var dateSplit = oldDate.split('/');

        var year = dateSplit[2];

        if( year.length == 2){
            year = year;
        }

    }


    });

  });
Share asked Apr 11, 2013 at 1:56 Yes I am a catYes I am a cat 2791 gold badge6 silver badges14 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 8

It's simple arithmetic:

year = year % 100;

But why do you want to do this? Don't you remember the Y2K problem?

You can try the library moment.js it is meant to solve this exact problem. Even if you don't use it, the source code may provide some pointers.

Can you convert oldDate to a Date object and go from there?

var newDate = new Date(oldDate);
var month = newDate.getMonth();
var year = newDate.getFullYear().toString().substr(2, 2); //get the last 2 digits of the full year

Here is a reference of the functions on a javascript date object

"20-Apr-2013".replace(/(\d+)-(\w+)-(\d+)/,function(p,p1,p2,p3) {
  return p1+'.'+String("00"+('janfebmaraprmayjunjulaugsepoctnovdec'.indexOf(p2.toLowerCase())/3+1)).slice(-2)+'.'+p3.slice(-2);
});

You'd be better converting it to a Date first:

$('.date').each(function() {

    var d = new Date($(this).text());

    $(this).text(d.getDate() + '.' + (d.getMonth() + 1) + '.'
                    + ('' + d.getFullYear()).substr(2, 2));

});
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1743689118a215508.html

最新回复(0)