In this blog post, we’ll explain how you can get the current date in Google Sheets and format it as HTML. Being able to manipulate dates and times is a crucial skill when working with spreadsheets, especially when you’re working with time-sensitive data.
Getting the Current Date in Google Sheets
Google Sheets provides a simple function to fetch the current date: TODAY(). This function does not take any arguments, and it returns the current date according to your computer’s system date settings.
To get the current date, follow the steps below:
- Click on the cell where you want to display the current date.
- Type in =TODAY() and press the ‘Enter’ key on your keyboard.
It’s as simple as that! The specified cell will now display the current date. It’s important to note that Google Sheets will automatically update this date whenever you reopen the file or make changes in the sheet, ensuring the date displayed is always current.
Formatting the Output as HTML
The next step is to format this date as HTML. Google Sheets does not natively support HTML output, but we can craft a workaround using Google Apps Script, Google Sheet’s built-in JavaScript-based scripting language.
Here’s a simple script that gets the current date in Google Sheets and formats it as an HTML string:
function getHTMLFormattedDate() { // Get the current date. let date = new Date(); // Format the date as dd-mm-yyyy. let formattedDate = Utilities.formatDate(date, Session.getScriptTimeZone(), "dd-MM-yyyy"); // Return the date formatted as an HTML string. return `<p>${formattedDate}</p>`; }
This script uses the date object to get the current date, formats it using the Utilities.formatDate() method, and then returns it as an HTML string enclosed in paragraph tags.
To use this script:
- Click on ‘Extensions’ -> ‘Apps Script.’ This will open the Apps Script editor.
- Copy and paste the provided script into the editor.
- Save and close the editor.
- To call this function from your Google Sheets, type =getHTMLFormattedDate() in a cell and press ‘Enter’.
And there you have it! You’ve successfully retrieved the current date in Google Sheets and formatted it as HTML. Remember, you can customise this script to suit your specific data needs, such as different date formats and HTML structures.
Thanks for reading. Be sure to check back for more tips and tricks for working efficiently with Google Sheets!