Solved How to parse a text format of Date/Time

  • 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!

  • LOOKING FOR A VERSION OF SKRIPT?

    You can always check out skUnity Downloads for downloads and any other information about Skript!

Status
Not open for further replies.

Cybers_

Member
Feb 21, 2017
18
2
0
24
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
 
I'm trying to use '"%now%" parsed as text'
You don't need to parse "%now%" as text because anything within quotes is already 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
I got this to work:
code_language.skript:
on script load:

    set {_now} to "%now%"
    send "Now is: '%{_now}%'" to console
 
    set {_date} to {_now} parsed as date
    send "Date is: '%{_date}%'" to console
Although, the hours got messed up. No idea why.
qRz0lWK.png


http://njol.ch/projects/skript/doc/classes/#date
 
Last edited by a moderator:
  • Like
Reactions: Amicus
You don't need to parse "%now%" as text because anything within quotes is already text.


I got this to work:
code_language.skript:
on script load:

    set {_now} to "%now%"
    send "Now is: '%{_now}%'" to console
 
    set {_date} to {_now} parsed as date
    send "Date is: '%{_date}%'" to console
Although, the hours got messed up. No idea why.
qRz0lWK.png


http://njol.ch/projects/skript/doc/classes/#date

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".
 
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".
So parse them as dates like I did in my post. :emoji_stuck_out_tongue:
 
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).
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.
[doublepost=1489113918,1489113498][/doublepost]
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.
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.
[doublepost=1489114655][/doublepost]
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.
[doublepost=1489113918,1489113498][/doublepost]
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.
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
Reactions: Rezz
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.
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
Since the values are in unix time, you can convert them directly into skript timespans (or integers) using:
code_language.skript:
set {_timespan} to "%{_unix-time}% seconds" parsed as timespan
Like in this example:
code_language.skript:
on script load:

    #  Getting the current time as unix time.
  
    set {_unix-time} to convert date now to unix date
    send "<cyan>  Now: '%now%' Unix time: '%{_unix-time}%'" to console
  
    #  Because unix time is stored as seconds, it can be parsed as a timespan with:
    #  '<unix time> seconds' parsed as timespan
  
    set {_timespan} to "%{_unix-time}% seconds" parsed as timespan
    send "<cyan>  Timespan: '%{_timespan}%'" to console
  
    #
    #   Waiting 5 seconds to compare times...
    #
  
    send "<white>  Waiting 5 seconds..." to console
    wait 5 seconds
  
    #   Getting the new current (future) time.
  
    set {_future-unix-time} to convert date now to unix date
    send "<light cyan>  Now (future): '%now%' Unix time: '%{_future-unix-time}%'" to console
  
    set {_future-timespan} to "%{_future-unix-time}% seconds" parsed as timespan
    send "<light cyan>  Timespan (future): '%{_future-timespan}%'" to console
  
    #   Finding the difference between the old time and the current time:
  
    subtract {_timespan} from {_future-timespan}
    send "<light green>  Time difference: %{_future-timespan}%" to console

BHUg5Wk.png


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

code_language.skript:
if {_future-timespan} is greater than 2 seconds:
        send "<white>  Yes!!!" to console
Y5XYK8E.png
 
Last edited by a moderator:
  • Like
Reactions: Rektb
Since the values are in unix time, you can convert them directly into skript timespans (or integers) using:
code_language.skript:
set {_timespan} to "%{_unix-time}% seconds" parsed as timespan
Like in this example:
code_language.skript:
on script load:

    #  Getting the current time as unix time.
 
    set {_unix-time} to convert date now to unix date
    send "<cyan>  Now: '%now%' Unix time: '%{_unix-time}%'" to console
 
    #  Because unix time is stored as seconds, it can be parsed as a timespan with:
    #  '<unix time> seconds' parsed as timespan
 
    set {_timespan} to "%{_unix-time}% seconds" parsed as timespan
    send "<cyan>  Timespan: '%{_timespan}%'" to console
 
    #
    #   Waiting 5 seconds to compare times...
    #
 
    send "<white>  Waiting 5 seconds..." to console
    wait 5 seconds
 
    #   Getting the new current (future) time.
 
    set {_future-unix-time} to convert date now to unix date
    send "<light cyan>  Now (future): '%now%' Unix time: '%{_future-unix-time}%'" to console
 
    set {_future-timespan} to "%{_future-unix-time}% seconds" parsed as timespan
    send "<light cyan>  Timespan (future): '%{_future-timespan}%'" to console
 
    #   Finding the difference between the old time and the current time:
 
    subtract {_timespan} from {_future-timespan}
    send "<light green>  Time difference: %{_future-timespan}%" to console

BHUg5Wk.png


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

code_language.skript:
if {_future-timespan} is greater than 2 seconds:
        send "<white>  Yes!!!" to console
Y5XYK8E.png

doesnt work
 
Status
Not open for further replies.