• js获取当前日期、前一天、后一天的日期的例子


    <script>
    function addByTransDate(dateParameter, num) {
        var translateDate = "", dateString = "", monthString = "", dayString = "";
        translateDate = dateParameter.replace("-", "/").replace("-", "/"); 
        var newDate = new Date(translateDate);
        newDate = newDate.valueOf();
        newDate = newDate + num * 24 * 60 * 60 * 1000;
        newDate = new Date(newDate);
        //如果月份长度少于2,则前加 0 补位   
        if ((newDate.getMonth() + 1).toString().length == 1) {
    monthString = 0 + "" + (newDate.getMonth() + 1).toString();
        } else {
    monthString = (newDate.getMonth() + 1).toString();
        }
        //如果天数长度少于2,则前加 0 补位   
        if (newDate.getDate().toString().length == 1) {
    dayString = 0 + "" + newDate.getDate().toString();
        } else {
    dayString = newDate.getDate().toString();
        }
        dateString = newDate.getFullYear() + "-" + monthString + "-" + dayString;
        return dateString;
    }
     
    function reduceByTransDate(dateParameter, num) {
        var translateDate = "", dateString = "", monthString = "", dayString = "";
        translateDate = dateParameter.replace("-", "/").replace("-", "/"); 
        var newDate = new Date(translateDate);
        newDate = newDate.valueOf();
        newDate = newDate - num * 24 * 60 * 60 * 1000;
        newDate = new Date(newDate);
        //如果月份长度少于2,则前加 0 补位   
        if ((newDate.getMonth() + 1).toString().length == 1) {
    monthString = 0 + "" + (newDate.getMonth() + 1).toString();
        } else {
    monthString = (newDate.getMonth() + 1).toString();
        }
        //如果天数长度少于2,则前加 0 补位   
        if (newDate.getDate().toString().length == 1) {
    dayString = 0 + "" + newDate.getDate().toString();
        } else {
    dayString = newDate.getDate().toString();
        }
        dateString = newDate.getFullYear() + "-" + monthString + "-" + dayString;
        return dateString;
    } 
     
    //得到日期  主方法
    function showTime(pdVal) {
        var trans_day = "";
        var cur_date = new Date();
        var cur_year = new Date().format("yyyy");
        var cur_month = cur_date.getMonth() + 1;
        var real_date = cur_date.getDate();
        cur_month = cur_month > 9 ? cur_month : ("0" + cur_month);
        real_date = real_date > 9 ? real_date : ("0" + real_date);
        eT = cur_year + "-" + cur_month + "-" + real_date;
        if (pdVal == 1) {
    trans_day = addByTransDate(eT, 1);
        }
        else if (pdVal == -1) {
    trans_day = reduceByTransDate(eT, 1);
        }
        else {
    trans_day = eT;
        }
       //处理
        return trans_day;
    }

    调用示例:

    //当前日期
    var tcur=showTime(0);
    //前一天
    var tqyt=showTime(-1);
    //后一天
    var thyt=showTime(1);

    附,JS得到当前日期时间的小例子

    function currentTime() {
        var d = new Date();
        var vYear = d.getFullYear();
        var vMon = d.getMonth() + 1;
        var vDay = d.getDate();
        var h = d.getHours(); www.jquerycn.cn
        var m = d.getMinutes();
        var se = d.getSeconds();
        var s = vYear+"-"+ (vMon < 10 ? "0" + vMon : vMon)+"-" + (vDay < 10 ? "0" + vDay : vDay)+" " + (h < 10 ? "0" + h : h)+":" + (m < 10 ? "0" + m : m)+":" + (se < 10 ? "0" + se : se);
        return s;
    }
  • 相关阅读:
    asterisk配置SIP服务器
    虚拟机桥接网卡下配置centOS静态IP
    在centOS5.9安装asterisk
    Cut 命令截取不同空格的string
    shell 中 贪婪匹配 和 非贪婪匹配
    shell 一些例子
    linux 系统时间 EST CST
    awk简单应用
    python3.5-ssh免输入密码过程
    GitHub个人使用入门
  • 原文地址:https://www.cnblogs.com/yaomajor/p/6256292.html
Copyright © 2020-2023  润新知