JS获取时间

1. 根据当前时间获取本周的起止日期

 var  getdate = new Date();
    //获取当前时间27     27号
    //console.log(getdate.getDate())

    //本周起始数组
    var weekArr= new Array();

    var getweek = getdate.getDay();
    // 是一周中的某一天  4    周四
    //console.log(getweek)

    var second = 1000*60*60*24;
    //每天的毫秒数 86400000
    //console.log(second)

    var minuday =getweek!=0?getweek-1:6
    //减去的天数   3
   //console.log(minuday)

    var monday = new Date(getdate.getTime()-(minuday*second));
    //本周周一
    //console.log(monday)      Mon Sep 24 2018 11:27:24 GMT+0800 (中国标准时间)
    var Tuesday = new Date(getdate.getTime()-(2*second));
    //console.log(Tuesday);
    var sunday=new Date(monday.getTime()+(6*second));
    //本周周日
    //console.log(sunday)       Sun Sep 30 2018 11:27:24 GMT+0800 (中国标准时间)
    weekArr.push(monday);
    weekArr.push(sunday);
    //本周期起止日期
    console.log(weekArr)
//

     switch(getweek)
     {
         case 7:
             var str =["周一","周二","周三","周四","周五","周六","周日"]
             console.log(str)
             break;

     }

  var str =[];
    for(var i=getweek;i>=1;i--){
        str.push(i)

   }
   console.log(str.reverse())


  var str =[];
    for(var i=1;i

2. 获取本月的天数

   var relativeDate=new Date(1);
            //获得当前月份0-11
            var relativeMonth=relativeDate.getMonth();
            //获得当前年份4位年
            var relativeYear=relativeDate.getFullYear();

            //当为12月的时候年份需要加1
            //月份需要更新为0 也就是下一年的第一个月
            if(relativeMonth==11){
                relativeYear++;
                relativeMonth=0;
            }else{
                //否则只是月份增加,以便求的下一月的第一天
                relativeMonth++;
            }
            //一天的毫秒数
            var millisecond=1000*60*60*24;
            //下月的第一天
            var nextMonthDayOne=new Date(relativeYear,relativeMonth,1);
            //返回得到上月的最后一天,也就是本月总天数
            console.log(new Date(nextMonthDayOne.getTime()-millisecond).getDate())

3. 获取本季度的开始月份

  var now = new Date(); //当前日期
        var nowMonth = now.getMonth(); //当前月
        //获得本季度的开始月份

            var quarterStartMonth = 0;
            if (nowMonth < 3) {
                quarterStartMonth = 0;
            }
            if (2 < nowMonth && nowMonth < 6) {
                quarterStartMonth = 3;
            }
            if (5 < nowMonth && nowMonth < 9) {
                quarterStartMonth = 6;
            }
            if (nowMonth > 8) {
                quarterStartMonth = 9;
            }

        console.log(quarterStartMonth)

4. 获取本季度分别有哪几个月份

   var now = new Date(); //当前日期
        var nowMonth = now.getMonth()+1; //当前月  获取月份+1为当前月
        //获得本季度的分别是哪几个月份
        var arr =[];
            var quarterStartMonth = 0;
            if (nowMonth <= 3) {
                arr=[1,2,3]
            }
            if (3 < nowMonth && nowMonth <= 6) {
                arr=[4,5,6]
            }
            if (6 < nowMonth && nowMonth <= 9) {
                arr=[7,8,9]
            }
            if (nowMonth > 9) {
                arr=[10,11,12]
            }
        console.log(arr)

5. 获取当前本年的月份

     var now = new Date(); //当前日期
        var nowMonth = now.getMonth()+1; //当前月  获取月份+1为当前月
        //获得当前半年的月份
        var arr =[];

        for(var i = 1;i<=nowMonth;i++){
            arr.push(i)

        }
             console.log(arr)

6. 获取当前半年内的月份

 var now = new Date(); //当前日期
        var nowMonth = now.getMonth()+1; //当前月  获取月份+1为当前月
        nowMonth=3
        //获得当前半年的月份
        var arr =[];
        var monthNum=nowMonth-5;
        if(monthNum<1){
            monthNum=1;
        }
        for(var i=monthNum;i<=nowMonth;i++){
            arr.push(i)
        }
         console.log(arr)

 

你可能感兴趣的:(JS获取时间)