0

I have build this function:

function currentDate() {
    var date;
    var currentDate = new Date();
    var day = currentDate.getDate();
    var month = currentDate.getMonth() + 1;
    var year = currentDate.getFullYear();

    // if the month length = 1 add "0" to ir
    if (currentDate.getMonth().toString().length == 1) {
        month.toString();
        month = "0" + month;
    }

    // if the day length = 1 add "0" to ir  
    if (currentDate.getDate().toString().length == 1) {
        day.toString();
        day = "0" + day;
    }

    var ISO = year + "-" + month + "-" + day;
    var EUR = day + "/" + month + "/" + year;

    return date = {
        "ISO": ISO,
        "EUR": EUR
    };
}

When I click on a button I want the ISO date or the EUR date to be passed into a text input. How can this be done???

Thank you..

3
  • And you just need return { "ISO": ISO, "EUR": EUR };. No need for date = , especially as you don't even use date. Commented Mar 10, 2011 at 16:20
  • tnx man I'll keep that in mind... the json variable will be accessed directly then Commented Mar 13, 2011 at 19:03
  • There is no JSON here. Commented Mar 14, 2011 at 1:02

3 Answers 3

1
<html>
  <head>
    <Script>
      // your sript goes here
    </script>
  </head>
  <body>
    ...
    <input type="text" id="date" size="15" /><br />
    <input type="button" onclick="document.getElementById('date').value=currentDate().ISO;" value="ISO" />
    <input type="button" onclick="document.getElementById('date').value=currentDate().EUR;" value="EUR" />
    ,...
  </body>
</html>

DEMO

Or jQuery: DEMO

Sign up to request clarification or add additional context in comments.

Comments

1

Try out this fiddle example

<button data-type="EUR">set date EUR</button>
<button data-type="ISO">set date ISO</button>
<input id="date" type="text" />

JS:

$('button').click(function(e){
    var d = currentDate(),
        type = $(this).data().type;
    $('#date').val(d[type]);
});

Comments

-1

When defining an object the key does not need to be in quotes.

<input id="cdate" />
<input type="button" onclick="$('#cdate').val(currentDate().ISO)" />

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.