• We just launched and are currently in beta. Join us as we build and grow the community.

Simple Calendar in JavaScript

savic7

Wallet Guru
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
148
Likes
13
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, we are going to create Simple Calendar in JavaScript. This simple work creates using JavaScript and HTML. The users enable to navigate the calendar by his/her choice of month. Using a drop down menu to select what they want to display or show in the web page.

Visit the Live Demo, kindly click the button below.

⚙ Live Demo

We are going to construct the Markup for the drop down function to choose the desired month to display in the web page.

In the source code below, shows all the list of a month in the drop down, then one textbox for the year, and a button to execute.

  1. <form

    name

    =

    "calControl"

    onSubmit

    =

    "return false;"

    method

    =

    "post"

    >
  2. <table

    cellpadding

    =

    "0"

    cellspacing

    =

    "0"

    border

    =

    "0"

    >
  3. <tr

    >
  4. <td

    colspan

    =

    "7"

    >
  5. <select

    class

    =

    "month_style"

    name

    =

    "month"

    onChange

    =

    "selectdate()"

    >
  6. <option

    >

    January</

    option

    >
  7. <option

    >

    February</

    option

    >
  8. <option

    >

    March</

    option

    >
  9. <option

    >

    April</

    option

    >
  10. <option

    >

    May</

    option

    >
  11. <option

    >

    June</

    option

    >
  12. <option

    >

    July</

    option

    >
  13. <option

    >

    August</

    option

    >
  14. <option

    >

    September</

    option

    >
  15. <option

    >

    October</

    option

    >
  16. <option

    >

    November</

    option

    >
  17. <option

    >

    December</

    option

    >
  18. </

    select

    >
  19. <input

    type

    =

    "text"

    class

    =

    "year_style"

    name

    =

    "year"

    size

    =

    "4"

    maxlength

    =

    "4"

    >
  20. <input

    type

    =

    "button"

    class

    =

    "build_style"

    name

    =

    "Go"

    value

    =

    "Find . . ."

    onClick

    =

    "selectDate()"

    >
  21. </

    td

    >
  22. </

    tr

    >
  23. </

    form

    >

And, we have to code for the controls in a calendar. It contains a next and previous button. Take a look a simple source code below.

  1. <form

    name

    =

    "calButtons"

    method

    =

    "post"

    >
  2. <tr

    >
  3. <td

    ><textarea

    font=

    "Courier"

    class

    =

    "area_style"

    name

    =

    "calPage"

    WRAP=

    "no"

    rows

    =

    "8"

    cols

    =

    "22"

    disabled></

    textarea

    ></

    td

    >
  4. <tr

    >
  5. <td

    >
  6. <input

    type

    =

    "button"

    class

    =

    "btn_nav"

    name

    =

    "previousYear"

    value

    =

    " << "

    onClick

    =

    "setPreviousYear()"

    >
  7. <input

    type

    =

    "button"

    class

    =

    "btn_nav"

    name

    =

    "previousYear"

    value

    =

    " < "

    onClick

    =

    "setPreviousMonth()"

    >
  8. <input

    type

    =

    "button"

    class

    =

    "btn_nav"

    name

    =

    "previousYear"

    value

    =

    "Today"

    onClick

    =

    "setToday()"

    >
  9. <input

    type

    =

    "button"

    class

    =

    "btn_nav"

    name

    =

    "previousYear"

    value

    =

    " >

    " onClick="setNextMonth()">
  10. <input

    type

    =

    "button"

    class

    =

    "btn_nav"

    name

    =

    "previousYear"

    value

    =

    " >

    > " onClick="setNextYear()">
  11. </

    td

    >
  12. </

    tr

    >
  13. </

    tr

    >
  14. </

    table

    >
  15. </

    form

    >

This is the script where we can set up the Year, Month, Day, and Date Today. Look the script below.

  1. function

    setToday(

    )

    {
  2. var

    now =

    new

    Date

    (

    )

    ;
  3. var

    day =

    now.getDate

    (

    )

    ;
  4. var

    month =

    now.getMonth

    (

    )

    ;
  5. var

    year =

    now.getYear

    (

    )

    ;
  6. if

    (

    year <

    2000

    )
  7. year =

    year +

    1900

    ;
  8. this

    .focusDay

    =

    day;
  9. document.calControl

    .month

    .selectedIndex

    =

    month;
  10. document.calControl

    .year

    .value

    =

    year;
  11. displayCalendar(

    month,

    year)

    ;
  12. }

Use this script to select Month in the web page using a select tag.

  1. function

    selectDate(

    )

    {
  2. var

    year =

    document.calControl

    .year

    .value

    ;
  3. if

    (

    isFourDigitYear(

    year)

    )

    {
  4. var

    day =

    0

    ;
  5. var

    month =

    document.calControl

    .month

    .selectedIndex

    ;
  6. displayCalendar(

    month,

    year)

    ;
  7. }
  8. }

And, this script helps us to navigate the calendar whether you can Next or Previous in the Month or Year.

  1. function

    setPreviousYear(

    )

    {
  2. var

    year =

    document.calControl

    .year

    .value

    ;
  3. if

    (

    isFourDigitYear(

    year)

    )

    {
  4. var

    day =

    0

    ;
  5. var

    month =

    document.calControl

    .month

    .selectedIndex

    ;
  6. year--;
  7. document.calControl

    .year

    .value

    =

    year;
  8. displayCalendar(

    month,

    year)

    ;
  9. }
  10. }

  11. function

    setPreviousMonth(

    )

    {
  12. var

    year =

    document.calControl

    .year

    .value

    ;
  13. if

    (

    isFourDigitYear(

    year)

    )

    {
  14. var

    day =

    0

    ;
  15. var

    month =

    document.calControl

    .month

    .selectedIndex

    ;
  16. if

    (

    month ==

    0

    )

    {
  17. month =

    11

    ;
  18. if

    (

    year >

    1000

    )

    {
  19. year--;
  20. document.calControl

    .year

    .value

    =

    year;
  21. }
  22. }

    else

    {
  23. month--;
  24. }
  25. document.calControl

    .month

    .selectedIndex

    =

    month;
  26. displayCalendar(

    month,

    year)

    ;
  27. }
  28. }

  29. function

    setNextMonth(

    )

    {
  30. var

    year =

    document.calControl

    .year

    .value

    ;
  31. if

    (

    isFourDigitYear(

    year)

    )

    {
  32. var

    day =

    0

    ;
  33. var

    month =

    document.calControl

    .month

    .selectedIndex

    ;
  34. if

    (

    month ==

    11

    )

    {
  35. month =

    0

    ;
  36. year++;
  37. document.calControl

    .year

    .value

    =

    year;
  38. }

    else

    {
  39. month++;
  40. }
  41. document.calControl

    .month

    .selectedIndex

    =

    month;
  42. displayCalendar(

    month,

    year)

    ;
  43. }
  44. }

  45. function

    setNextYear(

    )

    {
  46. var

    year =

    document.calControl

    .year

    .value

    ;
  47. if

    (

    isFourDigitYear(

    year)

    )

    {
  48. var

    day =

    0

    ;
  49. var

    month =

    document.calControl

    .month

    .selectedIndex

    ;
  50. year++;
  51. document.calControl

    .year

    .value

    =

    year;
  52. displayCalendar(

    month,

    year)

    ;
  53. }
  54. }

Output

calendar.jpg


⚙ Live Demo

That's it, you have a calendar after compiling all the source code above. For the full source code, kindly download below. Thank you.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

324,125

324,133

Top