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

JavaScript - Display Week Days From A Given Date

Abood000

DevOps Pipeline Architect
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
78
Likes
101
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial we will create a Display Week Days From A Given Datet using JavaScript. This code will generate a list of days in a week when user provide a input date. The code use onclick() to call a function that display a list of days using for loop to rearrange the index position of arrays to form a group of weekdays. This a free program feel free to modify it and use it in your system.

JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented.

Getting started:

This is the link for the bootstrap that has been used for the layout of the calculator https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1"

    /

    >
  5. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "css/bootstrap.css"

    /

    >
  6. </

    head

    >
  7. <body

    >
  8. <nav class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav>
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    JavaScript - Display Week Days From A Given Date</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-4"

    >
  18. <form

    >
  19. <div

    class

    =

    "form-group"

    >
  20. <label

    >

    Enter A Date</

    label

    >
  21. <input

    type

    =

    "date"

    id

    =

    "date"

    class

    =

    "form-control"

    /

    >
  22. <br

    /

    >
  23. <center

    ><button

    type

    =

    "button"

    onclick

    =

    "getWeekDays();"

    class

    =

    "btn btn-primary"

    ><span

    class

    =

    "glyphicon glyphicon-save"

    ></

    span

    >

    Submit</

    button

    ></

    center

    >
  24. </

    div

    >
  25. </

    form

    >
  26. </

    div

    >
  27. <div

    class

    =

    "col-md-8"

    >
  28. <div

    id

    =

    "result"

    ></

    div

    >
  29. </

    div

    >
  30. </

    div

    >
  31. </

    body

    >
  32. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  33. </

    html

    >

Creating the Main Function

This code contains the main function of the application. This code will display a list of days in a week when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor then save it as script.js inside the js folder.
  1. function

    dayInAWeek(

    date)

    {
  2. var

    days =

    new

    Array

    (

    )

    ;
  3. for

    (

    var

    i =

    0

    ;

    i <

    7

    ;

    i++

    )

    {
  4. days[

    i]

    =

    new

    Date

    (

    date.getFullYear

    (

    )

    ,

    date.getMonth

    (

    )

    ,

    date.getDate

    (

    )

    -

    date.getDay

    (

    )

    +

    1

    +

    i)

    ;
  5. }

  6. return

    days;
  7. }

  8. function

    getWeekDays(

    )

    {
  9. var

    date =

    document.getElementById

    (

    'date'

    )

    ;
  10. if

    (

    date.value

    ==

    ""

    )

    {
  11. alert(

    "Please enter a something first!"

    )

    ;
  12. }

    else

    {
  13. var

    months=

    [

    "January"

    ,

    "February"

    ,

    "March"

    ,

    "April"

    ,

    "May"

    ,

    "June"

    ,

    "July"

    ,

    "August"

    ,

    "September"

    ,

    "October"

    ,

    "November"

    ,

    "December"

    ]

    ;
  14. var

    days =

    [

    'Sunday'

    ,

    'Monday'

    ,

    'Tuesday'

    ,

    'Wednesday'

    ,

    'Thursday'

    ,

    'Friday'

    ,

    'Saturday'

    ]

    ;
  15. var

    newDate =

    new

    Date

    (

    date.value

    )

    ;
  16. var

    date_array =

    new

    Array

    (

    )

    ;
  17. date_array =

    dayInAWeek(

    newDate)

    ;
  18. var

    data =

    "<h2>Week Days</h2>"

    ;
  19. for

    (

    var

    i=

    0

    ;

    i <

    date_array.length

    ;

    i++

    )

    {
  20. data +=

    "<ul><li>"

    +

    days[

    date_array[

    i]

    .getDay

    (

    )

    ]

    +

    ": "

    +

    months[

    date_array[

    i]

    .getMonth

    (

    )

    ]

    +

    " "

    +

    date_array[

    i]

    .getDate

    (

    )

    +

    ", "

    +

    date_array[

    i]

    .getFullYear

    (

    )

    +

    "</li></ul>"

    ;
  21. }

  22. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    data;
  23. }
  24. }

There you have it we successfully created a Display Week Days From A Given Date using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!


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

451,993

323,299

323,308

Top