-2

I posted a similar question last night, unfortunately I included the wrong code to get the help I need. Let me try again.

I am trying to autofill a 'date' field in my web form so the user doesn't have to enter it.

Currently, I am using the code below in the form field, which works fine, except it doesn't provide the date in the correct format for my particular needs.

    <input id="date" name="date"><script type="text/javascript">document.getElementById('date').value = Date();</script>

The code above produces a result like this:

Wed Mar 22 2017 00:59:21 GMT-0700 (Pacific Daylight Time)

I would rather the result was like this:

Wednesday March 22, 2017

Jamie Sexton
  • 157
  • 1
  • 4
  • 16
  • Have a look at [MomentJS](https://momentjs.com/), they have loads of easy ways to format dates. – Matt Mar 22 '17 at 22:23

1 Answers1

0

Try this -

(function() {
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];

var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];

Date.prototype.getMonthName = function() {
    return months[ this.getMonth() ];
};
Date.prototype.getDayName = function() {
    return days[ this.getDay() ];
};
})();

var now = new Date();
var day = now.getDayName();
var month = now.getMonthName();
var date = now.getDate();
var year = now.getFullYear()
 
 document.getElementById('date').value = day + ' ' + month + ' ' + date + ', ' + year;
<body>
<input class="fieldsRt" id="date" name="date">
</body>
Harsheet
  • 728
  • 9
  • 23
  • Thanks Harsheet. I already have that code. and as a js file. And it works fine if I call it into another part of my site using a php include code. But it isn't working in the form field. Not as simply as your example is. – Jamie Sexton Mar 22 '17 at 22:21
  • How is the form field identifying that script as 'date'? and where should I include that js code? Same page? diff page? etc.... – Jamie Sexton Mar 22 '17 at 22:27
  • Sorry but I did not get your question. You can include that js code anywhere, means in the script tag or in a separate js file as well. – Harsheet Mar 22 '17 at 22:29
  • IDK.... still can't figure it out. I put your code in the head of the page, and did the form field exactly like you did..... Its not populating the field at all. Field is blank. – Jamie Sexton Mar 22 '17 at 22:35
  • Can you add a snippet with the exact code you have? – Harsheet Mar 22 '17 at 22:38
  • The form is here: http://www.rebelplanetnews.com/article-form2.php – Jamie Sexton Mar 22 '17 at 22:41
  • Are you sure you have added the correct script? – Harsheet Mar 22 '17 at 23:27
  • Yes.... I added your code correctly. And it does work. Thank you for that. However, the code only works if I put the full – Jamie Sexton Mar 23 '17 at 01:11
  • Have you tried adding the js code in a separate file and then calling it in your page in the head section? – Harsheet Mar 23 '17 at 01:16
  • Yes, I have. But somewhere between that and making it appear in the field (prepopulated), something isn't working I guess I need someone to outline it in all three parts for me since I can't seem to get my mind around it today. 1.) The external file 2.) The head code 3.) The code that goes inline to populate the field. – Jamie Sexton Mar 23 '17 at 01:28
  • Can you try by adding all the js code in window.onload = (the above function). – Harsheet Mar 23 '17 at 01:42
  • Thanks man. I'm having a stressful day today. I can't even think straight enough to concentrate on figuring this out right now. I'll just have to use the long code in the form for now and maybe come back to it later when I'm fresh. – Jamie Sexton Mar 23 '17 at 01:52
  • That's alright, but do try my suggestion once. Hope it works! – Harsheet Mar 23 '17 at 01:53