• 日期的获取


    获取本周的起止日期

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <script>
            window.onload = function(){
             
                var a = document.getElementById('a');
                var b = document.getElementById('b');
                //var clen = cells.length;
                var currentFirstDate;
                var formatDate = function(date){
                 
                    var year = date.getFullYear()+'-';
                    var month = (date.getMonth()+1);
                    var day = date.getDate();
                     if(month<10)
                     {
                        month='0'+month+'-';
                     }else{
                        month=month+'-'
                     }
    
                    if(day<10)
                     {
                        day='0'+day;
                     }
    
                    return year+month+day+' ';
                };
                var addDate= function(date,n){       
                    date.setDate(date.getDate()+n);        
                    return date;
                };
                var setDate = function(date){
                 
                    var week = date.getDay()-1;
                    date = addDate(date,week*-1);
                    currentFirstDate = new Date(date);
                    a.value=formatDate( addDate(date,0));
                    b.value=formatDate( addDate(date,6));       
                };
                 
                setDate(new Date());
            }
        </script>
    </head>
    <body>
    <input type="text" id="a" value="">
    <input type="text" id="b" value="">
    </body>
    </html>
    demo1

    获取上一周下一周的日期

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style>
         
        td{
         
            border:1px solid;
        }
         
        button{
         
            margin: 5px;
        }
             
        </style>
        <script>
            window.onload = function(){
             
                var cells = document.getElementById('monitor').getElementsByTagName('td');
                var clen = cells.length;
                var currentFirstDate;
                var formatDate = function(date){
                 
                    var year = date.getFullYear()+'';
                    var month = (date.getMonth()+1)+'';
                    var day = date.getDate()+'';
                    var week = '('+['星期天','星期一','星期二','星期三','星期四','星期五','星期六'][date.getDay()]+')';
                     
                    return year+month+day+' '+week;
                };
                var addDate= function(date,n){       
                    date.setDate(date.getDate()+n);        
                    return date;
                };
                var setDate = function(date){
                 
                    var week = date.getDay()-1;
                    date = addDate(date,week*-1);
                    currentFirstDate = new Date(date);
                     
                    for(var i = 0;i<clen;i++){
                     
                        cells[i].innerHTML = formatDate(i==0 ? date : addDate(date,1));
                    }
                     
                };
                 
                document.getElementById('last-week').onclick = function(){
                     
                    setDate(addDate(currentFirstDate,-7));         
                };
                 
                document.getElementById('next-week').onclick = function(){
                     
                    setDate(addDate(currentFirstDate,7));
                };
                 
                setDate(new Date());
            }
        </script>
    </head>
    <body>
        <button id="last-week">上一周</button><button id="next-week">下一周</button>
        <table id="monitor">
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </table>
    </body>
    </html>
    demo2
  • 相关阅读:
    正则表达式教程
    LINQ查询基本操作
    C# LINQ 基本操作实例
    Sublime Text 3 常用插件以及安装方法
    记一次矩阵列单元格合并和拆分组件的开发
    解析JavaScript函数的多种写法
    jQuery源码逐行分析学习02(第一部分:jQuery的一些变量和函数)
    jQuery源码逐行分析学习01(jQuery的框架结构简化)
    Vue IIS 403-Forbidden
    数组求和:二分递归
  • 原文地址:https://www.cnblogs.com/Secretmm/p/5676832.html
Copyright © 2020-2023  润新知