zoukankan      html  css  js  c++  java
  • 常用JS代码

    包含字符串

    indexOf()!=-1

    if(Ids.indexOf(node.id)!=-1){  //包含
        node.check = true;   //处理
    }

    数组大小

    arr.length;

    获取随机数

        /**
         * 生成从minNum到maxNum的随机数
         * @param minNum 最小值,若只有一个参数则这个值为上限
         * @param maxNum 最大值
         * @returns {number}
         */
        function randomNum(minNum,maxNum){
            switch(arguments.length){  //参数长度
                case 1:  //当只有一个参数时,取值范围为1-指定参数值
                    return parseInt(Math.random()*minNum+1,10);  //10为十进制
                    break;
                case 2:  //当参数值为两个时,取值范围为minNum ~ maxNum
                    return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10);
                    break;
                default:  //否则返回o
                    return 0;
                    break;
            }
        }

    获取时间戳

    时间转时间戳:javascript获得时间戳的方法有四种,都是通过实例化时间对象 new Date() 来进一步获取当前的时间戳

    1.var timestamp1 = Date.parse(new Date()); // 结果:1477808630000 不推荐这种办法,毫秒级别的数值被转化为000

      console.log(timestamp1);

    2.var timestamp2 = (new Date()).valueOf(); // 结果:1477808630404 通过valueOf()函数返回指定对象的原始值获得准确的时间戳值

    console.log(timestamp2);

    3.var timestamp3 = new Date().getTime(); // 结果:1477808630404 ,通过原型方法直接获得当前时间的毫秒值,准确

    console.log(timestamp3);

    4.var timetamp4 = Number(new Date()) ; //结果:1477808630404 ,将时间转化为一个number类型的数值,即时间戳

    console.log(timetamp4);

    时间戳转时间

    查看mysql数据库时间戳格式.,数据库时间戳为10位,单位为秒s

    查看java代码时间戳格式,java时间戳为13位,单位为ms

    //方法 一
    System.currentTimeMillis();
    //方法 二
    Calendar.getInstance().getTimeInMillis();
    //方法 三
    new Date().getTime();

    查看js时间戳格式,js时间戳为13位格式为ms

    当前端处理后端的直接发过来的时间戳时,要*1000转换为毫秒才可以

    var timestamp = new Date(1472048779952);  //直接用 new Date(时间戳) 格式转化获得当前时间,如果数据库里的时间戳是10位,这里要*1000

    console.log(timestamp);

    console.log(timestamp.toLocaleDateString().replace(///g, "-") + " " + timestamp.toTimeString().substr(0, 8));   //再利用拼接正则等手段转化为yyyy-MM-dd hh:mm:ss 格式

        //将时间戳转换为时间
        function unixToDateTime(sql_timestamp){  //后端传过来的时间戳为10位,单位为秒
            var timestamp = new Date(sql_timestamp*1000);  //将秒转为毫秒
            return timestamp.toLocaleDateString().replace(///g, "-") + " " + timestamp.toTimeString().substr(0, 8);
        }

    判断是否为空

    /*方法一: !*/
    var content=$("content").val();
    if(!content){  //相当于判断content=""、content=null、content = undefined、content=0
         alert("请输出内容!");
         return;
    }
    
    /*方法二:if*/
    function isEmpty(value){
        if(value == null || value == "null" || value == "" || value == "undefined" || value == undefined ){
            return "";
        }else{
            return value;
        }
    }
  • 相关阅读:
    WGS84经纬度坐标与web墨卡托之间的转换【转】
    ArcGIS API for Javascript配置
    百度地图BMap API实例
    VS2010 Web项目需要缺少的Web组件才能加载
    单态模式
    对服务的操作
    根据子级ID获取其所有父级
    在DropDownList里显示多级分类
    jQuery给CheckBox添加事件
    FolderBrowserDialog使用
  • 原文地址:https://www.cnblogs.com/aeolian/p/9564396.html
Copyright © 2011-2022 走看看