User Agents

Web browsers and everyday web tech, explained

Override Browser Date with Puppeteer: A Complete Guide

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To override the browser date with Puppeteer, you must use the page.evaluateOnNewDocument() method to redefine the global Date object before any page scripts execute. This allows you to simulate a specific point in time, which is essential for testing time-sensitive features like countdown timers, expiration dates, or region-specific scheduling without changing the system clock of your host machine.

What You'll Need

  • Node.js installed on your system.
  • The puppeteer package installed via npm.
  • A basic understanding of JavaScript and asynchronous functions.
  • A target URL or local HTML file for testing the date override.

Step-by-Step

  1. Initialize Puppeteer: Start by requiring the Puppeteer module and launching a browser instance. It is recommended to use headless: false during initial development so you can visually verify the date change on the page.
  2. Define Your Target Date: Create a variable containing the timestamp or the date string you wish to simulate. Using a Unix timestamp (milliseconds) is generally the most reliable method for JavaScript's Date object.
  3. Implement evaluateOnNewDocument: Call page.evaluateOnNewDocument(). This function is critical because it injects a script into every new document created by the browser before any other scripts run. If you use a standard page.evaluate(), the page's original scripts may have already captured the real system time.
  4. Override the Date Constructor: Inside the evaluation function, redefine window.Date. You cannot simply assign a value to a date; you must replace the Date constructor with a custom function that returns a new Date object based on your fixed timestamp.
  5. Handle Date Methods: Ensure your override accounts for both the constructor (new Date()) and the static method (Date.now()). If you only override the constructor, calls to Date.now() will still return the actual current time.
  6. Navigate to the Page: Use page.goto(url) to load your website. Because the override was set via evaluateOnNewDocument, the website will perceive the fake date from the moment it begins loading.
  7. Verify the Result: Use page.evaluate(() => new Date().toString()) to log the date currently being used by the browser to the console to confirm the override was successful.

Common Mistakes

  • Using page.evaluate() instead of evaluateOnNewDocument(): Standard evaluation happens after the page has loaded. Many websites determine the date immediately upon load, meaning your override will happen too late to affect the initial page state.
  • Forgetting Date.now(): Many modern frameworks use Date.now() for performance and simplicity. If you only override new Date(), these functions will still leak the real system time.
  • Timezone Mismatches: Overriding the date does not automatically override the browser's timezone. If your application logic relies on getTimezoneOffset(), the date may appear shifted depending on the local settings of the machine running Puppeteer.
  • Hardcoding Strings: Passing date strings can lead to parsing errors across different browser versions. Using numeric timestamps is the safest approach.

Tips

  • Dynamic Time Progression: If you need to test a timer that counts down, you can create a variable in the browser context that increments by a set amount every time Date.now() is called, simulating the passage of time.
  • Use ISO Strings: When defining your target date in Node.js, use new Date('2025-12-25T12:00:00Z').getTime() to ensure you are working with a precise UTC timestamp.
  • Combine with Timezone Emulation: Use page.emulateTimezone('America/New_York') alongside your date override to ensure both the calendar date and the local clock are consistent.

Troubleshooting

If the date is not overriding as expected, first check the browser console. If you see errors regarding the Date constructor, ensure your override function is returning a valid Date object and not just a string or number.

If the page seems to know the real time despite the override, the site might be fetching the time from a server-side API (Network Time Protocol) rather than relying on the client-side browser clock. In this case, no amount of Puppeteer date overriding will work, as the time is not coming from the browser. You would instead need to mock the API response using page.setRequestInterception(true).

If the date is off by a few hours, check your timezone settings. The Date object in JavaScript is inherently tied to the system's local timezone unless explicitly handled as UTC.

Frequently Asked Questions

Does this change the system clock of my computer?

No. This method only modifies the JavaScript environment within the Chromium instance controlled by Puppeteer. Your operating system's clock remains unchanged.

Will this work for dates in the future?

Yes, you can set the date to any valid timestamp, including dates far in the future or in the past.

Can I change the date while the page is already open?

Not easily. Because the Date object is often cached or used during the initial load, evaluateOnNewDocument is the standard. To change it mid-session, you would need to overwrite the global Date object again via page.evaluate(), but this may not affect scripts that have already stored the initial date in a local variable.

Does this affect the HTTP request headers?

No. The Date header in HTTP requests is generated by the browser's network layer or the server, not the JavaScript Date object. This override only affects client-side JavaScript execution.

Is this method compatible with Headless mode?

Yes, this works identically in both headful and headless modes.