<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="../Script/jQuery/jquery-1.6.2.min.js" type="text/javascript"></script>
    <script src="../Script/MTHCRMWidget/MTHCRMWidget.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            myClick();//点击事件触发
        })

        //专门包装点击事件;
        function myClick() {
            $(".tbBtn").click(function () {
                var sid = $(this).attr("id");
                var agoDate = "";
                var Cdate = new Date();
                if (sid == "CbtnNull") {
                    $("#txtCallCycleBegin").val("");
                    $("#txtCallCyclecurrend").val("");
                } else if (sid == "CbtnMoon") {
                    agoDate = ProcessDate(30);
                    $("#txtCallCycleBegin").val("{0}-{1}-{2}".format(agoDate.Year, agoDate.Moon, agoDate.Day));
                    $("#txtCallCyclecurrend").val("{0}-{1}-{2}".format(Cdate.getFullYear(), Cdate.getMonth() + 1, Cdate.getDate()));
                } else {
                    agoDate = ProcessDate(7);
                    $("#txtCallCycleBegin").val("{0}-{1}-{2}".format(agoDate.Year, agoDate.Moon, agoDate.Day));
                    $("#txtCallCyclecurrend").val("{0}-{1}-{2}".format(Cdate.getFullYear(), Cdate.getMonth() + 1, Cdate.getDate()));
                }
            })
        }

        //处理日期的函数,返回一个字面量;
        function ProcessDate(type) {
            //1.0获取现在时间的年月日:
            var currentTime = new Date("2016-01-02"); //得到当前的时间
            var currentYear = currentTime.getFullYear(); //得到当前的年份
            var currentMoon = currentTime.getMonth() + 1; //得到当前的月份(系统默认为0-11,所以要加1才算是当前的月份)
            var currentDay = currentTime.getDate(); //得到当前的天数

            //2.0获取当前时间的一个月内的年月日:(一个月内的大众业务需求为:当前时间的月份-1,当前时间的天数+1)
            var agoDay = "";
            var agoMoon = currentMoon;
            var agoYear = currentYear;
            var max = "";
            switch (type) {
                case 30:
                    agoDay = currentDay + 1;
                    agoMoon = currentMoon - 1;
                    max = new Date(agoYear, agoMoon, 0).getDate(); //获取上个月的总天数
                    break;
                case 7:
                    agoDay = currentDay - 6;
                    if (agoDay < 0) {
                        agoMoon = currentMoon - 1;//月份减1
                        max = new Date(agoYear, agoMoon, 0).getDate(); //获取上个月的总天数
                        agoDay = max + agoDay;//天数在上个月的总天数的基础上减去负数
                    }
                    break;
            }

            //3.0对处理的年月日作逻辑判断

            //如果beginDay > max(如果是当前时间的天数+1后的数值超过了上个月的总天数: 天数变为1,月份增加1)
            if (agoDay > max) {
                agoDay = 1;
                agoMoon += 1;
            }

            //如果月份当月为1月的时候, 那么一个月内:  年:-1  月:12  日:依然不变
            if (agoMoon == 0) {
                agoMoon = 12;
                agoYear = currentYear - 1;
            }

            //4.0对已经处理好的数据作格式处理(单位数则自动补零)
            currentMoon = Appendzero(currentMoon);
            currentDay = Appendzero(currentDay);
            agoMoon = Appendzero(agoMoon);
            agoDay = Appendzero(agoDay);

            //5.0帮助代码
            console.log("当前时间为:{0}-{1}-{2}".format(currentYear, currentMoon, currentDay));
            console.log("一个月前的时间为{0}-{1}-{2}".format(agoYear, agoMoon, agoDay));

            return { "Year": agoYear, "Moon": agoMoon, "Day": agoDay };
        }

        //处理各位数为零的数字(单位数则加0)
        function Appendzero(obj) {
            if (obj < 10) {
                return "0" + obj;
            } else {
                return obj;
            }
        }

    </script>
</head>
<body>
    <input type="button" class="tbBtn"  id="CbtnNull" style="background-color:#e3e3e3" value="不限"/>
    <input type="button" class="tbBtn" id="CbtnMoon" style="width: 80px; margin-left: 5px; margin-right: 5px;" value="一个月内"/>
    <input type="button" class="tbBtn" id="CbtnWeek" style="width: 80px; margin-left: 5px; margin-right: 5px;" value="一周内"/>
    <input id = "txtCallCycleBegin" type="text"/>
    <input id = "txtCallCyclecurrend" type="text"/>
</body>
</html>

javascript 关于一周前一个月前的处理方法的更多相关文章

  1. [转]Java中一周前一个月前时间计算方法

    Java中一周前一个月前时间计算方法 在java语言中,用如下方法获取系统时间: Date date = new Date(); String year=new SimpleDateFormat(&q ...

  2. [python实用代码片段]python获取当前时间的前一天,前一周,前一个月

    python获取当前时间的前一天,前一周,前一个月. 实用python的datetime.timedelta方法,避免了有的月份是30和31等不同的情况. 获取前一个月的时间,方法实现:首先datet ...

  3. JavaScript时间处理之几个月前或几个月后的指定日期

    在平常项目开发过程中,经常会遇到需要在JavaScript中处理时间的情况,无非两种(1,逻辑处理  2,格式转换处理).当然要说相关技术博,园子里闭着眼睛都能抓一把,但是我要做的是:既然有幸被我碰到 ...

  4. Oracle获取一周前,一个月前,一年前, 本周,本月,当年的日期

    1.获取当前时间一周前的日期 ' day from dual 类似的 --当前时间减去7分钟的时间 ' MINUTE from dual --当前时间减去7小时的时间 ' hour from dual ...

  5. Javascript实现时间转换为多少天前

    //实现时间转换,这篇文章发布时间为 2016/1/20 19:59:30 文章发表时间戳为:1453291170000于:11月前function getDateDiff(dateTimeStamp ...

  6. C#根据当前时间获取周,月,季度,年度等时间段的起止时间

    最近有个统计分布的需求,需要按统计本周,上周,本月,上月,本季度,上季度,本年度,上年度等时间统计分布趋势,所以这里就涉及到计算周,月,季度,年度等的起止时间了,下面总结一下C#中关于根据当前时间获取 ...

  7. Javascript获取某个月的天数-简单方法 .(转别人的)

    Javascript里面的new  Date("xxxx/xx/xx")这个日期的构造方法有一个妙处,当你传入的是"xxxx/xx/0"(0号)的话,得到的日期 ...

  8. 用js获取周、月第一天和最后一天(转载)

    var getCurrentWeek = function (day) { var days = ["周日", "周一", "周二", &q ...

  9. php获取前一天,前一个月,前一年的时间

    获取前一天的时间: $mytime= date("Y-m-d H:i:s", strtotime("-1 day")); 获取三天前的时间: $mytime= ...

随机推荐

  1. 关于Qt在子线程中使用QMessageBox的折衷方法

    Qt将所有GUI相关的处理都限制在主线程中,这么做有助于防止意想不到的访问冲突产生,但也限制了线程中某些简单的UI交互的实现,比如QMessageBox. 因为QMessageBox必须在主线程中打开 ...

  2. Chrome不支持NPAPI的信息与替代方案

    昨天Chrome稳定版更新到了42版,发现百度云.支付宝.网银等等的插件都失效了,打开 chrome://plugins/ 一看,NPAPI都消失了,只有flash的插件还在.解决办法是到 chrom ...

  3. Android网络传输中必用的两个加密算法:MD5 和 RSA

    MD5和RSA是网络传输中最常用的两个算法,了解这两个算法原理后就能大致知道加密是怎么一回事了.但这两种算法使用环境有差异,刚好互补. 一.MD5算法 首先MD5是不可逆的,只能加密而不能解密.比如明 ...

  4. sql union代替or

    ---原始SQL SQL> SELECT deptno FROM emp WHERE empno = 7788 OR job = 'SALESMAN' ORDER BY 1; DEPTNO -- ...

  5. POJ 2391 容牛问题

    题目大意:给定一个无向图,点i处有Ai头牛,点i处的牛棚能容纳Bi头牛,求一个最短时间T使得在T时间内所有的牛都能进到某一牛棚里去.(1 <= N <= 200, 1 <= M &l ...

  6. HDU 4828 - Grids (Catalan数)

    题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=4828 Catalan数的公式为 C[n+1] = C[n] * (4 * n + 2) / (n ...

  7. 启动android默认浏览器

    一.启动android默认浏览器 Intent intent = new Intent();         intent.setAction("android.intent.action. ...

  8. ERROR<53761> - Plugins - conn=-1 op=-1 msgId=-1 - Connection Bind through PTA failed (91). Retrying...

    LDAP6.3在DSCC控制台启动实例完成,但是操作状态显示“意外错误”,查看日志如下: 04/May/2016:21:10:39 +0800] - Sun-Java(tm)-System-Direc ...

  9. codevs 1027 姓名与ID

    /* 二分图匹配 建图稍麻烦点 不过 有STL大法带我上天 说正经的 先假设都有关系 然后把确定的没有关系的删掉 这样跑出来的一定是完美匹配 至于确定的匹配嘛 删掉这一条 不再是完美匹配 然后记下排序 ...

  10. FastReport 动态修改连接字符串

    代码如下: Report rp = new Report(); rp.Load(@"Print\aa.frx"); rp.Dictionary.Connections[0].Con ...