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

Accept date that is supplied relatively (yesterday, 2 days ago) #4

Open
icyflame opened this issue May 22, 2016 · 2 comments
Open

Accept date that is supplied relatively (yesterday, 2 days ago) #4

icyflame opened this issue May 22, 2016 · 2 comments

Comments

@icyflame
Copy link
Owner

This has a lot of possibility.

Can start with simple strings like yesterday, #{n} days ago, but can also be improved to last Wednesday, a week ago (which should resolve to 7 days ago)

This should be implemented as a separate module: (if one doesn't already exist)

  • which takes one argument and the present Date() (defaults to Date() if not supplied)
  • parses the argument, returns a Date() object.
  • can be given conditionals such must be in the past, must be in the future, etc.
@mrahul17
Copy link
Contributor

I have not tested this, but I am thinking along these lines. Have a look and comment.

  date_options = {
    "today" : 0,
    "yesterday" : 24*60*60*1000 ,
    "day" : 24*60*60*1000,
    "week" : 7*24*60*60*1000,
    "weeks" :7*24*60*60*1000 
  }

  if (expenseObject.date) {
    var dateRe = /(\d{4})-(\d{2})-(\d{2})/;
    var matchObject = expenseObject.date.match(dateRe);
    if (!matchObject) {
      var datesplit = expenseObject.date.split(" ")
      if (isNaN(parseInt(datesplit[0]))) {
        if(date_options.slice(0,2).indexOf(datesplit[0])==-1) {
          throw new Error(Consts.MSG_INVALID_DATE);
        } else {
          expenseObject.date = require('date-format').asString('yyyy-MM-dd', new Date().setTime((new Date()).getTime()-datesplit[0]));
        }
      } else {
        if(date_options.slice(2).indexOf(datesplit[1])==-1) {
          throw new Error(Consts.MSG_INVALID_DATE);
        } else {
          expenseObject.date = require('date-format').asString('yyyy-MM-dd', new Date().setTime((new Date()).getTime()-datesplit[1])); 
        }
      }

    }

  } else {
    expenseObject.date = require('date-format').asString('yyyy-MM-dd', new Date());
  }

@icyflame
Copy link
Owner Author

@light94 Thanks a lot for that snippet of code. I couldn't really get the nested if statements, but I did understand your thinking behind this.

I have setup everything both in this module terminal-wallet and also, I have created a new module relative-date-reverse, which will house the code for solving this problem.

I have used some of the code above, and cleaned it up, and used it to implement basic stuff like yesterday, day before yesterday, etc)

I hope you can find some time to help me out on that module! (index.js)

icyflame pushed a commit that referenced this issue Jun 12, 2016
Setup the basic stuff for #4, once the module can be used, it can be
immediately re-used here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants