轉換函式 conversion-functions

轉換函式可讓您在歷程運算式中將資料從一種型別轉換為另一種型別。 這些函式對於在使用不同的資料來源和作業時確保資料相容性和正確型別處理至關重要。

當您需要以下工作時,請使用轉換函式:

每個轉換函式都會自動處理型別特定規則和邊緣案例,讓資料轉換在歷程運算式中更加可靠且可預測。

快速參考 quick-reference

目標
函數
將字串或epoch轉換為具有​時區的日期
toDateTime
將字串或日期轉換為不含​時區的日期時間
toDateTimeOnly
僅擷取日期(年 — 月 — 日,無時間)
toDateOnly
轉換為整數
toInteger
轉換為十進位數字
toDecimal
轉換為true/false
toBool
將任何值轉換為字串
toString
轉換為持續時間(ISO-8601,例如PT10H)
toDuration
TIP
toDateTime與toDateTimeOnly:​時區很重要時使用toDateTime (例如,排程訊息、比較不同區域的事件時間戳記)。 當只有當地日期時間相關且可以忽略時區時使用toDateTimeOnly (例如,比較條件中的行事曆日期)。

常見陷阱 pitfalls

  • 時區必須是字串常數toDateTime中的時區引數不能是欄位參考或動態運算式。 一律傳遞常值字串,例如"UTC""Europe/Paris"
  • 字串輸入所需的ISO-8601格式 — 將字串傳遞至toDateTimetoDateTimeOnly時,請確定它遵循ISO-8601格式(例如"2023-08-18T23:17:59.123Z")。 格式錯誤的字串傳回null且沒有錯誤。
  • Epoch值以毫秒為單位toDateTime(1560762190189)需要毫秒。 如果您的來源以秒為單位提供Unix時間戳記,請先乘以1000 (例如toDateTime(myField * 1000))。
  • 具有未預期字串的​toBooltoBool只有在字串值剛好"true"時才傳回true。 任何其他字串(包括"1""yes""TRUE")會傳回false

toBool toBool

根據其型別,將引數值轉換為布林值。

  • 從字串:嘗試將字串值轉換為布林值,如果字串值為「true」,則從「true」,否則為false
  • 從數值:如果數值不等於0,則為true;否則為false
語法
toBool(<parameter>)
參數
  • 小數
  • 布林值
  • 字串
  • 整數
簽章與傳回的型別

toBool(<decimal>)

toBool(<boolean>)

toBool(<string>)

toBool(<integer>)

傳回布林值。

範例

toBool("true")

toBool(1)

傳回true。

toBool("this is not a boolean")

傳回false。

toDateOnly toDateOnly

將引數轉換為dateOnly型別值。 若要深入瞭解資料型別,請參閱此區段

語法
toDateOnly(<parameters>)
參數
table 0-row-2 1-row-2 2-row-2 3-row-2 4-row-2
參數 類型
以「YYYY-MM-DD」(XDM格式)表示日期的字串表示法。 也支援ISO-8601格式:只考慮​ 完整日期 ​部分(請參閱RFC 3339,第5.6節) 字串
日期時間 dateTime
沒有時區的日期時間 dateTimeOnly
紀元的整數值(以毫秒為單位) 整數
簽章與傳回的型別

toDateOnly(<dateTime>)

toDateOnly(<dateTimeOnly>)

toDateOnly(<string>)

toDateOnly(<integer>, <integer>, <integer>)

傳回dateOnly型別值。

範例

toDateOnly("2023-08-18")

toDateOnly("2023-08-18T00:00:00.000Z")

toDateOnly("2023-08-18T00:00:00")

所有傳回代表2023-08-18的dateOnly物件。

toDateOnly(#{ExperiencePlatform.ProfileFieldGroup.person.birthDate})

傳回dateOnly。

toDateTime toDateTime

根據引數的型別,將其轉換為日期時間值。

語法
toDateTime(<parameters>)
參數
table 0-row-2 1-row-2 2-row-2 3-row-2 4-row-2 5-row-2
參數 說明
字串 ISO-8601格式的日期時間。 日期時間的字串表示法,包含時區資訊
字串 時區ID。 時區識別碼(例如「UTC」、「歐洲/巴黎」)
dateOnly 代表沒有時區的日期,以年 — 月 — 日檢視
dateTimeOnly 代表沒有時區的日期時間,以年 — 月 — 日 — 小時 — 分鐘 — 秒 — 毫秒的方式檢視
整數 紀元的整數值(以毫秒為單位)
簽章與傳回的型別

toDateTime(<string>)

toDateTime(<string>, <dateOnly>)

toDateTime(<string>, <dateTimeOnly>)

toDateTime(<integer>)

傳回​日期時間

範例

toDateTime("2023-08-18T23:17:59.123Z")

傳回2023-08-18T23:17:59.123Z

ISO-8601字串已包含時區資訊。

toDateTime("Europe/Paris", toDateOnly("2023-08-18"))

傳回2023-08-18T00:00:00.000+02:00

這會將時區與僅限日期的值結合,以建立dateTime。 時間設定為指定時區的午夜(00:00:00)。

toDateTime("UTC", toDateTimeOnly("2023-08-18T23:17:59.123"))

傳回2023-08-18T23:17:59.123Z

這會透過將時區套用到dateTimeOnly值(沒有時區資訊)來建立dateTime。

toDateTime(1560762190189)

傳回2019-06-17T09:03:10.189Z

將以毫秒為單位的Unix時間戳記轉換為dateTime值。

NOTE
時區ID必須是字串常數。 它不能是欄位參考或運算式。 如需資料型別的詳細資訊,請參閱此頁面

toDateTimeOnly toDateTimeOnly

將引數值轉換為僅日期時間值。

語法
toDateTimeOnly(<parameters>)
參數
table 0-row-2 1-row-2 2-row-2
參數 類型
ISO-8601或"YYYY-MM-DD"格式的日期時間(XDM日期格式) 字串
日期時間 dateTime
簽章與傳回的型別

toDateTimeOnly(<dateTime>)

toDateTimeOnly(<string>)

傳回日期時間而不考慮時區。

範例

toDateTimeOnly ("2023-08-18")

傳回代表2023-08-18T00:00:00.000的dateTime

toDateTimeOnly(now())

toDecimal toDecimal

根據其型別,將引數值轉換為十進位值。

語法
toDecimal(<parameter>)
參數
table 0-row-2 1-row-2 2-row-2 3-row-2 4-row-2
參數 說明
字串 將字串值轉換為小數
dateTime 將日期轉換為毫秒數(紀元毫秒)
布林值 如果為true,則將布林值轉換為1,如果為false,則轉換為0
整數 轉換為小數(範例: 1變成1.0)
簽章與傳回的型別

toDecimal(<integer>)

toDecimal(<decimal>)

toDecimal(<string>)

toDecimal(<boolean>)

傳回小數。

範例

toDecimal("4.0")

傳回4.0。

toDuration toDuration

將引數值轉換為持續時間。 如需資料型別的詳細資訊,請參閱此頁面

語法
toDuration(<parameter>)
參數
table 0-row-2 1-row-2 2-row-2
參數 說明
字串 以ISO-8601持續時間格式PnDTnHnMn.nS為基礎的格式,將天數視為24小時
整數 毫秒數

如果字串運算式:接受的格式是以ISO-8601期間格式PnDTnHnMn.nS為基礎,而天數則視為24小時。

字串以選用的符號開頭,以ASCII負號或正號表示。 如果為負值,則整個期間都會被否定。 ASCII字母「P」是下一個大寫或小寫。 然後會有四個區段,每個區段包含一個數字和一個尾碼。 區段的ASCII尾碼為"D"、“H”、“M"及"S”,分別代表日、小時、分鐘及秒,可使用大寫或小寫。 尾碼必須依序出現。 ASCII字母「T」必須在小時、分鐘或秒區段的第一個專案(如果有的話)之前出現。 四個區段中的至少一個必須存在,如果存在「T」,則在「T」之後必須至少有一個區段。 每個區段的數字部分必須包含一個或多個ASCII數字。 數字可以以ASCII負號或正號為前置詞。 必須分析的天數、小時數和分鐘數。 秒數必須剖析為以及選用的分數。 小數點可以是點或逗號。 分數部分可能有0到9位數。

簽章與傳回型別

toDuration(<string>)

toDuration(<integer>)

傳回持續時間。

範例

toDuration("PT10H")

傳回10小時的持續時間。

toDuration("PT4S")

傳回4s的持續時間。

toDuration(4000)

傳回4s的持續時間。

toInteger toInteger

將引數值轉換為整數。

語法
toInteger(<parameter>)
參數
table 0-row-2 1-row-2 2-row-2 3-row-2 4-row-2
參數 說明
字串 將字串值轉換為整數
dateTime 將日期轉換為毫秒數(紀元毫秒)
小數 透過移除小數部分轉換為整數(範例: 1.5變為1)
布林值 如果為true,則將布林值轉換為1,如果為false,則轉換為0
簽章與傳回型別

toInteger(<dateTime>)

toInteger(<decimal>)

toInteger(<integer>)

toInteger(<string>)

toInteger(<boolean>)

傳回整數。

範例

toInteger("4")

傳回4。

toString toString

根據其型別,將引數值轉換為字串值。 如需資料型別的詳細資訊,請參閱此頁面

語法
toString(<parameter>)
參數
table 0-row-2 1-row-2 2-row-2 3-row-2 4-row-2 5-row-2 6-row-2
參數 說明
dateTime 將日期轉換為UTC日期格式
dateTimeOnly 將日期轉換為UTC日期格式
期間 轉換為字串形式的對應毫秒數
整數 轉換為值的字串表示法(1會變成「1」)
小數 轉換為值的字串表示法(1.5會變成"1.5")
布林值 將布林值轉換為’true’ (如果為true),‘false’ (如果為false)
簽章與傳回型別

toString(<dateTimeOnly>)

toString(<dateTime>)

toString(<duration>)

toString(<boolean>)

toString(<integer>)

toString(<decimal>)

傳回字串。

範例

toString(4)

傳回「4」。

toString(#{ExperiencePlatform.test_date.person.birthDate}))

傳回給定dateOnly欄位(XDM日期欄位)的字串表示法,例如「2023-08-18」。

toString(toDuration(1520))

傳回「PT1.52S」。

AI Knowledge Reference

This section contains structured knowledge intended to support interpretation, retrieval, and question answering related to this topic.

For complete understanding, this information should be combined with the documentation on this page. Neither source is intended to stand alone; the page describes the feature, while this section provides additional context that helps disambiguate terminology, intent, applicability, and constraints.

  • TL;DR: This page documents all conversion functions in AJO journey expressions, explaining how to transform values between types such as string, integer, decimal, boolean, date, datetime, and duration.

Intents:

  • Convert a string or epoch integer to a timezone-aware datetime using toDateTime
  • Convert a string or datetime to a timezone-less datetime using toDateTimeOnly
  • Extract a date-only value (year-month-day) from a string or datetime using toDateOnly
  • Cast a value to an integer, decimal, or boolean using toInteger, toDecimal, or toBool
  • Serialize any value to its string representation using toString
  • Convert a string or millisecond integer to a duration using toDuration

Glossary:

  • dateTime: A datetime value that includes timezone offset information (product-specific)
  • dateTimeOnly: A datetime value with no timezone information (product-specific)
  • dateOnly: A date value representing year-month-day with no time component (product-specific)
  • duration: A time period expressed in ISO-8601 format (e.g., PT10H) (product-specific)
  • epoch milliseconds: Unix timestamp expressed in milliseconds since 1970-01-01T00:00:00Z

Guardrails:

  • The timezone argument in toDateTime must be a string constant — field references and dynamic expressions are not allowed
  • String inputs to toDateTime and toDateTimeOnly must follow ISO-8601 format; malformed strings return null without an error
  • toDateTime with an epoch integer expects milliseconds; multiply seconds-based timestamps by 1000 before passing
  • toBool returns true only for the exact string "true"; strings like "1", "yes", or "TRUE" return false

Terminology:

  • Canonical name: Conversion functions — Acronym: none — variants: type casting functions, type conversion functions
  • Synonyms: “toDateTime” = “convert to datetime with timezone”; “toDateTimeOnly” = “convert to datetime without timezone”
  • Do not confuse: “toDateTime” (timezone-aware) ≠ “toDateTimeOnly” (no timezone)
  • Do not confuse: “toDateOnly” (date only, no time) ≠ “toDateTime” (date and time with timezone)

FAQ:

  • Q: When should I use toDateTime versus toDateTimeOnly? — Use toDateTime when timezone information matters (e.g., scheduling or cross-region comparisons); use toDateTimeOnly when only the local date-time is relevant and timezone can be ignored.
  • Q: Why does toBool("TRUE") return false?toBool only recognizes the exact lowercase string "true"; all other string values including "TRUE" or "yes" return false.
  • Q: How do I convert a Unix timestamp in seconds to a dateTime? — Multiply the seconds value by 1000 to get milliseconds, then pass it to toDateTime, e.g., toDateTime(myField * 1000).
  • Q: Can the timezone in toDateTime be read from a profile attribute? — No, the timezone ID must be a string constant; field references and expressions are not supported.
  • Q: What format does toDuration accept as a string? — ISO-8601 duration format, e.g., "PT10H" for 10 hours or "P1DT2H" for 1 day and 2 hours.
recommendation-more-help
journey-optimizer-help