Date.parse() flag to return only future dates

95 views
Skip to first unread message

Robert

unread,
Sep 30, 2010, 5:37:12 AM9/30/10
to Datejs - A JavaScript Date Library
This is an awesome library !

For scheduling purposes, it can be useful to require only future dates
(this relates to JamesG's recent post -
http://groups.google.com/group/datejs/browse_thread/thread/db652eee09f1978c)
At the moment the library seems to snap to the nearest day, week,
month year etc. for example

Date.parse( '8/22' ) => Sun Aug 22 2010
but I'd like 8/22/2011 (the first 8/22 in the future)
(today is 9/30/2010)

Date.parse( 'Mon' ) => Mon Sep 27 2010
but I'd like Mon Oct 4, 2010 (the next monday after this one)

i know I can say Date.parse( 'next Mon' ) but that does not work
universally (eg Date.parse( 'next 8/22') returns null)
Is there a way to control the snap-to nearest behavior (i.e. instead
of snap to THIS day/week/month/year, snap to NEXT if snap to THIS is
<=now)?

I saw this in the parser code but don't know whether it provides this
capability nor how I would activate it

// relative date / time expressions
g.orientation = _.process(g.ctoken("past future"),

Any suggestions welcome ! Thanks in advance
Robert

Chris Pratt

unread,
Sep 30, 2010, 2:58:36 PM9/30/10
to dat...@googlegroups.com
You could always use a simple function like:

while(d.isBefore()) {
  d.add(1).years();
}

(*Chris*)


--
You received this message because you are subscribed to the Google Groups "Datejs - A JavaScript Date Library" group.
To post to this group, send email to dat...@googlegroups.com.
To unsubscribe from this group, send email to datejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/datejs?hl=en.


Robert

unread,
Sep 30, 2010, 5:07:00 PM9/30/10
to Datejs - A JavaScript Date Library
Unfortunately that is not easy when the date parsed is quite generic
(ie I do not know whether to add an hour, day week or year etc.)

For example, the unit to add would be:

For "mon" it would be +0 or +1 week
For "8/22" it would be +0 or +1 year
etc.

Once you get a date back, you can tell it is in the past but you do
not know what unit to add.
Thats why it can only really be done inside the library. I am sure it
would be pretty easy since it already has to "pick a year" or "pick a
week". Instead of picking the "current" year/month etc. from
Date.today(), a flag could instruct it to pick the next of whatever
unit.

Robert



On Sep 30, 11:58 am, Chris Pratt <thechrispr...@gmail.com> wrote:
> You could always use a simple function like:
>
> while(d.isBefore()) {
>   d.add(1).years();
>
> }
>
> (*Chris*)
>
>
>
> On Thu, Sep 30, 2010 at 2:37 AM, Robert <135...@gmail.com> wrote:
> > This is an awesome library !
>
> > For scheduling purposes, it can be useful to require only future dates
> > (this relates to JamesG's recent post -
> >http://groups.google.com/group/datejs/browse_thread/thread/db652eee09...
> > )
> > At the moment the library seems to snap to the nearest day, week,
> > month year etc. for example
>
> > Date.parse( '8/22' ) => Sun Aug 22 2010
> > but I'd like 8/22/2011 (the first 8/22 in the future)
> > (today is 9/30/2010)
>
> > Date.parse( 'Mon' ) => Mon Sep 27 2010
> > but I'd like Mon Oct 4, 2010 (the next monday after this one)
>
> > i know I can say Date.parse( 'next Mon' ) but that does not work
> > universally (eg Date.parse( 'next 8/22') returns null)
> > Is there a way to control the snap-to nearest behavior (i.e. instead
> > of snap to THIS day/week/month/year, snap to NEXT if snap to THIS is
> > <=now)?
>
> > I saw this in the parser code but don't know whether it provides this
> > capability nor how I would activate it
>
> >    // relative date / time expressions
> >    g.orientation = _.process(g.ctoken("past future"),
>
> > Any suggestions welcome ! Thanks in advance
> > Robert
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Datejs - A JavaScript Date Library" group.
> > To post to this group, send email to dat...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > datejs+un...@googlegroups.com<datejs%2Bunsu...@googlegroups.com>
> > .
Reply all
Reply to author
Forward
0 new messages