JerrySievert/date-utils

Version 2 Feature Discussion

JerrySievert opened this issue · 14 comments

Let's talk about possible features and changes for version 2!

I would be nice if there was a version that does not alter the default Date object prototype.

Check out cromag.js

I want crop feature. sometimes I need to set milisecond, second, minute field to zero. I'd like to see that feature in date-utils.

What about i18n? :)

Just to continue on #36 and compacting the API, I would propose the following:

  • Instead of Date.validateDay etc. go for Date.validate. In this case the date atom is passed within an Object (ie {day: 4}). The function should check out the parameters and raise error/warning depending on Object contents.
  • Instead of d.addMilliseconds etc. go for Date.add. Same idea as above.
  • Instead of d.removeMilliseconds etc. go for Date.remove. Same idea as above.
  • Consider merging d.add and d.remove into a single method, d.offset. In this case the direction would be determined based on whether passed number is positive or negative. Ie. to remove a day you would do d.offset({day: -1}).

Doing this would remove 20-21 functions/methods from the API depending whether you go with offset.

Maybe there are other ways to compact but these seem the most obvious.

i like the idea of compacting the api - want to finish documenting everything so there is fresh knowledge, and a good tag for 1.x.

It would be nice if the toFormat( ) series of functions took symbols for timezones such as Z etc. So we can generate string dates that have an offset of -05:00 for example

@jadrake75 if you can expand that into a set of rules, it would help a lot - at that point we can talk about a complete ruleset and set up boundaries.

I would love chainable ruby/rails like dates (I know this is js, but bare with me). I would love to be able to code:

7.days().from_now()
14.minutes().ago()

I realize that we're not going to extend the Number object, but something like.

Date.integer(7).days().from_now().at_beginning_of_day() // hh:mm:ss.ms / 00:00:00.000
Date.integer(7).days().from_now().at_end_of_day() // hh:mm:ss.ms / 23:59:99.999
Date.i(14).minutes.ago()
Date.i(15).days().since(Date.i(1).week().ago())
Date.between(new Date("1/1/2014"), new Date("12/11/2014")).days()

would be amazing!

I would like to see a static method added as a proxy to calling the constructor. This would ease testing by providing a method that can be easily mocked. Perhaps... Date.now()? We already have Date.today() so this would be very similar.

I may be missing something with testing as I'm just getting started, however I can't figure out how to mock a constructor using Jest.

In fact I think I will fork and add this. Would you consider a pull request to add it?

i'm curious what this would add, since it would be a synonym to new Date().

as an aside, Date.now() is already taken :)

It is indeed. I've given it some more thought and I don't think it would help me with testing anyway since date-utils extends the global Date object. As far as I know Jest doesn't have a way to mock objects/methods unless they can be loaded with require(). Sorry for the spurious suggestion.

The library is very nice, thank you for providing it.

to(UTC)Format's formatting patterns could be more like what other languages use for specifying date formats; the current patterns are a tad surprising at times (like having HH24 for 24h padded time, but HH for 12h padded, and no way of having unpadded 24h hours). Java's patterns are, surprisingly enough, easy to use and follow. With patterns akin to Java's, the more letters you have in the pattern the longer the output you get (either more padding or abbreviated/long forms of words), and patterns use both lower and upper case.

Some examples:

  • the full name of a month could be MMMM, the abbreviated month name could be MMM, zero-padded month could be MM, and single or double digit month could be M
  • 24h hours could use H (with HH for zero-padded)
  • 12h hours could use h/hh
  • minutes could be m/mm instead of just the padded MI

etc.

An ordinal suffix for the day of month, like 2nd, 6th, etc would be incredibly useful. Like the S flag in PHP's date format function.