Home > Enterprise >  How can change datetime value to string in jquery script of laravel blade file
How can change datetime value to string in jquery script of laravel blade file

Time:01-14

I get data from database by laravel as follow

        $priceall = Price ::get();

    

       return response()->json([
        'priceall' => $priceall
     
       ]);

i send them as json to blade file.So i have jquery script in blade file as follow

 $(document).ready(function() {

      chart();
      function chart() {
                     
                     $.ajax({
                         type: "GET",
                         url: "/candle",
                         dataType: "json",
                         success: function(response) {
                 
                          $.each(response.priceall, function(key, item) {

                           [  item.created_at  ]
                          });

                       }

});

in this code , i want to change item.created to string time as strtotime() function in php that cannot use in javascript code .

how can i do that ,I want to change this

2023-01-12 10:49:00

to example

13243957486000

can someone help me

CodePudding user response:

The first answer would be to format the date server side as it's easier to manipulate date in php than js.

If that's not an option you can try something like this

let date = new Date(item.created_at);
date.getTime()

Be aware that the supported format of the string parameter is limited and should theorically be formated like this : YYYY-MM-DDTHH:mm:ss.sssZ (cf: https://tc39.es/ecma262/#sec-date-time-string-format). But most browsers accept regular variations and you should not encounter any issue directly using a date string formatted like "YYYY-MM-DD HH:mm:ss"

Basically doing that you're creating a Date (https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Date) object, then you use it like you'd do with a php datetime (with some quirks, like with getMonth which return months numbered from 0 to 11... RTFM)

Edit: clarification and typo

  • Related