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

Date Formatting with Date Picker using JavaScript

Zinex

Revenue Growth Analyst
Divine
Z Rep
0
0
0
Rep
0
Z Vouches
0
0
0
Vouches
0
Posts
158
Likes
194
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this article, we are going to show you Date Formatting with Date Picker using JavaScript. Using jQuery date picker we can select our desired date then, we are going to set the format of the selected date using dateFormat option in the select field.

Date Format
  • mm/dd/yy
  • dd/mm/yy
  • yy-mm-dd
  • M d, y

Creating Markup for Date Picker with Date Formats

This HTML source code shows the date picker where the user can select desired date and a list of date format.

  1. <div

    id

    =

    "date_Format_Style"

    >
  2. <h2

    >
  3. Date Formatting with Date Picker using JavaScript
  4. </

    h2

    >
  5. <div

    class

    =

    "input-style"

    >
  6. <div

    class

    =

    "frm-label"

    >

    Select Date:</

    div

    >
  7. <div

    ><input

    type

    =

    "text"

    id

    =

    "date_picker"

    size

    =

    "30"

    class

    =

    "form-input-style"

    ></

    div

    >
  8. </

    div

    >
  9. <div

    class

    =

    "input-style"

    >
  10. <div

    >

    Formats:</

    div

    >
  11. <div

    >
  12. <select

    id

    =

    "date-format"

    onchange

    =

    "change_DateFormat(this.value);"

    class

    =

    "form-input-style"

    >
  13. <option

    value

    =

    "mm/dd/yy"

    >

    mm/dd/yy</

    option

    >
  14. <option

    value

    =

    "dd/mm/yy"

    >

    dd/mm/yy</

    option

    >
  15. <option

    value

    =

    "yy-mm-dd"

    >

    yy-mm-dd</

    option

    >
  16. <option

    value

    =

    "M d, y"

    >

    M d, y</

    option

    >
  17. </

    select

    >
  18. </

    div

    >
  19. </

    div

    >
  20. </

    div

    >

This is the output of the source code above.

1_12.jpg

CSS, External Link, and Script Codes

Kindly copy and paste to your HEAD tag of your page for the external link, CSS, and a short script.

  1. <link

    rel

    =

    "stylesheet"

    href

    =

    "css/style.css"

    >
  2. <script

    src

    =

    "js/jquery-1.10.2.js"

    ></

    script

    >
  3. <script

    src

    =

    "js/jquery-ui.js"

    ></

    script

    >

  4. <style

    type

    =

    "text/css"

    >
  5. body {
  6. width:800px;
  7. margin:auto;
  8. }

  9. #date_Format_Style {
  10. padding: 20px 40px;
  11. background: #0079B3;
  12. border:2px solid #222;
  13. color: #FFF;
  14. font-size: 1.2em;
  15. margin-top:100px;
  16. text-align:center;
  17. }

  18. .input-style {
  19. margin-bottom: 20px;
  20. }

  21. .form-input-style {
  22. padding: 10px;
  23. width: 250px;
  24. border:1px solid blue;
  25. font-size: 18px;
  26. border-radius:8px;
  27. }
  28. </

    style

    >

  29. <script

    >
  30. $(document).ready(function() {
  31. $( "#date_picker" ).datepicker();
  32. });
  33. function change_DateFormat(date_format) {
  34. $( "#date_picker" ).datepicker( "option", "dateFormat", date_format );
  35. }
  36. </

    script

    >

Output

Date format of mm/dd/yy
2_9.jpg

Date format of dd/mm/yy
3_6.jpg

Date format of yy-mm-dd
4_4.jpg

Date format of M d, y
5_3.jpg


If you are interested in programming, we have an example of programs that may help you even just in small ways. If you want more tutorials, you can visit our website, click here
Hope that this tutorial will help you a lot.

Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.


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

452,292

323,348

323,357

Top