Question

i'm using X-editable extension for a date input with validation:

$('.editable-date').editable({
        type: 'date',
        pk:1,
        params: function(params){
            params.dataId = dataId;
            return params;
        },
        validate : function(value){
            return "value is "+value; //validation 
        },
        url: '<?php echo Yii::app()->createAbsoluteUrl("/rga/controller/update"); ?>',
        success: function(response){

        }
    });

and In the controller for the submit:

public function actionUpdate(){
    if(isset($_POST['value'])){
       $dataId = $_POST['dataId'];
       $value = $_POST['value'];
       ChromePhp::log($value); //output value to console      
    //some code here
    }
}

the Chrome log displays the date in yyyy/mm/dd format ( example 2014-24-02 ) but in the validate code in the JavaScript; when i try to output the date. it's in a different format, the return string outputs some kind of :

value is Sun Jun 29 2014 08:00:00 GMT+0800 (Malay Peninsula Standard Time)

I need to do some code for validation, but I need to do it in the yyyy/mm/dd format. Why is the date format different in the validate and submit? And if ever how do i convert the date string to the format i need? Is there a php/javascript function for this? Many Thanks!

Was it helpful?

Solution

The value being returned is a JavaScript Date instance. You should be able to use the Date methods to access the date in another format, for example:

validate : function(value){
    var month = ("0" + (value.getMonth()+1).toString()).slice(-2); // The getMonth() method returns the month (from 0 to 11) for the specified date
    var day = ("0" + value.getDate().toString()).slice(-2);
    var year = value.getFullYear().toString();
    var date = month + "/" + day + "/" + year;
},

Note the slices to add zero-padding for single-digit days and months.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top