Home > Software design >  Getting Date Field with Timestamps Using Axios In laravel
Getting Date Field with Timestamps Using Axios In laravel

Time:07-29

I have tasks table and I want to update the date field into database and through HTML date picker where I want to update the task and task due_date but when I get response data with the date field is coming with timestamps but I did not define the due_date in a tasks table field as timestamps, here is my migration

    {
        Schema::create('tasks', function (Blueprint $table) {
            $table->id();

            $table->foreignId('user_id')
            ->constrained('users')
            ->onUpdate('cascade')
            ->onDelete('cascade');

            $table->longText('task');
            $table->date('due_date');
            $table->integer('status');
            $table->timestamps();
        });
    }

Here is the my Controller Code

    public function single_task($id)
    {
        $task = Task::findOrFail($id);
        // dd($task);
        return response()->json($task, 200);
    }

and below code is from frontend side where I am getting date field as timestamps

        $(document).ready(function(){
            $('.EditTask').click(function(){
                var id = $(this).attr("data-id");
                // $('#task-text').val('');
                // $('#due_date').val('');
                axios.get('task/' id)
                .then(res => {
                    task = res.data;
                    $('#task-text').val(task.task);
                    $('#edit_due_date').val((task.due_date).substring(0,10));
                    test = (task.due_date).substring(0,10);
                    alert(task.due_date);
                    // var today = moment(task.due_date, "yy-MM-DD").format("DD/MM/yy");
                    // $('#edit_due_date').val(today);
                    // alert(today);
                    $('#UpdateTask').attr("data-edit-id", id);
                })
                .catch(err => {
                    console.error(err);
                })
            })

and here is my frontend HTML Code

                    <div >
                        <label>Date:</label>
                        <div >
                            <input type="date" name="edit_due_date" id="edit_due_date" >
                        </div>
                    </div>

and when I debug in chrome inspect element I am getting date with timestamps and if I have task whos due_date is 2022-07-31 but I am getting 2022-07-30T19:00:00.000000Z so please suggest me how to tackle it.

CodePudding user response:

You can use date('Y-m-d h:i:s', strtotime($yourDate)); Hope it will help you

$yourDate='2022-07-30T19:00:00.000000Z';

 dd(date('Y-m-d', strtotime($yourDate)));

This will return 2022-07-30

CodePudding user response:

$('#edit_due_date').val((task.due_date.getDate() '/' (task.due_date.getMonth() 1) '/' task.due_date.getFullYear()));

  • Related