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

String Array Input & String Input (+Solution) #95

Open
PAPIONbit opened this issue May 6, 2020 · 0 comments
Open

String Array Input & String Input (+Solution) #95

PAPIONbit opened this issue May 6, 2020 · 0 comments

Comments

@PAPIONbit
Copy link

PAPIONbit commented May 6, 2020

Hi dear Babakhani
First, thanks for this useful library.

Second:
I have an String input like this:
1399-02-10
It should be Parse from value of a <Input type="text"...
So sure the input value is String.
I need to set it on persianDate() but
There is no generic way for that.

Expected Behavior

  • A) I can set it as an array like this :
    var d = new persianDate(Input.value.split('-'));

or

  • B) I can set it as a standard time string value like 1399-02-01T22:10:11
    var d = new persianDate(Input.value);

Current Behavior

  • A) It will not respond or It will show a wrong date (In older version). because the format of the array is String.

or

  • B) Almost no option.
    Note: The Date can do it. var d = new Date('2019-10-11T20:10:00');

Possible Solution

A)
Instead of every time converting values to string, just convert it in the main function.
By changing the setup function to this:

_createClass(PersianDateClass, [{
    key: 'setup',
    value: function setup(input) {
        // Convert Any thing to Gregorian Date
        if (TypeChecking.isDate(input)) {
            this._gDateToCalculators(input);
        } else if (TypeChecking.isArray(input)) {


        // Removing invalid detector & Adding parseInt() for every input
            this.algorithmsCalc([parseInt(input[0]), input[1] ? parseInt(input[1]) : 1, input[2] ? parseInt(input[2]) : 1, input[3] ? parseInt(input[3]) : 0, input[4] ? parseInt(input[4]) : 0, input[5] ? parseInt(input[5]) : 0, input[6] ? parseInt(input[6]) : 0]);



        } else if (TypeChecking.isNumber(input)) {
            var fromUnix = new Date(input);
            this._gDateToCalculators(fromUnix);
        }

...
B)
In next
Accepting strings that have possible characters by a code like this:

        // instance of pDate
        else if (input instanceof PersianDateClass) {
                this.algorithmsCalc([input.year(), input.month(), input.date(), input.hour(), input.minute(), input.second(), input.millisecond()]);
            }
            // ASP.NET JSON Date
            else if (input && input.substring(0, 6) === '/Date(') {
                    var fromDotNet = new Date(parseInt(input.substr(6)));
                    this._gDateToCalculators(fromDotNet);


                  // String value
		} else if (RegExp(/[\\\/T\-\_\.\:\s\,\;\"\'\(\)\[\]\!\+]/g).test(String(input))) {
			input=String(input).replace(/[\u0660-\u0669\u06f0-\u06f9]/g,function (c){return c.charCodeAt(0)&0xf;}).replace(/[\\\/T\-\_\.\:\s\,\;\"\'\(\)\[\]\!\+]+/g,'-').split('-');
			this.algorithmsCalc([parseInt(input[0]), input[1] ? parseInt(input[1]) : 1, input[2] ? parseInt(input[2]) : 1, input[3] ? parseInt(input[3]) : 0, input[4] ? parseInt(input[4]) : 0, input[5] ? parseInt(input[5]) : 0, input[6] ? parseInt(input[6]) : 0]);



                } else {
                    var now = new Date();
                    this._gDateToCalculators(now);
                }
    }

Explain:
Finding any possible characters that can be in date formats like:

1382/11/10 1357-11-12 "20'20 '1389 12 12' ۱۳۶۹-۱۰-۲۱

Detecting by RegEx, if is there, convert all (Persian/Arabic number to English &) All other characters to Dash and also removing double dashes.
Then split them in an array by split()

Sure my coding is not perfect on your library but i think something close to this can be useful.

Best Regards.

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

1 participant