1

I want to use this input type date but to result after submit the next format: Day August 2019

The default format is 2019-08-06

<form action="#">
  <input type="date" name="code">
  <input type="submit">
</form>

What can I do to result this format: Day August 2019 ?

Thank you in advance.

Toony
  • 11
  • 4
  • Possible duplicate of [Where can I find documentation on formatting a date in JavaScript?](https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript) – Abdulhaq Shah Aug 19 '19 at 10:53
  • https://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript – Abdulhaq Shah Aug 19 '19 at 10:54

3 Answers3

0

There are certain functions to extract Month and Day from a date in Javascript. I hope the following reference might help :

How to change date format in JavaScript

amrs-tech
  • 485
  • 2
  • 14
0

You can do it with JavaScript.

Date is global object in JS. With it's own build-in methods, check it here.

In order to achieve what you want, so let the user to input a date in format (00 MONTH 0000) you should use 3 different inputs (one will be select to change month by name) and then concatenate it while submitting a form.

The other way, if you want this value to be f.e. printed in different place you can use formatting function like this:

function formatDate(date) {
  const months = [
    "January", "February", "March",
    "April", "May", "June", "July",
    "August", "September", "October",
    "November", "December"
  ];

  const day = date.getDate();
  const monthIdx = date.getMonth();
  const year = date.getFullYear();

  return `${day} ${months[monthIdx]} ${year}`;
}

console.log(formatDate(new Date())); // in order to print it in console
olejniczag
  • 384
  • 1
  • 10
0

Try using moment js, Its simple. Hope it helps

function dateformat()
{
     var date = document.getElementById('date').value;
     date = moment(date).format('D MMM, YYYY');
     console.log(date)
     return false;
}
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script>
<form action="#" onsubmit="return dateformat()">
  <input type="date" name="code" id="date">
  <input type="submit">
</form>
Deepak A
  • 1,624
  • 1
  • 7
  • 16