Need to increment before checking the maximum value allowed by the variable
Use correct size for substr mask. This is academic only, as the current timestamp returned by a date object will already have 8 digits when converted to hex (this is the reason why the current code works