轉換函式 conversion-functions
轉換函式可讓您在歷程運算式中將資料從一種型別轉換為另一種型別。 這些函式對於在使用不同的資料來源和作業時確保資料相容性和正確型別處理至關重要。
當您需要以下工作時,請使用轉換函式:
- 將字串值轉換為數值、布林值或日期型別(toInteger, toDecimal, toBool)
- 轉換不同格式和表示之間的日期和時間(toDateTime,toDateTimeOnly,toDateOnly)
- 轉換介於整數與小數型別(toInteger, toDecimal)之間的數值
- 將值轉換為字串格式(toString)或持續時間(toDuration)
- 確保比較和作業的型別相容性
- 處理來自可能具有不同型別格式的外部來源的資料
每個轉換函式都會自動處理型別特定規則和邊緣案例,讓資料轉換在歷程運算式中更加可靠且可預測。
快速參考 quick-reference
toDateTime (例如,排程訊息、比較不同區域的事件時間戳記)。 當只有當地日期時間相關且可以忽略時區時使用toDateTimeOnly (例如,比較條件中的行事曆日期)。常見陷阱 pitfalls
- 時區必須是字串常數 —
toDateTime中的時區引數不能是欄位參考或動態運算式。 一律傳遞常值字串,例如"UTC"或"Europe/Paris"。 - 字串輸入所需的ISO-8601格式 — 將字串傳遞至
toDateTime或toDateTimeOnly時,請確定它遵循ISO-8601格式(例如"2023-08-18T23:17:59.123Z")。 格式錯誤的字串傳回null且沒有錯誤。 - Epoch值以毫秒為單位 —
toDateTime(1560762190189)需要毫秒。 如果您的來源以秒為單位提供Unix時間戳記,請先乘以1000 (例如toDateTime(myField * 1000))。 - 具有未預期字串的toBool —
toBool只有在字串值剛好"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值。
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」。
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, ortoBool - 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
toDateTimemust be a string constant — field references and dynamic expressions are not allowed - String inputs to
toDateTimeandtoDateTimeOnlymust follow ISO-8601 format; malformed strings return null without an error toDateTimewith an epoch integer expects milliseconds; multiply seconds-based timestamps by 1000 before passingtoBoolreturnstrueonly for the exact string"true"; strings like"1","yes", or"TRUE"returnfalse
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
toDateTimeversustoDateTimeOnly? — UsetoDateTimewhen timezone information matters (e.g., scheduling or cross-region comparisons); usetoDateTimeOnlywhen only the local date-time is relevant and timezone can be ignored. - Q: Why does
toBool("TRUE")return false? —toBoolonly 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
toDateTimebe 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
toDurationaccept as a string? — ISO-8601 duration format, e.g.,"PT10H"for 10 hours or"P1DT2H"for 1 day and 2 hours.