Home > Software engineering >  Format Laravel Timestamps in Front End with Javascript
Format Laravel Timestamps in Front End with Javascript

Time:01-06

I want to format the Laravel date format generated by timestamps, but I want to do that in the front end with Javascript, the time value is something like this 2022-01-03T19:16:16.000000Z, so what I want to do is to format it to something like this: 02 Jan 2021

CodePudding user response:

Here's a previous post on how you can accomplish this: How to handle datetime between php (Laravel api) and javascript (AngularJS)

However, it's worth noting that Laravel handles this for you. See documentation on Date Casting.

/**
 * The attributes that should be cast.
 *
 * @var array
 */
protected $casts = [
    'created_at' => 'datetime:Y-m-d',
];

As the post suggests you can use momentjs:

correctTime = moment($myLaravelObject->my_laravel_date_at).format();

CodePudding user response:

You don't need to format it in your frontend, you can use carbon to accomplish that task in your laravel backend, it is simple and straight forward

  •  Tags:  
  • Related