JS call to check real world time?

Is there any way for MV to see the current real world time of the device it's running on?


I'd like to have certain elements of the game change depending upon what hour of the day it is.
calling the Date() method, will return a string with the day, Month, Year, Time, Time zone, in that Order, the time is Displayed in Hours:Minutes:Seconds. In order to just get the time, you could use something like 

 

Code:
var currentTime = Date().split( " " )[ 4 ]; //splits date into an array, then gets the 4th element in the array( which is the time ).
calling the Date() method, will return a string with the day, Month, Year, Time, Time zone, in that Order, the time is Displayed in Hours:Minutes:Seconds. In order to just get the time, you could use something like 

 



var currentTime = Date().split( " " )[ 4 ]; //splits date into an array, then gets the 4th element in the array( which is the time ).



Oooooooooh.....


And how would I load that into a variable? I'd usually use something like:


$gameVariables.setValue(13,Graphics._fpsMeter.fps);


But that won't work here...


Ideally I'd like to be able to load the hours, minutes, and seconds into different variables, even.
Well, the value that'd be returned would look something like this "12:24:30"(hour:minute:seconds);

so if you needed to get the number value of each unit of time you'd do something like this.

 

Code:
//Assuming our time is actaully "12:24:30"
var time = Date().split( " " )[ 4 ].split( ":" ).map( Number );//time now displays like so [12,24,30]
//the next step would be to set our variables based upon the time in our array.
$gameVariables.set(13, time[ 0 ] );//this would set our hours value( 12 ).
$gameVariables.set(14, time[ 1 ] );//this would set our minutes value( 24 ).
$gameVariables.set(15, time[ 2 ] );//this would set our seconds value( 30 ).
$gameVariables.setValue(13, new Date().getHours());
$gameVariables.setValue(14, new Date().getMinutes());
$gameVariables.setValue(15, new Date().getSeconds());
$gameVariables.setValue(16, new Date().getMilliseconds());


For the situation you described, the above code would be all you need.


If you want to compare those times later, it would be better to do this:


$gameVariables.setValue(13, new Date().getTime());


This will store the exact time in the variable. When you want to compare it, you can store the new time in another variable:


$gameVariables.setValue(14, new Date().getTime());


And to compare, you can even use a regular conditional branch. If you do variable 14 - variable 13, you'll get the number of milliseconds between them.
Thanks both! This is perfect and actually may very well solve another issue I was having previously. 😀

Original thread: https://forums.rpgmakerweb.com/threads/js-call-to-check-real-world-time.73892/