From Test-Scratch-Wiki
With Scratch 2.0's time 椭圆形积木 capabilities, displaying the time or creating a 字串 of the current time is possible. However, using the (目前時間的 [分 v])
block displays a 24-hour clock, excluding "a.m." and "p.m.", and also has other disabilities. This tutorial shows how to properly display the time to one's liking as an alternative to joining the basic time capabilities provided by the 目前时间的 () block.
Programming
One of the issues with the current () block is that minutes and seconds are not zero-padded, meaning they do not have a "0" before single-digit values. For example, time may be displayed realistically as:
1:04:07 p.m.
Meaning it is 1 hour, 4 minutes, and 7 seconds past 12:00 p.m. However, joining the strings together, Scratch would normally display the time as:
13:4:7
To work around this, 变量s can be used to designate the realistically correct value for each time unit, and then they can be joined together as a string. To fix the 24 hour issue, one can add -12 to the hour if it reaches a certain time. For the following script, assume:
(hour)
is the variable used to display the current hour(minute)
is the variable used to display the current minute(second)
is the variable used to display the current second(half)
is the variable used to display if the time is before or after 12:00 p.m.(time)
is the variable used to display the united time
In the script below, for example, if you do not want to include "seconds" or any other time unit, the instances of it as well as related blocks must be removed
定義 separate (string) into words 變數 [letter# v] 設為 (1) // this variable will represent an individual letter in "string" 刪除第 [全部 v] 項 \( [words v] \) // clears all the words 新增項目 [] \( [words v] \) // adds a blank item as the basis for the first word 重複 (字串長度\( (string) \)) 次 如果 <(字串中第 (letter#) 字\( (string) \)) = [ ]> 那麼 新增項目 [] \( [words v] \) // adds a new, blank item to the list if the current letter is a space 替換第 (last v) 項於 [words v] 成 (字串組合 (清單第 (last v) 項項目\( [words v] \) :: list) 和 (字串中第 (letter#) 字\( (string) \))) // adds the letter to the current word end // note that a space is inserted into this string, not nothing 變數 [letter# v] 改變 (1) // moves on to the next letter end // repeats the amount of letters in the string