get day month and year from string
#1

i have string

04.06.2015

i want to get day month and year from this string (04.06.2015 ) and store it to variables like

day = 04
month = 06
year = 2015

any help
Reply
#2

Код:
// Useful function to split a string by symbol
// Usage: 
// new destination[3][16];
// split( "my.source.string", destination, '.', 3 );
// Now destination is:
// [0] => "my"
// [1] => "source"
// [2] => "string"
stock split(const strsrc[], const strdest[][], delimiter, destlen)
{
    new i, li;
    new aNum;
    new len;
    while(i <= strlen(strsrc))
    {
        if(strsrc[i] == delimiter || i == strlen(strsrc))
        {
            len = strmid(strdest[aNum], strsrc, li, i, 128);
            strdest[aNum][len] = 0;
            li = i+1;
            aNum++;
        }
        i++;
    }
	if (aNum < destlen) {
		for (new j=aNum; j < destlen; j++) {
			strdest[j][0] = 0;
		}
	}
    return 1;
}

stock stringToDate( const dateAsString[], &day, &month, &year ) {
	new dest[3][5];
	split( dateAsString, dest, 3 );
	day = strval( dest[0] );
	month = strval( dest[1] );
	year = strval( dest[2] );
}

// How to use:
...
new day, month, year;
stringToDate( "11.06.2015", day, month, year );
// now day == 11, month == 6, year == 2015
...
Reply
#3

Or simply use sscanf:
pawn Код:
sscanf(datestr, "p<.>ddd", day, month, year);
Reply
#4

Thanks @prineside & @Vince

Solved
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)