Search code examples
javascriptjqueryparsingdatedatejs

is there any way to parse date in string using Datejs?


I came across Datejs recently and found it very useful. However I could not figure out if there is a way to parse a string and extract only date part from it using the same.

For example, if there is a string >> "I will start exercise from next Monday."

Then it should parse the string, extract 'next monday' from it and convert it into date and give me the result.

How can it be implemented?

Thanks :)


Solution

  • You can write some RegEx for that. That would be the easiest way. 'next' will be a keyword in that case. A simple function can lookup the current weekday and return the date of the next monday. Should not that complicated.

    Edit:

    You can do something like this:

    var pattern = /^([\w\W.]*)(next){1}([\sa-zA-Z]*)/;
    while (result = pattern.exec(yourTextVariable) != null){
       // read the data as you need from the result array
    }
    

    The Pattern above expecting a white space then the keyword next and will red the next word if it only has alpha-letters. (please note that the RegEx is untested and may need some refactoring to fit your needs. You may take a look at this page to do this: javascriptkit.com)