Javascript to add or subtract dates by month

Source: Internet
Author: User

Javascript to add or subtract dates by month

JavaScript code example for date addition and subtraction. Because JavaScript does not have the AddDays method similar to C #, you need to write a function to implement date addition and subtraction. I would like to share it with you. If you need it, you can refer

 

 

You need to use it in the project and write it yourself. Javascript date plus or minus by month

?

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

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

<! DOCTYPE html>

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Meta charset = "UTF-8">

<Title> </title>

<Script>

Function dateToDate (date ){

Var sDate = new Date ();

If (typeof date = 'object'

& Typeof new Date (). getMonth = "function"

){

SDate = date;

}

Else if (typeof date = "string "){

Var arr = date. split ('-')

If (arr. length = 3 ){

SDate = new Date (arr [0] + '-' + arr [1] + '-' + arr [2]);

}

}

 

Return sDate;

}

 

 

Function addMonth (date, num ){

Num = parseInt (num );

Var sDate = dateToDate (date );

 

Var sYear = sDate. getFullYear ();

Var sMonth = sDate. getMonth () + 1;

Var sDay = sDate. getDate ();

 

Var eYear = sYear;

Var eMonth = sMonth + num;

Var eDay = sDay;

While (eMonth> 12 ){

EYear ++;

EMonth-= 12;

}

 

Var eDate = new Date (eYear, eMonth-1, eDay );

 

While (eDate. getMonth ()! = EMonth-1 ){

EDay --;

EDate = new Date (eYear, eMonth-1, eDay );

}

 

Return eDate;

}

 

Function calcDate (){

Var d = document. getElementById ('date'). value;

Var n = document. getElementById ('num'). value;

Var eDate = addMonth (d, n );

Document. getElementById ('result '). innerHTML = eDate. getFullYear () + '-' + (eDate. getMonth () + 1) + '-' + eDate. getDate ();

}

</Script>

</Head>

<Body>

<Input type = "date" id = "date"/>

<Input type = "number" id = "num" value = "1"/>

<Input type = "button" value = "Calculate" onclick = "calcDate ()"/>

<Div id = "result"> </div>

</Body>

</Html>

Method 2:

?

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

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

<Script language = "javascript">

Date. prototype. Format = function (fmt)

{

// Code Author: meizz

Var o =

{

"M +": this. getMonth () + 1, // month

"D +": this. getDate (), // day

"H +": this. getHours (), // hour

"M +": this. getMinutes (), // minute

"S +": this. getSeconds (), // second

"Q +": Math. floor (this. getMonth () + 3)/3), // quarter

"S": this. getMilliseconds () // millisecond

};

If (/(y +)/. test (fmt ))

Fmt = fmt. replace (RegExp. $1, (this. getFullYear () + ""). substr (4-RegExp. $1. length ));

For (var k in o)

If (new RegExp ("(" + k + ")"). test (fmt ))

Fmt = fmt. replace (RegExp. $1, (RegExp. $1. length = 1 )? (O [k]): ("00" + o [k]). substr ("" + o [k]). length )));

Return fmt;

}

Date. prototype. addDays = function (d)

{

This. setDate (this. getDate () + d );

};

Date. prototype. addWeeks = function (w)

{

This. addDays (w * 7 );

};

Date. prototype. addMonths = function (m)

{

Var d = this. getDate ();

This. setMonth (this. getMonth () + m );

If (this. getDate () <d)

This. setDate (0 );

};

Date. prototype. addYears = function (y)

{

Var m = this. getMonth ();

This. setFullYear (this. getFullYear () + y );

If (m <this. getMonth ())

{

This. setDate (0 );

}

};

</Script>

Method 3:

?

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

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

<Script>

Function DateAdd (interval, number, date)

{

/*

* --------------- DateAdd (interval, number, date )-----------------

* DateAdd (interval, number, date)

* Function: implements the DateAdd function of VBScript.

* Parameter: interval, a string expression, indicating the time interval to be added.

* Parameter: number, numeric expression, indicating the number of time intervals to be added.

* Parameter: date, time object.

* Return: a new time object.

* Var now = new Date ();

* Var newDate = DateAdd ("d", 5, now );

* Author: wanghr100 (Baby gray bean. net)

* Update:

* --------------- DateAdd (interval, number, date )-----------------

*/

Switch (interval)

{

Case "y ":{

Date. setFullYear (date. getFullYear () + number );

Return date;

Break;

}

Case "q ":{

Date. setMonth (date. getMonth () + number * 3 );

Return date;

Break;

}

Case "m ":{

Date. setMonth (date. getMonth () + number );

Return date;

Break;

}

Case "w ":{

Date. setDate (date. getDate () + number * 7 );

Return date;

Break;

}

Case "d ":{

Date. setDate (date. getDate () + number );

Return date;

Break;

}

Case "h ":{

Date. setHours (date. getHours () + number );

Return date;

Break;

}

Case "m ":{

Date. setMinutes (date. getMinutes () + number );

Return date;

Break;

}

Case "s ":{

Date. setSeconds (date. getSeconds () + number );

Return date;

Break;

}

Default :{

Date. setDate (d. getDate () + number );

Return date;

Break;

}

}

}

Var now = new Date ();

// Add five days.

Var newDate = DateAdd ("d", 5, now );

Alert (newDate. toLocaleDateString ())

// Add two months.

NewDate = DateAdd ("m", 2, now );

Alert (newDate. toLocaleDateString ())

// Add one year

NewDate = DateAdd ("y", 1, now );

Alert (newDate. toLocaleDateString ())

</Script>

The above is all the content of this article. I hope you will like it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.