zoukankan      html  css  js  c++  java
  • js 获取昨天日期

    想要做个记录 但是发现有位老哥的博客写的比较全  拿过来记录一下  

    https://www.cnblogs.com/sxxjyj/p/6093326.html

    1. 开发过程中某些前台页面的时间控件我们需要给默认当前时间,jquery可以轻松的帮我们实现,代码如下
      复制代码
       1 //昨天的时间
       2 var day1 = new Date();
       3 day1.setTime(day1.getTime()-24*60*60*1000);
       4 var s1 = day1.getFullYear()+"-" + (day1.getMonth()+1) + "-" + day1.getDate();
       5 //今天的时间
       6 var day2 = new Date();
       7 day2.setTime(day2.getTime());
       8 var s2 = day2.getFullYear()+"-" + (day2.getMonth()+1) + "-" + day2.getDate();
       9 //明天的时间
      10 var day3 = new Date();
      11 day3.setTime(day3.getTime()+24*60*60*1000);
      12 var s3 = day3.getFullYear()+"-" + (day3.getMonth()+1) + "-" + day3.getDate();
      13 //拼接时间
      14 function show(){
      15      var str = "" + s1 + "至" + s2;
      16      return str;
      17 }
      18 //赋值doubleDate
      19  $('#dateS').val(show());        
      复制代码
    2. 下面是具体到时分秒的获取方法
      复制代码
       1 function writeCurrentDate() {
       2         var now = new Date();
       3         var year = now.getFullYear(); //得到年份
       4         var month = now.getMonth();//得到月份
       5         var date = now.getDate();//得到日期
       6         var day = now.getDay();//得到周几
       7         var hour = now.getHours();//得到小时
       8         var minu = now.getMinutes();//得到分钟
       9         var sec = now.getSeconds();//得到秒
      10        var MS = now.getMilliseconds();//获取毫秒
      11         var week;
      12         month = month + 1;
      13         if (month < 10) month = "0" + month;
      14         if (date < 10) date = "0" + date;
      15         if (hour < 10) hour = "0" + hour;
      16         if (minu < 10) minu = "0" + minu;
      17         if (sec < 10) sec = "0" + sec;
      18         if (MS < 100)MS = "0" + MS;
      19         var arr_week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
      20         week = arr_week[day];
      21         var time = "";
      22         time = year + "年" + month + "月" + date + "日" + " " + hour + ":" + minu + ":" + sec + " " + week;
      23         //当前日期赋值给当前日期输入框中(jQuery easyUI)
      24         $("#currentDate").html(time);
      25         //设置得到当前日期的函数的执行间隔时间,每1000毫秒刷新一次。
      26         var timer = setTimeout("writeCurrentDate()", 1000);
      27       }
      复制代码
    3. 2017.6.27更新
      今天又发现了一种简单的方法:可以直接对年月日时分秒进行操作,假如今天2017-06-01 那么所得昨天为 2017-05-31
      复制代码
      1  //昨天的时间 
      2  var day1 = new Date();
      3  day1.setDate(day1.getDate() - 1);
      4  var s1 = day1.format("yyyy-MM-dd");
      5  //前天的时间
      6  var day2 = new Date();
      7  day2.setDate(day2.getDate() - 2);
      8  var s2 = day2.format("yyyy-MM-dd");
      复制代码


      其中,format函数为扩展函数。

      复制代码
      /**
       *对Date的扩展,将 Date 转化为指定格式的String
       *月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
       *年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
       *例子:
       *(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
       *(new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
       */
      Date.prototype.format = function (fmt) {
          var o = {
              "M+": this.getMonth() + 1, //月份
              "d+": this.getDate(), //日
              "h+": this.getHours(), //小时
              "m+": this.getMinutes(), //分
              "s+": this.getSeconds(), //秒
              "q+": Math.floor((this.getMonth() + 3) / 3), //季度
              "S": this.getMilliseconds() //毫秒
          };
          if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
          for (var k in o)
              if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
          return fmt;
      }
      复制代码
    4. 页面日期 2017-06-27 变 20170627      “2017-06-27”.replace(/-/g,"")。
  • 相关阅读:
    wget(转)
    852. Peak Index in a Mountain Array
    617. Merge Two Binary Trees
    814. Binary Tree Pruning
    657. Judge Route Circle
    861. Score After Flipping Matrix
    832. Flipping an Image
    461. Hamming Distance
    654. Maximum Binary Tree
    804. Unique Morse Code Words
  • 原文地址:https://www.cnblogs.com/Mr-Y1907/p/14023552.html
Copyright © 2011-2022 走看看