获取今天,昨天,本周,上周,本月,上月时间
生活随笔
收集整理的這篇文章主要介紹了
获取今天,昨天,本周,上周,本月,上月时间
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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 };
?
轉載于:https://www.cnblogs.com/yaomin/p/6253146.html
總結
以上是生活随笔為你收集整理的获取今天,昨天,本周,上周,本月,上月时间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 地方坐标系
- 下一篇: 01【在线日志分析】之Flume-1.7