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

How to Get 7 Days in a Week With Date using JavaScript

milesp

Digital Search Specialist
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
116
Likes
191
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
How to Get 7 Days in a Week With Date using JavaScript

Introduction

In this tutorial we will create a How to Get 7 Days in a Week With Date using JavaScript. This tutorial purpose is to help how to display 7 days in a week data. This will cover all the basic functions that provide the calculating the date input. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any kind of system that need to display the days per week. I will give my best to provide you the easiest way of creating this program 7 days in a week with date. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will display a form date selector and a button. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html

    >
  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"

    >

    How to Get 7 Days in a Week With Date using JavaScript</

    h3

    >
  16. <hr

    style

    =

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

    /

    >
  17. <div

    class

    =

    "col-md-3"

    >
  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. <button

    type

    =

    "button"

    onclick

    =

    "GetWeek();"

    class

    =

    "btn btn-primary btn-block"

    ><span

    class

    =

    "glyphicon glyphicon-save"

    ></

    span

    >

    Submit</

    button

    >
  24. </

    div

    >

  25. </

    form

    >
  26. </

    div

    >
  27. <div

    class

    =

    "col-md-9"

    >
  28. <div

    id

    =

    "result"

    ></

    div

    >
  29. </

    div

    >
  30. </

    div

    >
  31. </

    body

    >

  32. <script

    src

    =

    "script.js"

    ></

    script

    >
  33. </

    html

    >

Creating JavaScript Function

This is where the main function of the application is. This code will let you dynamically add notes and display it. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function

    CalculateDayOfWeek(

    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

    GetWeek(

    )

    {
  9. var

    date =

    document.getElementById

    (

    'date'

    )

    ;
  10. if

    (

    date.value

    ==

    ""

    )

    {
  11. alert(

    "Please enter a date 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

    dates =

    new

    Array

    (

    )

    ;
  17. dates =

    CalculateDayOfWeek(

    newDate)

    ;
  18. var

    data =

    "<h2>7 days of a week data</h2>"

    ;
  19. for

    (

    var

    i=

    0

    ;

    i <

    dates.length

    ;

    i++

    )

    {
  20. data +=

    "<p class='text-danger'>"

    +

    days[

    dates[

    i]

    .getDay

    (

    )

    ]

    +

    ": "

    +

    months[

    dates[

    i]

    .getMonth

    (

    )

    ]

    +

    " "

    +

    dates[

    i]

    .getDate

    (

    )

    +

    ", "

    +

    dates[

    i]

    .getFullYear

    (

    )

    +

    "</p>"

    ;
  21. }

  22. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    data;
  23. }
  24. }

In the first line of code we will create a method called CalculateDayOfWeek(). This method will only return the date value by appending each date indexes within a 7 length. Next we will create another method called GetWeek(), this method purpose is to fetch the date indexes and append the date string value. In order to display the 7 days, we must use the for loop function to display the indexes with the date value accordingly into your html element.

Output:

How%20to%20Get%207%20Days%20in%20a%20Week%20With%20Date%20using%20JavaScript%201.png


The How to Get 7 Days in a Week With Date using JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Get 7 Days in a Week With 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!

More Tutorials for JavaScript Language

JavaScript Tutorials


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

450,695

323,213

323,222

Top