← Back to How-to's

How to Display the Full Month and Year in Spark AR (Full Code Included)

#
Spark AR

Are you looking to add a dynamic and stylish date display to your Instagram filters? Whether it’s for a special event or just to add a professional touch to your creations, displaying the current month and year in your filter can be a game-changer. Here’s a step-by-step guide on how you can achieve this using Meta's Spark AR Studio.

Step 1: Set Up Your Text Objects

Begin by opening your project in Spark AR Studio and adding two text objects to your scene. Name them monthText and yearText for easy identification.

Step 2: Add a Script for Dynamic Text

Dynamic text requires a bit of scripting, but don't worry, we've got you covered. In the Assets panel, add a new script by clicking + Add Asset and then Script.

Step 3: Write the Date Script

Inside your script, you'll write a function that retrieves the current date and formats it to display only the month and year. Here's a simple script that does just that:

// Load in the required modules
const Scene = require('Scene');
const Time = require('Time');
const Diagnostics = require('Diagnostics'); // Include Diagnostics for debugging

// Enable async/await in JS [part 1]
(async function() {
    // Use findFirst to ensure the objects are available when the script runs
    const [monthText, yearText] = await Promise.all([
        Scene.root.findFirst('monthText', {recursive: true}),
        Scene.root.findFirst('yearText', {recursive: true})
    ]);

    // Array of month names for manual formatting
    const monthNames = [
        "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December"
    ];

    // Function to set the current date
    function setCurrentDate() {
        const now = new Date();

        // Debugging: Log the current date to the console
        Diagnostics.log('Current Date: ' + now.toString());

        // Manually get the month and year
        const month = monthNames[now.getUTCMonth()]; // getUTCMonth returns a zero-indexed month
        const year = now.getUTCFullYear().toString(); // getUTCFullYear returns the 4-digit year

        // Debugging: Log the formatted month and year to the console
        Diagnostics.log('Formatted Month: ' + month);
        Diagnostics.log('Formatted Year: ' + year);

        // Set the month and year text separately
        monthText.text = month;
        yearText.text = year;
    }

    // Call the function to set the date when the script starts
    setCurrentDate();

    // Update the date every minute (60000 milliseconds)
    Time.setInterval(setCurrentDate, 60000);
})();

Step 4: Style Your Text

Customize the font, size, color, and other properties of your text objects to match the aesthetic of your filter.

Step 6: Debug

We have added some debugging lines to our JS which will cause your console to display the following:

Step 6: Test and Publish

Always test your filter in the Spark AR Studio simulator and on your device to ensure everything is working smoothly. Once you're happy with it, follow the Spark AR Hub's guidelines to publish your filter.

And there you have it! You're now ready to add a dynamic date display to your Instagram filters. This feature not only elevates the user experience but also adds a layer of professionalism to your creative offerings. For more tips and tricks on digital marketing and branding, stay tuned to Mattered’s blog!

Want to keep reading?