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( ); //获取日期与时间

posted @ 2014-04-10 12:57  jun_wang  阅读(3580)  评论(0编辑  收藏  举报