Discuss Scratch

1117Hayden2
Scratcher
32 posts

How to Detect Time Zones?

So there currently isn't a code for detecting our own time zones on Scratch. I am working on an upcoming project, and it involves detecting my own time zone. Can anyone think of a solution? I need some help from you all.
deck26
Scratcher
1000+ posts

How to Detect Time Zones?

You can detect what the local time is compared to UTC and get a rough idea of where you are in the world but there will generally be multiple locations which match and you have to take into account any daylight savings adjustments (which would apply at different times of year for northern/southern hemispheres). So this is not as trivial as it may appear.
1117Hayden2
Scratcher
32 posts

How to Detect Time Zones?

Thanks for the suggestion. Well, I would actually need some actual code for that.
deck26
Scratcher
1000+ posts

How to Detect Time Zones?

The days since 2000 block's fractional part gives the UTC time - eg at 12 noon the fractional part is 0.5. So you can work out the hours by multiplying by 24 and taking the ‘floor’ of that value. The remaining fractional part gives you the minutes (multiply by 60) but can potentially be ignored unless you're looking at time zones which are not a whole number of hours different from UTC.

The current hour block gives the local time.
dspace1015
Scratcher
94 posts

How to Detect Time Zones?

So in scratch blocks it would look like this:
set [Time v] to (([floor v] of ([1440]*(days since 2000)))/(60)) //1440* gives minutes since 2000, dividing by 60 gives decimal hours since 2000 accurate to the minute, as most timezones are.
set [Timezone v] to ((((((current [hour v])+((current [minute v])/(60)))-(Time))+[12])mod(24))-[12]) //Subtract your time from UTC Hour calculated to get time zone difference.
This does not account for time zones beyond UTC+12 or UTC-12 but those time zones do not have that large of a population so it should work for most people. It should account for the slightly more common fractional time zones however because it accounts for the minute.

If you need one that should work in all scenarios and need date prediction beyond what scratch provides for the current time, check out my project Here inside the start calculator sprite there is a block called “Get Time Based on Seconds Since 2000” It works with the “!Time” variable replacing 86400*(days since 2000)

But this solution should work for your case.

Like @deck26 said though, these time zone detections only work in comparison to UTC. Daylight time is common in many countries that change time zones twice a year, so just be mindful of that when using your time zone detection.
1117Hayden2
Scratcher
32 posts

How to Detect Time Zones?

Here's another question for you all. So I need some playtesting. If I switch my time on my device, does the “days since 2000” counter change with it too? If it does, then does it still follow the UTC time?
kouryou118103
Scratcher
1000+ posts

How to Detect Time Zones?

I tested it. “Days since 2000” always seems to be UTC.
1117Hayden2
Scratcher
32 posts

How to Detect Time Zones?

Well, sometimes the time on scratch doesn't sync with the time on your display, especially if you've just recently changed the time. However, sometimes it does sync.
dspace1015
Scratcher
94 posts

How to Detect Time Zones?

(days since 2000)
Is always UTC based, so it is the same for everyone that uses it.
(current [hour v])
(current [minute v])
(current [second v])
are from your local time zone.
This is how we detect time zones in the first place is because we have a universal reference to compare our local time to.
As for the syncing issue, I don't remember experiencing it, but you shouldn't need to worry about it because most users won't be switching time zones anyway. It should reset to your local time after not that long, so I wouldn't worry about it.

Edit: I'm pretty sure (days since 2000) is linked to your system time, so it won't be exactly the same for everyone. Most peoples devices automatically update their time via the internet but If you set your time manually and leave your time zone the same, it may mess this up a little. So if you do more testing make sure you only change your system's time zone and not the actual hour. If you change the hour but not your time zone it might think you are living 1hr in the future rather than 1hr different time zone.

Last edited by dspace1015 (Dec. 25, 2025 04:35:37)

kouryou118103
Scratcher
1000+ posts

How to Detect Time Zones?

According to the source code(https://github.com/scratchfoundation/scratch-vm/blob/develop/src/blocks/scratch3_sensing.js#L252 ),
it always returns a UTC-based value.
daysSince2000 () {
    const msPerDay = 24 * 60 * 60 * 1000;
    const start = new Date(2000, 0, 1); // Months are 0-indexed.
    const today = new Date();
    const dstAdjust = today.getTimezoneOffset() - start.getTimezoneOffset();
    let mSecsSinceStart = today.valueOf() - start.valueOf();
    mSecsSinceStart += ((today.getTimezoneOffset() - dstAdjust) * 60 * 1000);
    return mSecsSinceStart / msPerDay;
}

Last edited by kouryou118103 (Dec. 25, 2025 04:37:33)

Powered by DjangoBB