Is it possible to define time?
#1

Title basically says it all, could I define like a second, minute, hour, day, month, year, etc? Then somehow incorporate that into a command that shows the date? Any help would be appreciated.
Reply
#2

You mean this? I created this function before, or you could simply use getdate(..) and gettime(..)

pawn Код:
stock GetTodaysDate()
{
    new
        dString[ 18+2 ],
        Date[ 6 ]
    ;
            //Year      //Month     //Day
    getdate(Date[ 0 ], Date[ 1 ], Date[ 2 ]);
            //Hour      //Minutes   //Seconds
    gettime(Date[ 3 ], Date[ 4 ], Date[ 5 ]);

    format(dString, sizeof (dString), "%d/%d/%d %d:%d:%d", Date[ 2 ], Date[ 1 ], Date[ 0 ], Date[ 3 ], Date[ 4 ], Date[ 5 ]);
    return dString;
}
Original Thread: http://forum.sa-mp.com/showpost.php?...&postcount=255
Reply
#3

Kind of like this, but how could I make this right here into a /time command using ZCMD?
Reply
#4

Quote:
Originally Posted by K9IsGodly
Посмотреть сообщение
Kind of like this, but how could I make this right here into a /time command using ZCMD?
This should work.

pawn Код:
CMD:time(playerid, params[])
{
    new string[ 30 ], Date[ 6 ]
    ;
            //Year      //Month     //Day
    getdate(Date[ 0 ], Date[ 1 ], Date[ 2 ]);
            //Hour      //Minutes   //Seconds
    gettime(Date[ 3 ], Date[ 4 ], Date[ 5 ]);
    format( string, sizeof( string ), "Todays Time: %i:%i", Date[3], Date[4]), SendClientMessage( playerid, -1, string );
    format( string, sizeof( string ), "Todays Date: %i:%i:%i", Date[ 0 ], Date[ 1 ], Date[ 2 ]), SendClientMessage( playerid, -1, string );
    return true;
}
Reply
#5

Quote:
Originally Posted by pds2k12
Посмотреть сообщение
This should work.

pawn Код:
CMD:time(playerid, params[])
{
    new string[ 30 ], Date[ 6 ]
    ;
            //Year      //Month     //Day
    getdate(Date[ 0 ], Date[ 1 ], Date[ 2 ]);
            //Hour      //Minutes   //Seconds
    gettime(Date[ 3 ], Date[ 4 ], Date[ 5 ]);
    format( string, sizeof( string ), "Todays Time: %i:%i", Date[3], Date[4]), SendClientMessage( playerid, -1, string );
    format( string, sizeof( string ), "Todays Date: %i:%i:%i", Date[ 0 ], Date[ 1 ], Date[ 2 ]), SendClientMessage( playerid, -1, string );
    return true;
}
Works perfectly, thanks man! One question though, is it possible for it to display the time like how for me right now it's 3:41 P.M. versus displaying it as 15:41 P.M.? So I guess what I mean is is there a way to have it print like 3:41 P.M. or if it was 3:41 A.M. it could detect that, too?
Reply
#6

Quote:
Originally Posted by K9IsGodly
Посмотреть сообщение
Works perfectly, thanks man! One question though, is it possible for it to display the time like how for me right now it's 3:41 P.M. versus displaying it as 15:41 P.M.? So I guess what I mean is is there a way to have it print like 3:41 P.M. or if it was 3:41 A.M. it could detect that, too?
Nope, because gettime and getdate gets the information from the server side not from the client side, unless you host the server with your own computer then it will print the time that your computer currently have.
Reply
#7

pawn Код:
CMD:time(playerid, params[])
{
    new string[ 30 ], Date[ 7 ]
    ;
            //Year      //Month     //Day
    getdate(Date[ 0 ], Date[ 1 ], Date[ 2 ]);
            //Hour      //Minutes   //Seconds
    gettime(Date[ 3 ], Date[ 4 ], Date[ 5 ]);
    format( string, sizeof( string ), "Todays Date: %i:%i:%i", Date[ 0 ], Date[ 1 ], Date[ 2 ]), SendClientMessage( playerid, -1, string );
    if(Date[ 3 ] >= 12 && Date[ 4 ] > 0)
    {
        Date [ 6 ] = Date[ 3 ] - 12;
        format( string, sizeof( string ), "Todays Time: %i:%i PM", Date[ 6 ], Date[ 4 ]), SendClientMessage( playerid, -1, string );
    }
    else if(Date[ 3 ] < 12 && Date[ 4 ] > 0)
    {
        format( string, sizeof( string ), "Todays Time: %i:%i AM", Date[ 3 ], Date[ 4 ]), SendClientMessage( playerid, -1, string );
    }
    return true;
}
this should work the way you want...

EDIT: Modified PDS2k12's code a little
Reply
#8

Quote:
Originally Posted by Danyal
Посмотреть сообщение
pawn Код:
CMD:time(playerid, params[])
{
    new string[ 30 ], Date[ 7 ]
    ;
            //Year      //Month     //Day
    getdate(Date[ 0 ], Date[ 1 ], Date[ 2 ]);
            //Hour      //Minutes   //Seconds
    gettime(Date[ 3 ], Date[ 4 ], Date[ 5 ]);
    format( string, sizeof( string ), "Todays Date: %i:%i:%i", Date[ 0 ], Date[ 1 ], Date[ 2 ]), SendClientMessage( playerid, -1, string );
    if(Date[ 3 ] >= 12 && Date[ 4 ] > 0)
    {
        Date [ 6 ] = Date[ 3 ] - 12;
        format( string, sizeof( string ), "Todays Time: %i:%i PM", Date[ 6 ], Date[ 4 ]), SendClientMessage( playerid, -1, string );
    }
    else if(Date[ 3 ] < 12 && Date[ 4 ] > 0)
    {
        format( string, sizeof( string ), "Todays Time: %i:%i AM", Date[ 3 ], Date[ 4 ]), SendClientMessage( playerid, -1, string );
    }
    return true;
}
this should work the way you want...

EDIT: Modified PDS2k12's code a little
A more compact version:

pawn Код:
CMD:time(playerid, params[])
{
    new string[ 30 ], Date[ 6 ]
    ;
            //Year      //Month     //Day
    getdate(Date[ 0 ], Date[ 1 ], Date[ 2 ]);
            //Hour      //Minutes   //Seconds
    gettime(Date[ 3 ], Date[ 4 ], Date[ 5 ]);
    format( string, sizeof( string ), "Todays Date: %i:%i:%i", Date[ 0 ], Date[ 1 ], Date[ 2 ]), SendClientMessage( playerid, -1, string );
    format( string, sizeof( string ), "Todays Time: %i:%i AM", Date[ 3 ]%12, Date[ 4 ]), SendClientMessage( playerid, -1, string );
    return true;
}
Reply
#9

Quote:
Originally Posted by king_hual
Посмотреть сообщение
A more compact version:

pawn Код:
CMD:time(playerid, params[])
{
    new string[ 30 ], Date[ 6 ]
    ;
            //Year      //Month     //Day
    getdate(Date[ 0 ], Date[ 1 ], Date[ 2 ]);
            //Hour      //Minutes   //Seconds
    gettime(Date[ 3 ], Date[ 4 ], Date[ 5 ]);
    format( string, sizeof( string ), "Todays Date: %i:%i:%i", Date[ 0 ], Date[ 1 ], Date[ 2 ]), SendClientMessage( playerid, -1, string );
    format( string, sizeof( string ), "Todays Time: %i:%i AM", Date[ 3 ]%12, Date[ 4 ]), SendClientMessage( playerid, -1, string );
    return true;
}
Your code will always show AM mate he still needs to switch statements with if condition
Reply
#10

Quote:
Originally Posted by Danyal
Посмотреть сообщение
Your code will always show AM mate he still needs to switch statements with if condition
My bad,

Код:
format( string, sizeof( string ), "Todays Time: %i:%i %s", Date[ 3 ]%12, Date[ 4 ], (Date[3] >= 12 ? ("PM") : ("AM"))), SendClientMessage( playerid, -1, string );
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)