recabos710
recabos710

Reputation: 79

Format Laravel Timestamps in Front End with Javascript

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

Upvotes: 0

Views: 462

Answers (2)

Kevin Pimentel
Kevin Pimentel

Reputation: 1916

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('<YOUR_UTC_TIME_STRING>').format();

Upvotes: 2

Abdusalam mohamed
Abdusalam mohamed

Reputation: 378

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

Upvotes: 0

Related Questions