1. Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

  2. LOOKING FOR A VERSION OF SKRIPT?

    You can always check out our Wiki for downloads and any other information about Skript!

Dismiss Notice
This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved How to parse a text format of Date/Time

Discussion in 'Skript' started by Cybers_, Mar 9, 2017.

Thread Status:
Not open for further replies.
  1. Cybers_

    Cybers_ Member

    Joined:
    Feb 21, 2017
    Messages:
    18
    Likes Received:
    2
    I'm trying to use '"%now%" parsed as text' from a database back into the game to use for 'difference between <x> and <y>', I've tried parsing as 'date', 'date and time', 'time', but nothing seems to work

     
  2. Best Answer:
    Post #6 by ShaneBee, Mar 10, 2017
  3. ShaneBee

    Moderator Resource Staff Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,185
    Likes Received:
    215
    You don't need to parse "%now%" as text because anything within quotes is already text.

    I got this to work:
    Code (Skript):
    1. on script load:
    2.  
    3.     set {_now} to "%now%"
    4.     send "Now is: '%{_now}%'" to console
    5.  
    6.     set {_date} to {_now} parsed as date
    7.     send "Date is: '%{_date}%'" to console
    Although, the hours got messed up. No idea why.
    [​IMG]

    http://njol.ch/projects/skript/doc/classes/#date
     
    #2 ShaneBee, Mar 9, 2017
    Last edited by a moderator: Mar 9, 2017
    • Like Like x 1
  4. ShaneBee

    Moderator Resource Staff Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,185
    Likes Received:
    215
    hmm, I'm trying to use it for a daily bonus thing. It works fine with variables, but because I'm saving it as "%now%" in the database, then when I load in the data I can't use it as a time to find the "difference between now and X".
     
  5. ShaneBee

    Moderator Resource Staff Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,185
    Likes Received:
    215
    So parse them as dates like I did in my post. :emoji_stuck_out_tongue:
     
  6. ShaneBee

    Moderator Resource Staff Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,185
    Likes Received:
    215
  7. ShaneBee

    Moderator Resource Staff Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,185
    Likes Received:
    215
    I recommend getting SkUtilities and storing unix time in your database instead of a the output of "%now%": https://skunity.com/search?search=unix
    Unix time is stored in seconds, so it should be much easier to manage (no date format conversions, etc).
     
  8. ShaneBee

    Moderator Resource Staff Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,185
    Likes Received:
    215
    Ah, that makes a lot more sense to use unix time, never though of that. Just tested it and it works great, thanks for your help.
    --- Double Post Merged, Mar 10, 2017, Original Post Date: Mar 10, 2017 ---
    Is there a way to parse the result to unix? Doesn't seem to work if I use the string result to convert to a date format.
    --- Double Post Merged, Mar 10, 2017 ---
    Actually, I think a 24 hour difference in unix is 86400, so I should be able to just subtract the last bonus from now as unix, (parse them both as integers?) and then check if the difference is > 86400
     
    • Like Like x 1
  9. ShaneBee

    Moderator Resource Staff Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,185
    Likes Received:
    215
    Since the values are in unix time, you can convert them directly into skript timespans (or integers) using:
    Code (Skript):
    1. set {_timespan} to "%{_unix-time}% seconds" parsed as timespan
    Like in this example:
    Code (Skript):
    1. on script load:
    2.  
    3.     #  Getting the current time as unix time.
    4.  
    5.     set {_unix-time} to convert date now to unix date
    6.     send "<cyan>  Now: '%now%' Unix time: '%{_unix-time}%'" to console
    7.  
    8.     #  Because unix time is stored as seconds, it can be parsed as a timespan with:
    9.     #  '<unix time> seconds' parsed as timespan
    10.  
    11.     set {_timespan} to "%{_unix-time}% seconds" parsed as timespan
    12.     send "<cyan>  Timespan: '%{_timespan}%'" to console
    13.  
    14.     #
    15.     #   Waiting 5 seconds to compare times...
    16.     #
    17.  
    18.     send "<white>  Waiting 5 seconds..." to console
    19.     wait 5 seconds
    20.  
    21.     #   Getting the new current (future) time.
    22.  
    23.     set {_future-unix-time} to convert date now to unix date
    24.     send "<light cyan>  Now (future): '%now%' Unix time: '%{_future-unix-time}%'" to console
    25.  
    26.     set {_future-timespan} to "%{_future-unix-time}% seconds" parsed as timespan
    27.     send "<light cyan>  Timespan (future): '%{_future-timespan}%'" to console
    28.  
    29.     #   Finding the difference between the old time and the current time:
    30.  
    31.     subtract {_timespan} from {_future-timespan}
    32.     send "<light green>  Time difference: %{_future-timespan}%" to console
    [​IMG]

    And because the results are in timespans, you can check if it's "greater than 1 day" or "greater than 30 seconds", etc.

    Code (Skript):
    1. if {_future-timespan} is greater than 2 seconds:
    2.         send "<white>  Yes!!!" to console
    [​IMG]
     
    #8 ShaneBee, Mar 10, 2017
    Last edited by a moderator: Mar 10, 2017
    • Like Like x 1
  10. ShaneBee

    Moderator Resource Staff Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,185
    Likes Received:
    215
    doesnt work
     
  11. ShaneBee

    Moderator Resource Staff Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,185
    Likes Received:
    215
    It does work, as I've shown with my example.
     
  12. ShaneBee

    Moderator Resource Staff Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,185
    Likes Received:
    215
    I found out the issue, it was because I changed some stuff and I had to parse the current UNIX into a number to be able to subtract it.
     
Thread Status:
Not open for further replies.

Share This Page

Loading...