获取今天,昨天,本周,上周,本月,上月时间

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取今天,昨天,本周,上周,本月,上月时间相关的知识,希望对你有一定的参考价值。

  1 //获取今天
  2 var nowDate= new Date(); //当天日期
  3 console.log(nowDate);
  4 //今天是本周的第几天
  5 var nowDayOfWeek= nowDate.getDay();
  6 console.log(nowDayOfWeek);
  7 //当前日
  8 var nowDay = nowDate.getDate();
  9 console.log(nowDay);
 10 //当前月
 11 var nowMonth = nowDate.getMonth();
 12 console.log(nowMonth);
 13 //当前年
 14 var nowYear = nowDate.getFullYear();
 15 console.log(nowYear);
 16 //var nowHours = nowDate.getHours();
 17 //var nowMinutes = nowDate.getMinutes();
 18 //var nowSeconds = nowDate.getSeconds();
 19 
 20 nowYear += (nowYear < 2000) ? 1900 : 0;  //
 21 console.log(nowYear);
 22 
 23 
 24 var lastMonthDate = new Date();  //上月日期
 25 console.log(lastMonthDate);
 26 
 27 lastMonthDate.setDate(1);
 28 console.log(lastMonthDate.setDate(1));
 29 
 30 lastMonthDate.setMonth(lastMonthDate.getMonth()-1);
 31 console.log(lastMonthDate.setMonth(lastMonthDate.getMonth()-1));
 32 
 33 var lastYear = lastMonthDate.getYear();
 34 console.log(lastYear);
 35 
 36 var lastMonth = lastMonthDate.getMonth();
 37 console.log(lastMonth);
 38 
 39 
 40 //格式化日期:yyyy-MM-dd
 41 function formatDate(date) {
 42   var myyear = date.getFullYear();
 43   var mymonth = date.getMonth()+1;
 44   var myweekday = date.getDate();
 45   //var myHours = date.getHours();
 46   //var myMinutes = date.getMinutes();
 47   //var mySeconds = date.getSeconds();
 48 
 49   if(mymonth < 10){
 50     mymonth = "0" + mymonth;
 51   }
 52   if(myweekday < 10){
 53     myweekday = "0" + myweekday;
 54   }
 55   //if(myHours < 10){
 56   //  myHours = "0" + myHours;
 57   //}
 58   //if(myMinutes < 10){
 59   //  myMinutes = "0" + myMinutes;
 60   //}
 61   return (myyear+"/"+mymonth + "/" + myweekday);
 62   //return (myyear+"/"+mymonth + "/" + myweekday + " " + myHours+ ":" + myMinutes);
 63 }
 64 
 65 //获得某月的天数
 66 function getMonthDays(myMonth){
 67   var monthStartDate = new Date(nowYear, myMonth, 1);
 68   var monthEndDate = new Date(nowYear, myMonth + 1, 1);
 69   var   days   =   (monthEndDate   -   monthStartDate)/(1000   *   60   *   60   *   24);
 70   return   days;
 71 }
 72 
 73 ////获得本季度的开始月份
 74 //function getQuarterStartMonth(){
 75 //  var quarterStartMonth = 0;
 76 //  if(nowMonth<3){
 77 //    quarterStartMonth = 0;
 78 //  }
 79 //  if(2<6){
 80 //    quarterStartMonth = 3;
 81 //  }
 82 //  if(5<9){
 83 //    quarterStartMonth = 6;
 84 //  }
 85 //  if(nowMonth>8){
 86 //    quarterStartMonth = 9;
 87 //  }
 88 //  return quarterStartMonth;
 89 //}
 90 
 91 
 92 //今天
 93 $scope.toDay = function(){
 94   var getCurrentDate = new Date();
 95   var getCurrentDate  = formatDate(getCurrentDate);
 96   $scope.today = getCurrentDate;
 97   console.log($scope.today);
 98   $("#jqueryPickerTime3").val($scope.today);
 99   $("#jqueryPickerTime4").val($scope.today);
100 };
101 
102 //昨天
103 $scope.yesTerDay = function(){
104   var getYesterdayDate = new Date(nowYear, nowMonth, nowDay - 1);
105   var getYesterdayDate =  formatDate(getYesterdayDate);
106 
107   $scope.yesTday = getYesterdayDate;
108   console.log(getYesterdayDate);
109   $("#jqueryPickerTime3").val($scope.yesTday);
110   $("#jqueryPickerTime4").val($scope.yesTday);
111 };
112 
113 
114 //获得本周的开始日期
115 $scope.thisWeek = function(){
116   var getWeekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
117   var getWeekStartDate =  formatDate(getWeekStartDate);
118   $scope.tswkStart = getWeekStartDate;
119   console.log($scope.tswkStart);
120   $("#jqueryPickerTime3").val($scope.tswkStart);
121   //获得本周的结束日期
122   var getWeekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
123   var getWeekEndDate =  formatDate(getWeekEndDate);
124   $scope.tswkEnd = getWeekEndDate;
125   console.log($scope.tswkEnd);
126   $("#jqueryPickerTime4").val($scope.tswkEnd);
127 };
128 
129 $scope.lastWeek = function(){
130   //获得上周的开始日期
131   var getUpWeekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek -7);
132   var getUpWeekStartDate =  formatDate(getUpWeekStartDate);
133   $scope.startLastWeek = getUpWeekStartDate;
134   console.log($scope.startLastWeek);
135   $("#jqueryPickerTime3").val($scope.startLastWeek);
136   //获得上周的结束日期
137   var getUpWeekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek - 7));
138   var getUpWeekEndDate =  formatDate(getUpWeekEndDate);
139   $scope.endLastWeek = getUpWeekEndDate;
140   console.log($scope.endLastWeek);
141   $("#jqueryPickerTime4").val($scope.endLastWeek);
142 
143 
144 };
145 //本月
146 $scope.thisMonth  = function(){
147   //获得本月的开始日期
148   var getMonthStartDate = new Date(nowYear, nowMonth, 1);
149   var getMonthStartDate =  formatDate(getMonthStartDate);
150   $scope.startThisMonth = getMonthStartDate;
151   console.log($scope.startThisMonth);
152   $("#jqueryPickerTime3").val($scope.startThisMonth);
153 
154   //获得本月的结束日期
155   var getMonthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
156   var getMonthEndDate =  formatDate(getMonthEndDate);
157   $scope.endThisMonth = getMonthEndDate;
158   console.log($scope.endThisMonth);
159   $("#jqueryPickerTime4").val($scope.endThisMonth);
160 };
161 //上月
162 $scope.lastMonth = function(){
163   //获得上月开始时间
164   var getLastMonthStartDate = new Date(nowYear, lastMonth+1, 1);
165   var getLastMonthStartDate = formatDate(getLastMonthStartDate);
166 
167   $scope.startLastMonth = getLastMonthStartDate;
168   console.log($scope.startLastMonth);
169 
170   $("#jqueryPickerTime3").val($scope.startLastMonth);
171   //获得上月结束时间
172   var getLastMonthEndDate = new Date(nowYear, lastMonth+1, getMonthDays(lastMonth+1));
173   var getLastMonthEndDate = formatDate(getLastMonthEndDate);
174 
175   $scope.endLastMonth = getLastMonthEndDate;
176   console.log($scope.endLastMonth);
177   $("#jqueryPickerTime4").val($scope.endThisMonth);
178 };

 

以上是关于获取今天,昨天,本周,上周,本月,上月时间的主要内容,如果未能解决你的问题,请参考以下文章

sql语句获取今天昨天近7天本周上周本月上月半年数据

Oracle查询今天昨天本周上周本月上月数据

Sql Server中查询今天昨天本周上周本月上月数据

php一行代码获取本周一,本周日,上周一,上周日,本月一日,本月最后一日,上月一日,上月最后一日日期

php一行代码获取本周一,本周日,上周一,上周日,本月一日,本月最后一日,上月一日,上月最后一日日期 转

用php获取本周,上周,本月,上月,本季度日期的代码