更改日期格式。?

时间:2011-09-05 08:20:59

标签: javascript jquery

如何在此处将正常数字格式更改为日期格式(http://jsfiddle.net/prL83/)?

以此格式dir="rtl"从右到左YYYY/MM/DD键入日期。

  1. 首先输入日期(DD
  2. secend输入月份(MM
  3. thrid键入年份(YYYY

    $('。find_input')。delegate(“input.numeric:text”,“keyup”,function(){         $ val = $(this).val()。match(/ [0-9] / g).reverse()。join(“”)。match(/ [0-9] {1,3} / g) 。。加入( “ ”)匹配(/./克).reverse()加入(“”);         $(本).VAL($ VAL)     });

1 个答案:

答案 0 :(得分:0)

这会有用吗?

$dateSet = 0;
$monthSet = 0;
$yearSet = 0;

$('.find_input').delegate("input.numeric:text", 'keyup', function () {
    $val = $(this).val().replace(/[^\d]+/g, "").match(/\d{1,12}$/);
    if($val == null) {
        return;
    } else {
        $val = $val.join("");
    }

    if($(this).val().match(/\d{4,}$/) && $val.length%2 == 0) {
        $val = $val.match(/\d{2}/g);
        if($yearSet < $monthSet) {
            if($val.length == 4) {
                $(this).val($val.join("").replace(/(\d{2})(\d{2})(\d{4})$/,'$3/$1/$2'));
                $yearSet++;
            } else if($val.length == 6){

                $(this).val($val.join("").replace(/(\d{4})(\d{2})(\d{2})(\d{4})$/,'$4/$2/$3'));
                $yearSet++;
            }
        } else {
            if($monthSet < $dateSet) {
                $(this).val($val.join("").replace(/(\d{4})(\d{2})(\d{2})(\d{2})$/,'$1/$4/$3'));
                $monthSet++;
            } else {

                if($val.length == 2) {
                       $(this).val($val.reverse().join("/"));
                    $dateSet++;
                    $monthSet++;
                } else {
                       $(this).val($val.join("").replace(/(\d{4})(\d{2})(\d{2})(\d{2})$/,'$1/$2/$4'));
                    $dateSet++;
                }
            }
        }
    }
});