-1

can javascript change the date format taken from php like this: 2018-09-22 05:20:48 to like this Saturday,22 September ? iam using vue.js and laravel. because this date is taken from the database using a query i dont know how to get int time and it will be passing to vue component.

Vue Component :

    <tr v-for="(category,index) in allCategories">
                  <td class="v-align-middle"> <img :src="category.images[0].link" class="img-fluid categoriesImg" alt=""> </td>
                  <td class="v-align-middle">{{category.category}}</td>
                  <td class="v-align-middle">{{formatDate(category.created_at)}}</td>
                </tr>



<script>



    import {mapGetters} from 'vuex';
export default {
  name:"category",
  computed: {
      ...mapGetters({
        allCategories:'allCategories'
      }),

    },
    created(){

        // this.$swal('Hello Vue world!!!');

      this.$store.dispatch('GetAllCategories');

    },
    methods:{
      formatDate(date){
        let tanggal = new Date(date);
        return tanggal;
      },
    }

}
</script>

Result

"2018-09-21T22:20:48.000Z"
Faris Dewantoro
  • 1,597
  • 4
  • 17
  • 31

2 Answers2

0

momentjs is the best soltion.

Make sure you set the right format (https://momentjs.com/docs/#/parsing/string-format/)

formatDate(date){
    let tanggal = moment(date, 'YYYY-MM-DD HH:mm:ss').format('dddd,DD MMMM');
    return tanggal;
  },
Hoon
  • 109
  • 4
0

You can use below code :

var monthNames = ["January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"];
var dayNames=['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
d = new Date(date);
time = dayNames[d.getDay()] + ", " + d.getDate() + monthNames[d.getMonth()] ;

For more information you can checkout the Date in javaScript

Mosi
  • 198
  • 6