formkit / tempo

📆 Parse, format, manipulate, and internationalize dates and times in JavaScript and TypeScript.
https://tempo.formkit.com
MIT License
2.37k stars 33 forks source link

[Feature request] Dates comparing #17

Closed vanodevium closed 8 months ago

vanodevium commented 8 months ago

It would be nice to have methods for comparing dates like before, equal and after

hrynevychroman commented 8 months ago

@justin-schroeder sorry for tagging, if there is more date-fns function that need to be migrated here I will move them ❤️

justin-schroeder commented 8 months ago

@vanodevium We do have a sameDay function for simple equality, but help me understand before and after. Is this pure syntactic sugar? Since tempo’s primitive is native Date objects you can compare them directly:

const dateA = new Date()
const dateB = new Date('2012-11-10')

dateA > dateB // true
vanodevium commented 8 months ago

@justin-schroeder

yeap, these methods are simply sugar

main goal: I can compare not only Date instances, but strings

Something like

before(new Date(), '2024-02-23');
before(tempoInstance, '2024-02-23');
before(tempoInstance, '2024-02-23 00:01:02');
before(tempoInstance, new Date());
before(tempoInstance1, tempoInstance2);

All these variants works as expected -> true/false

friendly reminder: sameDay() isn't equal() because it skips time.

justin-schroeder commented 8 months ago

I think i can get onboard with the syntax sugar since it should all tree shake out nicely.