Using date.js with my ionic app


#1

hi folks,
i’m trying to use date.js within my app and having a hard time importing it.
i installed date.js as well as @typing/datejs. as far as i understand, importing this correctly should extend the Date object with more functions (e.g.: Date.today()). however, nothing i tried works :frowning:

i tried:
import { Date } from 'date.js’
import Date from 'date.js’
import 'date.js’
import * as date from 'date.js’
and probably another thing or two.

no matter what, either i end up with an undefined Date object, or a Date object that looks like the original with non of the functions i’m looking for. also, tried to apply similar solutions i found for moment.js with no luck

any help would be greatly appreciated!


#2

If you don’t get any better answers, I would recommend ditching it in favor of date-fns.


#3

thanks @rapropos. for one, i don’t know that i’ll have a better time with that library on this specific issue. also, i looked at date-fns but date.js is able to parse strings like “next week” (and so on) which is something i’d really like to use in my app. as far as i can tell, date-fns does not support that.


#4

Hi

You may need to do an explicit import of the js library in your index.html

Regards

Tom


#5

I think “next-week” will come in the next major release of date-fns. They already did the development and the feature is implemented in the alpha version (if I understand correctly the feature you are thinking of)

If your app is not yet in store, maybe you could try to work with alpha? Till you’ll be ready, maybe the version 2 of date-fns will be ready too?

Alternatively you could use moment.js which support this feature but pay attention, this library is BIGGER than date-fns which means that your app will take more time to load it which could also impact your boot time according where you use it.


#6

thanks @Tommertom. i tried that too, but to no effect.
i could be doing it wrong, so if you’d care to share an example i’d appreciate it!


#7

thanks @reedrichards. the additional feature i’m looking for (which is why i’m eyeing date.js over others), beyond proper date formatting and parsing, is being able to parse a string like “on tuesday morning” into a Date. Natty (java) does this really well, and it looks like date.js does (i just checked moment.js again and can’t find a reference to something like this in the docs)


#8

Hi

Did you check some former posts:

https://forum.ionicframework.com/search?context=topic&context_id=107313&q=Import%20js%20library&skip_context=true

And the official doc

try importing the examples in the tutorials and try to repeat on date.js

Possibly on a blank project

Regards

Tom


#9

Tom: i did check previous posts as well as the official doc (which i followed initially with no success).
i fully expect i’m missing something semi-obvious, but not sure what it is.
in my original post above, i added a few examples of things i tried while attempting to import the library.


#10

Honestly I’ve got no idea how to solve that. There is absolutely no way to find workaround to that process?

Maybe you could submit a feature request/idea to the github of date-fns or moment.js, maybe some guyz gonna like your idea and would like to implement it

Sorry can’t help much


#11

That is fine. he only issue I need to figure out is how to import this lib
:slight_smile:

Thanks again


#12

Hi
I tried myself to import datejs. I notice there is a npm package called datejs. And indeed typings, but soon after the import fails as the package is not recognised as a package.

Later I tried importing a different library in similar way, which worked.

Looking at the postings on stackoverflow and google search I am inclined to think that a bit of deepdiving is needed, to look how the package is actually placed in node_modules.

And that goes beyond my appetite for this moment.

Regards

Tom


#13

well, at least i know it’s not something super obvious that i’m missing.
fwiw, i did dig into node_modules for the package but found nothing meaningful (or didn’t know what to look for) before posting here :slight_smile:

thanks again, and if someone else has ideas - pile them on!