Js 獲取 本周、本月起始時間


涉及到顯示本月或本周相關信息,又不想讓php去判斷,只好直接用js去計算,麻煩了好一陣,還是老老實實的看了下js的日期函數。現總結一下:

 

//計算本周起始日期,並以 Y-m-d 形式返回。
    function getWeekTime()
    {
      var now = new Date();      
      var Year = now.getFullYear();
      var Month = now.getMonth() + 1;
      var Day = now.getDate()- now.getDay();
      if(now.getDay()==0)           //星期天表示 0 故當星期天的時候,獲取上周開始的時候
      {
          Day -= 7;
      }
      var beginTime = Year + "-" + Month +"-" + Day;        //格式 Y-m-d
      return beginTime;
    }

 

//計算本月開始時間,並以Y-m-d 形式返回
    function getMonthTime(){
      var now = new Date();             //獲取當前時間
      var beginTimes = now.getFullYear();     //開始計算
      var Month = now.getMonth() +1 ;           //getMonth()是以0開始的月份
      var beginTimes = beginTimes + "-" +Month +"-1";        //格式 Y-m-d
      return beginTimes;
    }

 

js日期函數總結:

var mydate = new Date();
mydate.getYear(); //獲取當前年份(2位)
mydate.getFullYear(); //獲取完整的年份(4位,1970-????)
mydate.getMonth(); //獲取當前月份(0-11,0代表1月)
mydate.getDate(); //獲取當前日(1-31)
mydate.getDay(); //獲取當前星期X(0-6,0代表星期天)
mydate.getTime(); //獲取當前時間(從1970.1.1開始的毫秒數)
mydate.getHours(); //獲取當前小時數(0-23)
mydate.getMinutes(); //獲取當前分鍾數(0-59)
mydate.getSeconds(); //獲取當前秒數(0-59)
mydate.getMilliseconds(); //獲取當前毫秒數(0-999)
mydate.toLocaleDateString(); //獲取當前日期
var mytime=mydate.toLocaleTimeString(); //獲取當前時間
mydate.toLocaleString( ); //獲取日期與時間


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM