欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JS获取时间代码展示

程序员文章站 2022-06-10 16:36:50
js获取时间 var getdate = new date(); //获取当前时间27 27号 //console.log(getdate.getdate())...

js获取时间

 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!=0getweek-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;i2. 获取本月的天数
   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)
;i++){>