Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nuxt 3 support ? #62

Open
productdevbook opened this issue Oct 12, 2021 · 4 comments
Open

nuxt 3 support ? #62

productdevbook opened this issue Oct 12, 2021 · 4 comments

Comments

@productdevbook
Copy link

repo: https://github.com/productfrontenddeveloper/nuxt3_bug

nuxt/bridge#226

@jyotirmaybarman
Copy link

Hello, I wrote a plugin for my use case,

import { defineNuxtPlugin } from '#app';

export default defineNuxtPlugin(nuxtApp => {
  nuxtApp.provide('format',(rawtime)=>{
    const months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ];
    // let date = new Date("2021-10-30T19:51:27.710Z");
    let date = new Date(rawtime);
    let year = date.getFullYear();
    let month = date.getMonth();
    let day = date.getDate();
    let hh = date.getHours();
    let mm = date.getMinutes();

    let AmOrPm = hh >= 12 ? 'AM' : 'AM';
    hh = (hh % 12) || 12;

    let diffInSec = Math.floor((Date.now() - date) / 1000);
    if(diffInSec < 30){
        return  'Just Now';
    }
    if(diffInSec < 59){
        return  diffInSec + ' seconds ago';
    }
    let diffInMin = Math.floor(diffInSec/60);
    if(diffInMin<59){
        return diffInMin + ' mins ago';
    }
    return months[month] +' '+ day+' '+year+' , '+hh+':'+mm +' '+ AmOrPm;
  });
});

But is it the right way of doing it ? or can I use dateFns as a plugin (I tried but failed to get it working) in nuxt ??

@Cast-El
Copy link

Cast-El commented Nov 6, 2023

Hello I have found a way to import datefns within nuxt3 :

//plugins\datefns.ts
import * as datefns from 'date-fns'

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.provide('datefns', datefns)
})

@MickL
Copy link

MickL commented Nov 23, 2023

import * as datefns from 'date-fns'

This will import FULL date-fns without tree shaking, even tho you might only use a few functions of it. I highly recommend not doing that.

If you want to use date-fns within your client- or server-code (but not in template) there is no need to use this module! This module is just needed if you want to use date-fns function within your templates.

Be aware that until date-fns 3.0 is released date-fns is not working on edge workers (like Cloudflare Workers or Vercel Edge Functions): nitrojs/nitro#1889

@Cast-El
Copy link

Cast-El commented Nov 28, 2023

On our project we use only few functions.
In that case you can also pick functions you need :

import {format,parse, parseISO, getDay} from 'date-fns'

const datefns: any={
  format,
  parse,
  parseISO,
  getDay
}

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.provide('datefns', datefns)
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants