Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

obink's avatar
Level 1

convert date format into moment js

laravel blade HTML

<div id="datedisplay" class="">
	{{$invoice->first()->created_at}}
</div>

Javascript

$(document).ready( function () {
var locale = moment.locale('fr');
var theID = document.getElementById("datedisplay");

moment(theID).locale(locale).format('ll');
});

I think I don't quite understand how javascript works. please help.

0 likes
5 replies
Tray2's avatar

This row gives you the whole element and not the value of it

var theID = document.getElementById("datedisplay");

Depending on the type of element it is you need to use either .value or .innedHTML to get the date inside.

moment(theID.value).format('ll');

I heven't tried the code but it looks sound.

obink's avatar
Level 1

I just change that and . . .

2020-12-17 14:35:10

the date doesn't change

Tray2's avatar

What is ot you are trying to achieve here?

  • static date format?
  • A clock that updates every second?

If it's the first you need to assign the value to the element.

theId.value = moment(theID.value).format('ll');

If it's the latter you need to call the function every second.

You can use setInterval for that.

setInterval(function(){ 
   // The code to update here	
 }, 1000);
obink's avatar
Level 1

it is a static date format from the database? it's sort of an Invoice Date to be precise.

Tray2's avatar

Then you should use php to format it with an getFormattedDateAttribute() method in your model so you can call model->formattedDate in your blade file.

public function getFormattedDateAttribute()
    {
        return $this->date->format('F j, Y');
    }

You of course need to adapt the code to suit your desired format.

From php.net

$today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
$today = date("m.d.y");                         // 03.10.01
$today = date("j, n, Y");                       // 10, 3, 2001
$today = date("Ymd");                           // 20010310
$today = date('h-i-s, j-m-y, it is w Day');     // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // it is the 10th day.
$today = date("D M j G:i:s T Y");               // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:18 m is month
$today = date("H:i:s");                         // 17:16:18
$today = date("Y-m-d H:i:s");                   // 2001-03-10 17:16:18 (the MySQL DATETIME format)

Please or to participate in this conversation.