user3142695
user3142695

Reputation: 17332

Set current date to input[type=date] in meteor template

I want to set a date input field to the current date:

Template.something.onRendered(function() {
    var today = new Date();
    var dateString = today.format("yyyy-MM-dd");
    $('#age').val(dateString);
});

<template name="something">
    <input type="date" id="age">
</template>

But this doesn't work and I don't see, what I'm doing wrong...

I'm getting the error Exception from Tracker afterFlush function

Upvotes: 0

Views: 676

Answers (2)

416serg
416serg

Reputation: 448

First off, formatting is off, gotta use moment.

Second, make sure the #age element is in the dom if ($('#age').length > 0) $('#age').val(dateString);

As long as you format your date right, everything should be okay

Upvotes: 0

Christian
Christian

Reputation: 19740

There's a few things that could be preventing that code from working.

Firstly, .format() comes from moment.js, unless you've got your own .format() method set, or you're using some other library. I'm going to assuming you're using moment. So first up you'll need to ensure that you're loading moment.js.

Secondly, you can't use .format() on the Date object directly. Instead, you need to do it like this:

moment(today).format();

Finally, the date string is case sensitive. So "yyyy-MM-dd" won't work. You need to use all uppercase: "YYYY-MM-DD"

moment(today).format("YYYY-MM-DD");

This and more is covered in the docs: http://momentjs.com/docs/

Upvotes: 1

Related Questions