Amir
Amir

Reputation: 4111

Select and show regional jquery datepicker but save in converted gregorian date

I am using an input field element and jquery datepicker in a smarty tpl file like below

MySmarty.tpl:

...        
    <input type="text" id="date_test" />
        <script type="text/javascript">
            {literal}
            $(document).ready(function(){
            $('#date_test').datepicker({dateFormat: 'yy-mm-dd'
        }).datepicker($.datepicker.regional['fa'])
    });
    {/literal}</script>
...

The user select a date from my regional datepicker like 1393-05-21 and save it to the database.

I need to save converted gregorian date 2014-08-12 to the database.

in this case: select and show regional date is correct but save is incorrect.

So i changed the code like below:

    <input type="text" id="date_test" />
    <script type="text/javascript">
        {literal}
        $(document).ready(function(){
        $('#date_test').datepicker({dateFormat: 'yy-mm-dd',
        onSelect: function(dateStr, inst) {
        var date = new gregorian_date(inst.selectedYear, inst.selectedMonth, inst.selectedDay);
        $(this).val(date.getFullYear() + '-' + (date.getMonth()+1) + '-' + date.getDate());
        } 
    }).datepicker($.datepicker.regional['fa'])
});
{/literal}</script>

The problem is: it shows gregorian date value in the box field. It should be like this:

Real value:2014-08-12 show value:1393-05-21

in this case: select regional date and save is correct,but show is incorrect.

Comment: gregorian_date is a custom function

Upvotes: 0

Views: 507

Answers (1)

klenwell
klenwell

Reputation: 7148

Datepicker provides a method to set the date value. Try that:

$('#date-test').datepicker({
    dateFormat: 'yy-mm-dd',
    onSelect: function(value, dp) {
        var gregDate = gregorian_date(dp.selectedYear, dp.selectedMonth, dp.selectedDay);
        $('#date-test').datepicker('setDate', gregDate);
    }
});

Here's a fiddle to demonstrate:

http://jsfiddle.net/klenwell/jqadkmud/3/

Take note: after updating the value, changing the date value for the user quickly gets confusing. It may make more sense to list the Gregorian date in another field.

For example:

http://jsfiddle.net/klenwell/jqadkmud/4/

Upvotes: 1

Related Questions