How to change the date format in a Laravel View file?

Laravel provides us with many built-in functions and the Carbon library that greatly eases all date and time-related operations for your project. Many times in application development, you might wish to alter the date format as per your application’s requirements. Let us go through this tutorial to see how to alter the date format as per your requirements in a Laravel View file.

format() method – change date format

The format() function in Laravel helps change the date format in a Laravel View file. Consider the code snippet below that shows its use.

public function functionCall()
{
    $date = now(); // You can replace `now()` with your actual date value

    return view('call_to_view', compact('date'));
}

The functionCall() in a controller passes the current date to your view file. You can then manipulate this date and perform operations like formatting. So to change your date’s format, simply. call the format() method. on the date variable as shown below.

<p>Date: {{ $date->format('d-m-Y') }}</p>

You can pass your desired format along with the format() method and the date preview will show up in your specified format.