前端校验日期选择是否有效,且转换日期格式为YYYY-MM-dd

旧城等待, 2022-12-04 12:58 246阅读 0赞
  1. //对日期进行转化格式 YYYY-MM-DD
    function getNowFormatDate() {
    date = new Date(); //获取系统当前日期
    var seperator1 = “-”;
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
    month = “0” + month;
    }
    if (strDate >= 0 && strDate <= 9) {
    strDate = “0” + strDate;
    }
    currentdate = year + seperator1 + month + seperator1 + strDate;
    return currentdate; //将系统当前日期进行转化,转化为yyyy-mm-dd
    }
  2. //日期比较函数
    function StringToDate(Str)
    {
    return new Date(Str.replace(/-/,”/“));
    }
  3. 在调用时:加入判断
    if(StringToDate(currentdate)<StringToDate(rateTime)){ //获取当前时间和之前的时间进行比较
    $(“#datePercent”).val(“请输入合法日期”);
    }else{
    rateMapFun(rateTime,null);
    }

发表评论

表情:
评论列表 (有 0 条评论,246人围观)

还没有评论,来说两句吧...

相关阅读