js 获取今天以及前一周/前20天时间

js 获取今天以及前一周/前20天时间

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

var end = new Date();

var start = new Date();

console.log('==========今天=============')

console.log(start);

console.log(end);

console.log('==========今天=============')

console.log(start.getFullYear());

console.log(start.getMonth());

console.log(start.getDate());

console.log('==========20天前=============')

start.setDate(start.getDate() - 20);   //day和month会自动计算到上一个月的

console.log(start.getFullYear());

console.log(start.getMonth());

console.log(start.getDate());

var startYear = start.getFullYear();

var startMonth = start.getMonth() + 1 >= 10 ? start.getMonth() + 1 : "0" + (start.getMonth() + 1);

var startDate = start.getDate() >= 10 ? start.getDate() : "0" + start.getDate();

var endYear = end.getFullYear();

var endtMonth = end.getMonth() + 1 >= 10 ? end.getMonth() + 1 : "0" + (end.getMonth() + 1);

var endDate = end.getDate() >= 10 ? end.getDate() : "0" + end.getDate();

startDate1 = startYear + "-" + startMonth + "-" + startDate;

endDate1 = endYear + "-" + endtMonth + "-" + endDate;

console.log(startDate1);

console.log(endDate1)

  console输出:

转载自:Andrew_F

猜你喜欢

转载自blog.csdn.net/admin123404/article/details/107617236