Avatar of plusone3055
plusone3055
Flag for United States of America asked on

textlink that will insert date into a textbox

hello
 
I need a textlink ("Yesterday") that by clcicking it will insert the current date (- 1 day) into a textbox
on the current page

so  kind of like clicking a date on a calendar and it inserting a date into a textbox but instead just clicking a textlink athat only inserts yesterdays date
JavaScriptHTML

Avatar of undefined
Last Comment
plusone3055

8/22/2022 - Mon
Kyle Hamilton

here's a jquery one.

You can format the date different ways. look up the date unction

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>

<script>
    $(function(){
        
        var yesturday =new Date();
        yesturday.setDate(yesturday.getDate()-1);

        $("#link").click(function(e){
            e.preventDefault();
            $("#date").text(yesturday);
        });
    });
    
</script>

<a href="#" id="link">Get yesturday's date</a><br/>
    <div id="date"></div>

Open in new window

plusone3055

ASKER
I cannot use  JQuery in this page
just regular Javascript please

I already have a textbox in my form that a calendar links to but the calendar only starts wit  the current date for reasons they set..It was requested that we have another link next to the calendar that puts  only yeterdays date into the textbox
Kyle Hamilton

<script>
var yesterday =new Date();
yesterday.setDate(yesterday.getDate()-1);

var link = document.getElementById("link");

function setYesterday() {
    var date = document.getElementById("date");
    date.innerHTML = yesterday;
 };

</script>

<a href="#" id="link" onclick="setYesterday();">Get yesterday't date</a><br/>
<div id="date"></div>
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Kyle Hamilton

If the textbox is a textarea or input field you may need to use:

var formObject = document.forms['myform_id'];
formObject.elements["element_name"].value = yesterday;
plusone3055

ASKER
yes
the form is named       form1        
The textboxname is named datefilter

can you put that into the codeyou had above..

im sorry the var date and var link confused me
Kyle Hamilton

var yesterday =new Date();
yesterday.setDate(yesterday.getDate()-1);

function setYesterday() {
    var formObject = document.forms["form1"];
    formObject.elements["datefilter"].value = yesterday;
 };

Open in new window


sorry for all the edits, bloody typos....
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
plusone3055

ASKER
okay and now for the link with that codeand if possible just the dd/mm/yy format I don't need the day :)
Kyle Hamilton

<script>
var yesterday = new Date();
yesterday.setDate(yesterday.getDate()-1);

var dayofWeek = yesterday.getDay(); // day o fthe week
var month = yesterday.getMonth() + 1; // the month 0-11
var dayofmonth = yesterday.getDate(); // day of the month
var year = yesterday.getFullYear(); // the year

var y = month +"/"+ dayofmonth +"/"+ year;

function setyesterday() {
    var formObject = document.forms["form1"];
    formObject.elements["datefilter"].value = y;
};
</script>

<a href="#" onclick="setyesterday();">Get yesterday't date</a>

<form name="form1">
    <textarea name="datefilter"></textarea>
</form>

Open in new window

plusone3055

ASKER
okay heres where its getting ugly
I had taken the code you gave me the last time before the recent post
and did this...

<script>
function setYesterday(dateString) {
    var yesterday =new Date();
yesterday.setDate(yesterday.getDate()-1);
      
      var formObject = document.forms["Form1"];
    formObject.elements["datefilter"].value = yesterday;
 };
            </script>

<a href="##" id="datefilter" onclick="setYesterday();">Pastdue</a><br/>

now its doing what I was hoping it would do and put yesterdays date into the textbox...

28-May-2012

BUT
when I click the button to run the query its saying

Conversion failed when converting datetime from character string
im using ColdFusion by the way.. anyway to force it to date format ???
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Kyle Hamilton

Yes, it gets inserted as a string. I'm not that familiar with coldfusion. But if you are inserting the date into a database, then you can format it for that particular database if you want.

OR,  if the link is clicked, get the date -1 with your coldfusion application - then it's an actual date. And only use the textarea for display purposes.

Does that make sense?
plusone3055

ASKER
what is the quickest way to make it (dateformat dd-mmm-yyyy) hwen being passed in with Javascript
Ive been trying mayn different ways and its not working
Kyle Hamilton

I just did a little cursory "research". If you use the latest code I gave you, I think the format  (mm/dd/yyyy) will satisfy coldfusion. Give it a shot.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
plusone3055

ASKER
could you please put it in the code..
im sorry i don't know where it would go ?
Kyle Hamilton

Use the latest code I posted. change this line:

var y = month +"/"+ dayofmonth +"/"+ year;

to:

var y = month +"-"+ dayofmonth +"-"+ year;

You can remove dateString from your function parameters. You won't need it anymore. No need to run extra functions.
Kyle Hamilton

here's the whole script again:

<script>



var yesterday = new Date();
yesterday.setDate(yesterday.getDate()-1);

var dayofWeek = yesterday.getDay(); // day o fthe week
var month = yesterday.getMonth() + 1; // the month 0-11
var dayofmonth = yesterday.getDate(); // day of the month
var year = yesterday.getFullYear(); // the year

var y = month +"-"+ dayofmonth +"-"+ year;

function setyesterday() {
    var formObject = document.forms["form1"];
    formObject.elements["datefilter"].value = y;
};

 
</script>

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
plusone3055

ASKER
this code does not put anyhting in the box
i got a JS error saying it was expecting a ; somewhere


<script>
var yesterday = new Date();
yesterday.setDate(yesterday.getDate()-1);

var dayofWeek = yesterday.getDay(); // day o fthe week
var month = yesterday.getMonth() + 1; // the month 0-11
var dayofmonth = yesterday.getDate(); // day of the month
var year = yesterday.getFullYear(); // the year

var y = month +"/"+ dayofmonth +"/"+ year;

function setyesterday() {
    var formObject = document.forms["form1"];
    formObject.elements["datefilter"].value = y;
};
</script>

<a href="##" id="datefilter" onclick="setyesterday();">test</a><br/>
plusone3055

ASKER
WAIT WAIT
The script you gave me works except i need the format to be dd-mmm-yyyy

 can it be that way with your code ????
Kyle Hamilton

It works for me. There must have a typo somewhere.

Here's the entire file:

<!DOCTYPE html>
<html>
<head>
<title>Yesterday</title>
<script>



var yesterday = new Date();
yesterday.setDate(yesterday.getDate()-1);

var dayofWeek = yesterday.getDay(); // day o fthe week
var month = yesterday.getMonth() + 1; // the month 0-11
var dayofmonth = yesterday.getDate(); // day of the month
var year = yesterday.getFullYear(); // the year

var y = month +"-"+ dayofmonth +"-"+ year;

function setyesterday() {
    var formObject = document.forms["form1"];
    formObject.elements["datefilter"].value = y;
};

 
</script>


</head>
<body>

    <form name="form1">
    <a href="#" onclick="setyesterday();">Get yesterday't date</a><br/>
    <textarea name="datefilter"></textarea>
    </form>
</body>
</html>

Open in new window


One thing that I did that might be an issue, is at some point I changed the function name from setYesterday to setyesterday...
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
plusone3055

ASKER
no no i got it to now throw the error but it said i entered an invalid date cause its set to
mm-ddd-yyyy

heres the code that worked up to that point

<script>



var yesterday = new Date();
yesterday.setDate(yesterday.getDate()-1);

var dayofWeek = yesterday.getDay(); // day o fthe week
var month = yesterday.getMonth() + 1; // the month 0-11
var dayofmonth = yesterday.getDate(); // day of the month
var year = yesterday.getFullYear(); // the year

var y = month +"-"+ dayofmonth +"-"+ year;

function setyesterday() {
    var formObject = document.forms["form1"];
    formObject.elements["datefilter"].value = y;
};

 
</script>

i just need it changed so the date format is dd-mmm-yyyy
is that possible
I thank you for all this help I can go home after this its 3 hours past quitting time
Kyle Hamilton

oh wait yerself :)

need to change this:

var y = month +"-"+ dayofmonth +"-"+ year; // 5-28-2012

to this:

var y = dayofmonth +"-"+ month +"-"+ year; // 28-5-2012

do you need the whole script again?
plusone3055

ASKER
im so sorry here is the real correct format i hadn't finished typing before you submitted

Alert! You have submitted an invalid date !
Please note that all dates must be input using the "d-mmm-yy" format! You submitted the value "5-28-2012".
The month value must be any one of the following: Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Kyle Hamilton

just to clarify:

28-May-12

or can it be:

28-May-2012
plusone3055

ASKER
28-May-2012
ASKER CERTIFIED SOLUTION
Kyle Hamilton

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
plusone3055

ASKER
thank you for all the time :)
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.