2

I am using mobiscroll.js and am trying to set a particular date like 14/06/2005. This is my senior's code. I am not sure how to edit to set a particular date.

Code:

(function ($) {
    var date = new Date();
    $("#id_childbirthday").scroller({mode: 'clickpick',
        dateOrder: 'ddMyyyy',
        dateFormat: 'dd M yyyy',
        endYear: date.getFullYear() - 7,
        startYear: 1990});
})(jQuery);

The image looks like this:enter image description here

I can set the year to the year i want... But how to edit this code to get it outputting a particular date... Need some guidance...

lakshmen
  • 28,346
  • 66
  • 178
  • 276

3 Answers3

3

According to the docs, your mobiscroll instance has a setValue method.

The syntax appears to be:

$('#id_childbirthday').scroller('setValue', data, true);

... where data is an array of values, matching the order of the wheels.

David Hedlund
  • 128,221
  • 31
  • 203
  • 222
  • I believe that is the best way to go. Odd to have a `startYear` option, but no `startMonth`, `startDay` etc. – Christofer Eliasson Aug 08 '12 at 08:19
  • so if i want to change it to 14/6/2004: $("#id_childbirthday").scroller('setDate', 14/06/2004, true); something like this? – lakshmen Aug 08 '12 at 08:25
  • or $("#id_childbirthday").scroller('setDate', 14/June/2004, true);? – lakshmen Aug 08 '12 at 08:26
  • 1
    For `setValue`, possibly `$('#id_childbirthday').scroller('setValue`, [14, 'June', 2004], true)`, depending on how the month wheel is populated. For `setDate` it's `$('#id_childbirthday').scroller('setDate', new Date(2004, 5, 14), true)`. Note the `5`, as month is defined by a 0-based collection in JavaScript dates. – David Hedlund Aug 08 '12 at 09:02
  • so basically $('#id_childbirthday').scroller('setDate', new Date(2004, 5, 14), true) means that it refers to 14/6/2004? – lakshmen Aug 08 '12 at 09:38
3

I read the document.

I found this: doc

$("#id_childbirthday").scroller('setDate', date, true)

Code shoud be:

(function ($) {
    var date = new Date(); //you can specify the date here!
    $("#id_childbirthday").scroller({mode: 'clickpick',
        dateOrder: 'ddMyyyy',
        dateFormat: 'dd M yyyy',
        endYear: date.getFullYear() - 7,
        startYear: 1990});
    var date1 = new Date(2012, 3, 10);
    $("#id_childbirthday").scroller('setDate', date1, true);

})(jQuery);
Shih-En Chou
  • 4,007
  • 6
  • 20
  • 27
2

From DateTime Preset documentation

.scroller('setDate', date, fill, time) If a preset is selected sets the scroller date/time from the date parameter passed as a Date object. If the 'fill' parameter is true, the associated input field is also updated with the new value. The 'time' parameter specifies the duration of the animation in seconds to scroll the wheels to the new date. There is no animation, if 'time' is not specified

You'd need something like this:

(function ($) {
    var date = new Date();
    $("#id_childbirthday").scroller({mode: 'clickpick',
        dateOrder: 'ddMyyyy',
        dateFormat: 'dd M yyyy',
        endYear: date.getFullYear() - 7,
        startYear: 1990}).scroller('setDate',new Date(2005,5,14));
})(jQuery);
Levi Kovacs
  • 1,159
  • 8
  • 14