Forum Moderators: open

Message Too Old, No Replies

Post Tomorrows Date to Hidden Input?

         

MizzBia

11:51 pm on Dec 17, 2008 (gmt 0)

10+ Year Member



hmm... I may be pushing it here but would anyone happen to know of any JS code that will allow me to post tomorrows date to a hidden input field in this format YYYY-MM-DD

I found JS code to post todays date in that format on IRT.org except that it only prints the code to the page. I need to put the JS code in the header and then call it through hidden form fields when needed. This is the code I found for todays date:


<script language="JavaScript"><!--
var date = new Date();
var d = date.getDate();
var day = (d < 10) ? '0' + d : d;
var m = date.getMonth() + 1;
var month = (m < 10) ? '0' + m : m;
var yy = date.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;

document.write(day + "/" + month + "/" + year);
//--></script>

Any tips?

lavazza

12:41 am on Dec 18, 2008 (gmt 0)

10+ Year Member



You could create a variable to 'hold' the string

e.g.

Instead of
document.write(day + "/" + month + "/" + year);

try
var myDateString = (day + '\/' + month + '\/' + year);

Ive escaped the slashes and (in keeping with the rest of the code) used single quotes

Oh... and its not
<script language="JavaScript">
its
<script type="text/javascript">

One question:
What will happen when visitors have scripts turned off?

One suggestion:
Consider php dates - they're implemented server side :)

Results 1 - 100 of about 29,800 from webmasterworld.com for php date [google.com]

MizzBia

2:08 am on Dec 18, 2008 (gmt 0)

10+ Year Member



Well I'm red in the face!
THANK you for the tip, I didn't realize PHP did this so easily. I ended up using PHP's date function and it works perfectly.

Thanks again =]

lavazza

2:31 am on Dec 18, 2008 (gmt 0)

10+ Year Member



I see absolutely no reason for you to be embarrassed

Rather, you should feel proud that you are willing to ask for and accept advice

As there's a heap of stuff that that I don't know and you probably/possibly either know it already or will learn it soon, please do subscribe to the relevant forums and be ready to help me when I ask for help :)