date
Description
Returns a formatted date.
string date( string format [, int timestamp ])
Returns the date, formatted according to the rules of Java's SimpleDateFormat class. See the class documentation for the full list of letter codes; here are the most useful:
Letter | Date or Time Component | Examples |
---|---|---|
y | Year | 2012; 12 |
M | Month in year | July; Jul; 07 |
d | Day in month | 10 |
E | Day in week | Tuesday; Tue |
a | AM/PM marker | PM |
H | Hour in day (0-23) | 0 |
h | Hour in am/pm (1-12) | 12 |
m | Minute in hour | 30 |
s | Second in minute | 55 |
Pattern letters are usually repeated. The number of repetitions determines their presentation, add more or less repetitions to get more or less abbreviated names. Example of a day of the week:
{date("E")} --> Mon
{date("EEEE")} --> Monday
By default, the current date, or the send date of the message, is what is used.
{date('MMM dd, yyyy')} ---> Jul 30, 2012 {date('MM/dd/yyyy')} ---> 07/30/2012 {date('MM/dd/yy')} ---> 07/30/12
If you pass a second timestamp
parameter as a UNIX timestamp, it will format the date for that timestamp.
See the time( )
function for a way to generate such timestamps, for example:
{date('MMM dd, yyyy', time('Jul 15, 2012 +1 week'))} ---> Jul 22, 2012
In addition, you can use the date( ) dynamically in an email with the time( ). For example:
{date("MMM dd, yyyy", time("+1 day"))} ---> tomorrow's date
Examples
Convert UNIX time to date when pulling from a data feed:
{date('MMM dd, yyyy', content[0].date)}
If you are using UNIX time with a decimal, consider standardizing the format by wrapping the data-feed placeholder 'content[0].date' with the int() function.
Show Today’s Date in Reader-Friendly Format
Use Case: Show the current date in an email body/subject line in any desired format. Note that this also uses the time() function.
Zephyr:
Today's date is {date('MMMM dd, yyyy',time("now"))}
Output:
Today’s date is August 01, 2016.
Explanation:
This script uses the time() function to return the UNIX timestamp for the current date and time, then uses date() to convert that timestamp to the common format of Month Day, Year.
Format Date from a Content Feed
Use Case: You want to pass and display the publish date and time of an article from a Content Feed. Since the sytem automatically adds this parameter as a timestamp to your content, you can format simply with the data() function.
Zephyr:
Your top story for the day:<br/>
<a href="{content[0].url}">{content[0].title}</a> published on {date("MMMM dd, yyyy", content[0].date)} at {date("hh:mm:ss aa", content[0].date)}
Output:
Your top story for the day: Stephen King's New Book is a Real Scream! published on March 13, 2017 at 12:42:39 PM
Description:
This script uses the date() function to format the UNIX timestamp on a piece of content from a Content Feed (note that the system automatically adds the "date" parameter and value upon spider). It converts it to a human-readable format in two steps: First to format the month, day, and year; and then to format the hour, minute, second, and produce the AM/PM marker.
Set Current Date/Time as User Profile Field Value
Use Case: Used in conjunction with the date() function, apply a date value to a user profile after they receive a “Subscription Cancellation” template in order to re-target down the line (ex. 30 days later, send an offer for 15% off if they resubscribe)
Zephyr:
{api_user({"vars" : {"canceled_date" : date('yyyy-MM-dd', time("now"))}})}
Output:
Explanation:
This script uses the api_user() function to set a custom field (called “canceled_date”) on a user upon receipt of a transactional template (in this scenario, a Cancellation Confirmation email). To generate the value, we use the time() function to produce the current timestamp and the date function to format it in the UNIX YYYY-MM-DD format, which is a requirement for storing a date value on the user profile. This can be beneficial for future retargeting, such as creating a daily Recurring Campaign with a Smart List of users whose “canceled_date” variable is exactly 30 days ago.
Create a Countdown Clock for a Certain Date
Use Case:
If you want to create a countdown clock until a certain date (for example, Christmas), you can use int() in conjunction with time() in order to subtract and get the whole-number remainder.
Zephyr:
{currentYear = date('yyyy')}
{christmasTime = (time("December 25, +currentYear+ 00:00:00") - time("now"))/86400}
{if christmasTime < 1 && christmasTime > 0}
{christmasCountdown = 1}
{else}
{christmasCountdown = int(christmasTime)}
{/if}
{if christmasCountdown > 0}
There are {christmasCountdown} days left until Christmas!
{/if}
Output:
christmasTime = 25.508055555555554int(christmasTime) = 25
Explanation:
This script uses the date() function to isolate the current year, and within a local variable called “christmasTime”, it appends that value on to “December 25,” and from there uses time() to generate a UNIX timestamp, which is the timestamp of Christmas that year. That value is then subtracted by the current timestamp and then divided by 86400, which is the number of seconds in a day. This gives us the number of days left until Christmas. Since this renders as a “float” (i.e., has a decimal place), an “if” statement is used to to find any numbers between 0 and 1 (for example, “.5”, which would be roughly 12 hours left until Christmas) and set the value of a local variable called “christmasCountdown” to “1”, as this means Christmas is the next day. Otherwise, using the int() function, the float value “christmasTime” variable is rounded down to the nearest whole number and assigned to the “christmasCountdown” variable. Finally, the message is wrapped in a conditional checking if the value for “christmasCountdown” is greater than 0. If it is, display the message.
Add Ordinal Dates to the Current Date
Use Case:
You want to add the ordinal date (i.e. “st” to “January 1) based on the current date.Zephyr:
In the Setup: {*Ordinal Date*} {suffixes = [“th”, “st”, “nd”, “rd”, “th”, “th”, “th”, “th”, “th”, “th”,”th”, “th”, “th”, “th”, “th”, “th”, “th”, “th”, “th”, “th”,”th”, “st”, “nd”, “rd”, “th”, “th”, “th”, “th”, “th”, “th”, “th”, “st”]} {month = date(‘MMMM’)} {day = date(‘d’)} {suffix = suffixes[int(day)]} {ordDate = month + ” ” + day + suffix} In the Code:<p>Today is {ordDate}!</p>
Output: Today is February 29th!
Explanation:This script uses the date() function to isolate the current month and and day, with the output for the day being “1”, “2”, “3”, etc. There is also an array called “suffixes” created where each suffix is in its proper numerical position (i.e. “st” is in the first position, “nd” in the second position, and “rd” in the third, so that “1st”, “2nd”, and “3rd” will be outputted. There’s also a local variable called “suffix” uses the numerical value of the current day, created using the int() function. This number is used to find the proper suffix in the “suffixes” array. For instance, if today is March 2, the value of “suffix” is suffixes[2], which equates to “nd”. Finally, an “ordDate” variable is created, which is the concatenation of the current month plus the current day plus its proper suffix.
Include Dynamic Date for ‘Email Pause’ Opt-down Option in Optout Page
Use Case: You want to let users “pause” their subscription by setting a custom field on their profile called “optback_date.” You’ll then add these users to a suppression list to avoid mailing them until their selected date has passed:
Zephyr:
In a Hosted Page:<form method="post"><p>When do you want to hear from us again?</p><label><input type="radio" name="vars[optback_date]" value="{date('yyyy-MM-dd', time("+30 days"))}" data-type="date"/> Mail me again in 30 Days ({date('MMMM dd, yyyy', time("+30 days"))})</label><br/><br/><label><input type="radio" name="vars[optback_date]" value="{date('yyyy-MM-dd', time("+60 days"))}" data-type="date"/> Mail me again in 60 Days ({date('MMMM dd, yyyy', time("+60 days"))})</label><br/><br/><label><input type="radio" name="vars[optback_date]" value="{date('yyyy-MM-dd', time("+90 days"))}" data-type="date"/> Mail me again in 90 Days ({date('MMMM dd, yyyy', time("+90 days"))})</label><br/><br/><label><input type="radio" name="vars[optback_date]" value="{date('yyyy-MM-dd', time("now"))}" data-type="date"/> I miss you! Resubscribe Me.</label><br/><br/><input type="submit" value="Submit"/></form>
Output:
You can then create a suppression list to suppress any user whose “optback_date” value is after today:Explanation:
This script uses the date() and time() functions to set a dynamic value for a custom field on a user profile called “optback_date”. The time() function generates the UNIX timestamp for 30, 60, and 90 days from now, and the date() function formats them as YYYY-MM-DD (required format for setting a “date” variable in Sailthru). This value is then set on a user’s profile as the optback_date, and any user who has a value for that comes after today’s date, they’ll be added to a suppression list. The code also includes a way for users to resubscribe by resetting that value to today’s date, using the time() function to pull the current UNIX timestamp, and again formatting it as YYYY-MM-DD with the date() function.Format Date from a Var
Use Case: You want to reformat a date var, such as a profile var, send var, event var, etc.
Zephyr:
Here is the original var: {"expiration_date":"2023-02-22"}
The date reformatted: {date("MMMM dd, yyyy", time(expiration_date))}
Output: The date reformatted: February 22, 2023
Description:
This uses the time()
function to convert the var to a timestamp, and uses the date()
function to format it.