5

I'm using verta library to convert gregorian dates to hijri date in laravel https://github.com/hekmatinasser/verta

in simple mode (.blade.php files) I'm using code like this:

{{ Verta($category->created_at)->format('%d %B %Y') }}
for converting {{created_at}}

but in Vue js I can't access to this format an I have

{{category.created_at}}

how can I convert the vue js dates with this method?

jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • if you want to reuse what you are doing in laravel you will need to parse the dates before returning them to the frontend. you can use mutations (https://laravel.com/docs/5.8/eloquent-mutators) to do this automatically when reading database entries – Frnak Mar 05 '19 at 09:24
  • https://stackoverflow.com/questions/46708110/convert-date-format-in-javascript-using-vuejs – Ajay Makwana Mar 05 '19 at 10:38

4 Answers4

6

Simply, You can do it by follow three steps by moment.js.

Step 1:

npm install moment --save

Step 2:

import moment from "moment";

Step 3:

export default {
    data() {
        return {
            moment: moment
        }
    } }

Done.

Now do in below

 {{ moment(category.created_at).format("DD-MM-YYYY") }}
Faridul Khan
  • 1,741
  • 1
  • 16
  • 27
1

You can use a mutator on your model to parse the date before returning it.

public function getCreatedAtAttribute($value)
{
    return Verta($value)->format('%d %B %Y');
}

This should be defined on your Category Model

{{category.created_at}}

Will then contain the already parsed date

Frnak
  • 6,601
  • 5
  • 34
  • 67
1

In Laravel Side

1)
public function getCreatedAtAttribute($value)
{
    return $value->format('d-m-Y');
}

2)protected $casts = [
    'birthday'  => 'date:Y-m-d'
];

In Vue .js side make filters

npm install moment

In  resources/js/app.js

import moment from 'moment';

Vue.filter('formatDate', function(value) {
    if (value) {
        return moment(String(value)).format('MM/DD/YYYY')
    }
});

in Vue component side

{{ category.created_at | formatDate }}
Talha Fayyaz
  • 162
  • 1
  • 2
0

You can use vue-moment.

npm install vue-moment

...and require the plugin like so:

Vue.use(require('vue-moment'));

Then, in your application you can do something like this:

{{ someDate | moment("dddd, MMMM Do YYYY") }}

Deprecated in Vue 3

Victor
  • 995
  • 1
  • 10
  • 26