Runtime verified Differs from official docs Test scripts included

Syntax

DatePart(dateString, datePart)  →  string
2 arguments — exactly

Parameters

Name Type Required Description
dateString string | date Yes A real date value or a parseable date string
datePart string Yes One of Y, M, D, H or MI

Example

%%=DatePart("2026-11-23 19:35:47", "H")=%%

Renders 19.

The result can go into arithmetic directly:

%%=Add(DatePart("2026-03-04 09:05:07", "Y"), 1)=%%

Renders 2027.

Return value

string — the requested component as digits, with no padding: from 2026-03-04 09:05:07 the tokens Y, M, D, H and MI give 2026, 3, 4, 9 and 5.

Add and Multiply accept the result (Multiply(DatePart("2026-03-04 09:05:07", "M"), 10) gives 30). Wrapping it in Length() made the preview fail with HTTP 500, so do not measure it.

Behaviour

Five tokens. Y, M, D, H and MI work. The long tokens year, month, monthName, day, hour and minute make the preview fail (HTTP 500); DatePart("2026-03-04 09:05:07", "monthName") on its own is enough. Use FormatDate with MMMM for a month name.

No leading zeros. Month and day come back as 3 and 4, not 03 and 04, for any input format: ISO, ISO with T, US slash and spelled-out strings all gave unpadded values.

The hour is on a 24-hour clock.

Call Renders
DatePart("2026-11-23 19:35:47", "H") 19
DatePart("2026-03-04 00:30:00", "H") 0
DatePart("2026-03-04 12:30:00", "H") 12
DatePart("2026-03-04", "H") 0

Date values work. DatePart(Now(), "Y") gives the current year, and a DateAdd result goes in directly.

Bad input breaks the render. An unreadable date, a seconds token S, a dayName token and an unknown token ZZ each make the preview fail.

Exactly two arguments. An email that calls DatePart with one or with three arguments cannot be saved: the content API answers HTTP 400 with “The AMPscript syntax isn’t valid”.

Show Next test script
<!-- Marketing Cloud Next test script for DatePart. Paste it into the HTML component of a
     Next email and open the preview. Each line prints the result Next gave when the call
     was rendered on its own (expect) next to the result of this run (got).
     Assembled from single-call test emails (dates on the page); it has not been
     run as one email. Calls that make Next fail are listed separately. -->
<p>c1 expect=[2026] got=[%%=DatePart("2026-01-15","Y")=%%]</p>
<p>c2 expect=[1] got=[%%=DatePart("2026-01-15","M")=%%]</p>
<p>c3 expect=[19] got=[%%=DatePart("2026-11-23 19:35:47","H")=%%]</p>
<p>c4 expect=[2026|3|4|9|5|2026|11|23|19|35] got=[%%=DatePart("2026-03-04 09:05:07","Y")=%%|%%=DatePart("2026-03-04 09:05:07","M")=%%|%%=DatePart("2026-03-04 09:05:07","D")=%%|%%=DatePart("2026-03-04 09:05:07","H")=%%|%%=DatePart("2026-03-04 09:05:07","MI")=%%|%%=DatePart("2026-11-23 19:35:47","Y")=%%|%%=DatePart("2026-11-23 19:35:47","M")=%%|%%=DatePart("2026-11-23 19:35:47","D")=%%|%%=DatePart("2026-11-23 19:35:47","H")=%%|%%=DatePart("2026-11-23 19:35:47","MI")=%%]</p>
<p>c5 expect=[0|12|0] got=[%%=DatePart("2026-03-04 00:30:00","H")=%%|%%=DatePart("2026-03-04 12:30:00","H")=%%|%%=DatePart("2026-03-04","H")=%%]</p>
<p>c6 expect=[2027|30] got=[%%=Add(DatePart("2026-03-04 09:05:07","Y"),1)=%%|%%=Multiply(DatePart("2026-03-04 09:05:07","M"),10)=%%]</p>
<p>c7 expect=[2026|5] got=[%%=DatePart(Now(),"Y")=%%|%%=DatePart(DateAdd("2026-03-04 09:05:07",1,"D"),"D")=%%]</p>
<p>c8 expect=[4|4|3|5] got=[%%=DatePart("2026-03-04","D")=%%|%%=DatePart("3/4/2026 9:05 AM","D")=%%|%%=DatePart("4 March 2026","M")=%%|%%=DatePart("2026-03-04T09:05:07","MI")=%%]</p>

Show calls that fail in Next (one email each)
<!-- Run each call below in its own Next email: one of them in a combined email
     would stop the whole preview or block the save. -->

<!-- email 1: expect that the email saves, but the preview fails (HTTP 500) -->
%%=DatePart("2026-03-04 09:05:07","monthName")=%%

<!-- email 2: expect that the email saves, but the preview fails (HTTP 500) -->
%%=DatePart("2026-03-04 09:05:07","year")=%%|%%=DatePart("2026-03-04 09:05:07","month")=%%|%%=DatePart("2026-03-04 09:05:07","monthName")=%%|%%=DatePart("2026-03-04 09:05:07","day")=%%|%%=DatePart("2026-11-23 19:35:47","hour")=%%|%%=DatePart("2026-03-04 09:05:07","minute")=%%

<!-- email 3: expect that the email saves, but the preview fails (HTTP 500) -->
%%=DatePart("2026-03-04 09:05:07","y")=%%|%%=DatePart("2026-03-04 09:05:07","m")=%%|%%=DatePart("2026-03-04 09:05:07","d")=%%|%%=DatePart("2026-11-23 19:35:47","h")=%%|%%=DatePart("2026-03-04 09:05:07","mi")=%%|%%=DatePart("2026-03-04 09:05:07","Mi")=%%|%%=DatePart("2026-03-04 09:05:07","YEAR")=%%|%%=DatePart("2026-03-04 09:05:07","MONTHNAME")=%%|%%=DatePart("2026-03-04 09:05:07","monthname")=%%

<!-- email 4: expect that the email saves, but the preview fails (HTTP 500) -->
%%=Length(DatePart("2026-03-04 09:05:07","M"))=%%|%%=Length(DatePart("2026-03-04 09:05:07","H"))=%%

<!-- email 5: expect that the email saves, but the preview fails (HTTP 500) -->
%%=DatePart("not a date at all","Y")=%%

<!-- email 6: expect that the email saves, but the preview fails (HTTP 500) -->
%%=DatePart("2026-03-04 09:05:07","S")=%%

<!-- email 7: expect that the email saves, but the preview fails (HTTP 500) -->
%%=DatePart("2026-03-04 09:05:07","dayName")=%%

<!-- email 8: expect that the email saves, but the preview fails (HTTP 500) -->
%%=DatePart("2026-03-04 09:05:07","ZZ")=%%

<!-- email 9: expect that the save is rejected (HTTP 400) -->
%%=DatePart("2026-03-04 09:05:07")=%%

<!-- email 10: expect that the save is rejected (HTTP 400) -->
%%=DatePart("2026-03-04 09:05:07","Y","extra")=%%

Differs from official docs

  • Documented as unavailable in Next, yet accepted. The reference’s availability table says No for Marketing Cloud Next. In a Next email the tokens Y, M, D, H and MI all returned the expected parts (unpadded, 24-hour hour); only wrapping the result in Length() failed the render. Card

Availability & differences

PlatformAvailable
Marketing Cloud EngagementYes
Marketing Cloud NextYes, from API 68.0

See also