JS 获取 今天 本周 本月 最近三十天

var now = new Date();
var year = now.getFullYear(); //getFullYear getYear
var month = now.getMonth()+1;
var date = now.getDate();
//今天
var strToadoy=year+'-'+month+'-'+date; 
//或者 
var strToadoy=new Date(new Date().getTime());
    
 
//将datetime的UTC表示形式转换为:"yyyy-MM-dd字符串表示形式 
    function dateFormat(DateIn) {
         var Year = 0;
         var Month = 0;
         var Day = 0;
         var CurrentDate = "";
         //初始化时间
         Year = DateIn.getYear();
         Month = DateIn.getMonth()+1;
         Day = DateIn.getDate();
         Year = (Year < 1900 ? (1900 + Year) : Year);
         CurrentDate = Year + "-";
         if (Month >= 10) {
             CurrentDate = CurrentDate + Month + "-";
         }
         else {
             CurrentDate = CurrentDate + "0" + Month + "-";
         }
         if (Day >= 10) {
             CurrentDate = CurrentDate + Day;
         }
         else {
             CurrentDate = CurrentDate + "0" + Day;
         }
         return CurrentDate;
    }

//七天前
 
  
var dateStart = new Date((new Date().getTime()-1000*60*60*24*6));  //6     
           dateStart=dateFormat(dateStart);
 
//三十天前
var dateStart = new Date((new Date().getTime()-1000*60*60*24*29));  //29     
           dateStart=dateFormat(dateStart); 
 
//本月
var current=new Date();
var currentMonth=current.getMonth()+1;

var monthFirstDay=current.getFullYear()+"-";
if(currentMonth>9){
	monthFirstDay=monthFirstDay+nextMonth+"-01";
}else{
        monthFirstDay=monthFirstDay+"0"+nextMonth+"-01";
}

var nextMonthDayOne =new Date(current.getFullYear(),currentMonth,1);
var minusDate=1000*60*60*24;
          
var myYear=new Date(nextMonthDayOne.getTime()-minusDate).getFullYear();
var monthLastDay=myYear+"-";
var myMonth=new Date(nextMonthDayOne.getTime()-minusDate).getMonth()+1;
var myDay=new Date(nextMonthDayOne.getTime()-minusDate).getDate();
if(myMonth>9){
	monthLastDay=monthLastDay+myMonth+"-"+myDay;
}else{
        monthLastDay=monthLastDay+"0"+myMonth+"-"+myDay;
}

 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
 

你可能感兴趣的:(javascript)