Get System Date and Time

I am working on this adonis project and i need to get the date and time for the application.

Your question is not related with adonis.
take a look
If u talking about database date/time
here is the documentation

It is related to adonis maybe you do not understand my question properly. In laravel you use {{ now()->format('l, d F Y \a\t h:i A')}} which displays Tuesday, 30 June 2020 at 09:54 PM how do you get the current date and time also in Adonis Js or Node?

u can make ur own function like this

const week_day = {
  0: "Sunday",
  1: "Monday",
  2: "Tuesday",
  3: "Wednesday",
  4: "Thursday",
  5: "Friday",
  6: "Saturday",
};
const month_names = {
  0: "January",
  1: "February",
  2: "March",
  3: "April",
  4: "May",
  5: "June",
  6: "July",
  7: "August",
  8: "September",
  9: "October",
  10: "November",
  11: "December",
};

const now = new Date();
const date = formatDate(now);

function formatDate(date) {
  return `${week_day[date.getDay()]} ${date.getDate()} ${
    month_names[date.getMonth()]
  } ${date.getFullYear()} at ${date.getHours()}:${date.getMinutes()}`;
}

or just use moment.js
https://momentjs.com/docs/#/displaying/

1 Like

I did this{{moment().format("dddd, MMMM Do YYYY, h:mm:ss a");}} and i got this error Error Cannot call function moment from layouts/navigation.edge view

I did this

'use strict'

const Post = use('App/Models/Post')

const week_day = {
    0: "Sunday",
    1: "Monday",
    2: "Tuesday",
    3: "Wednesday",
    4: "Thursday",
    5: "Friday",
    6: "Saturday",
  };
  const month_names = {
    0: "January",
    1: "February",
    2: "March",
    3: "April",
    4: "May",
    5: "June",
    6: "July",
    7: "August",
    8: "September",
    9: "October",
    10: "November",
    11: "December",
  };

const now = new Date();
const date = formatDate(now);

class PostController {
    async home ({view}) {
        //Fetch a post
        const posts = await Post.all()

        return view.render('home', { posts: posts.toJSON() })
    }

    async formatDate(date)
    {
        return `${week_day[date.getDay()]} ${date.getDate()} ${
            month_names[date.getMonth()]
          } ${date.getFullYear()} at ${date.getHours()}:${date.getMinutes()}`;
    }
    
}

In my edge view i did this ${date.getFullYear()} and i am getting this error date is not defined

why u make it a controller method? My advice to you is learn javascript and mvc architecture. I’m sorry, but i can’t help u while u doing such things.

I was able to do this and it worked like a cham
You install luxon npm i luxon . And add the following inside start/hooks.js :

const { hooks } = require('@adonisjs/ignitor')

hooks.after.providersBooted(async () => {
  const View = use('View')
  const { DateTime } = require('luxon')

  View.global('todayDate', () => {
    return DateTime.fromJSDate(new Date()).toFormat('LLLL\',\' d cccc Y \'at\' h:m a')
  })
})
1 Like