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

Improve addition/substraction with units #160

Open
Tharit opened this issue Jun 3, 2013 · 3 comments
Open

Improve addition/substraction with units #160

Tharit opened this issue Jun 3, 2013 · 3 comments

Comments

@Tharit
Copy link

Tharit commented Jun 3, 2013

I think it could be really useful if the currently implemented unit handling (such as '+1 Monday') would be improved in a way that I could retrieve things like:

  • the next/previous Monday only if the specified date is not a Monday
  • the start of the next/previous week only if the specified date is not the beginning of a week (would require adding first_weekday from glibc locales)
  • same for months and years..
@bigeasy
Copy link
Owner

bigeasy commented Jun 3, 2013

Actually, you can get plus one Monday if today is not monday with the following:

var startOfWeek = tz(Date.now(), '-1 day', '+1 monday');

Going backwards...

var lastWeek = tz(Date.now(), '+1 day', '-1 monday');

When I'm able to get something via a recipe, I'm adverse to adding something more to the core. One of the goals of Timezone is to be a MicroJS library.

@Tharit
Copy link
Author

Tharit commented Jun 3, 2013

Oh.. I must admit I didn't think of that, thanks. You're absolutely right, no need to add another way for something that's already possible - using Timezone for that very reason.

And now that I'm thinking about it, for months and years this should do the trick:

var startOfMonth = tz(tz(Date.now(),'%Y-%m-01'));
var nextMonth = tz(tz(Date.now(),'%Y-%m-01'),'+1 month');

Just the issue with weeks starting on locale dependant days remaining then. Probably not THAT relevant.

@bigeasy
Copy link
Owner

bigeasy commented Jun 3, 2013

Yes. Exactly. I knew I had a solution for "last monday of the month", but I couldn't remember it.

var secondMonday = tz(tz(Date.now(),'%Y-%m-01'), '-1 day', '+2 mondays');
var lastMonday = tz(tz(Date.now(),'%Y-%m-01'), '+1 month', '-1 monday');

It is reassuring to know that people appreciate the design goals of Timezone. Thank you very much.

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

2 participants