在 PowerApps 中顯示文字並設定日期和時間的格式Show text and format dates and times in PowerApps

新增日期和時間並設定其格式,以顯示正確的詳細資料層級,或反映您的地區設定。Add dates and times, and format them to show the right level of detail or to reflect your locale. 計算兩個日期之間相隔的時間量,或計算您所指定日期之前或之後一段時間的日期。Calculate the amount of time between two dates, or calculate a date that's a certain amount of time before or after a date that you specify. 在日期與日、月和年的個別值之間來回轉換,以及在時間與小時、分鐘和秒鐘的個別值之間來回轉換。Convert dates to or from separate values for days, months, and years, and convert times to or from separate values for hours, minutes, and seconds.

例如,新增來自使用者有關股票交易或客戶會議的資料、來自外部來源的資料,或來自 PowerApps 中所建立之另一個應用程式的資料。For example, add data from users about stock trades or client meetings, data from an external source, or data from another app created in PowerApps. 如果該資料包含的時間計算到毫秒,則將它四捨五入至最接近的分鐘,以求簡化。If that data includes times down to the millisecond, round it to the nearest minute for simplicity. 計算還有多少天才會到達重大里程碑。Calculate how many days remain before a major milestone. 如果您想要排程每隔五天舉行一次客戶會議,則自動計算這些日期。If you want to schedule client meetings every five days, calculate those dates automatically. 如果 1985 年 5 月 10 日分別儲存在日、月和年的欄位中,則將這些欄位合併成單一值。If May 10, 1985, is stored in separate fields for the day, the month, and the year, consolidate them into a single value. 相反地,如果您的應用程式分開管理這些值,請將每個日期拆解成個別值。Conversely, break each date into separate values if your app manages them separately.

必要條件Prerequisites

  • 註冊 PowerApps、加以安裝、予以開啟,然後以您註冊時所使用的認證登入。Sign up for PowerApps, install it, open it, and then sign in by providing the same credentials that you used to sign up.
  • 在 PowerApps 中建立應用程式或開啟現有的。Create an app or open an existing app in PowerApps.
  • 了解如何在 PowerApps 中設定控制項Learn how to configure a control in PowerApps.

在標籤控制項中顯示文字Show text in a Label control

設定標籤控制項之 Text 屬性的值,以在控制項中顯示文字。Show text in a Label control by setting the value of its Text property. 直接輸入到控制項或在公式列中輸入運算式,以設定此屬性。Set this property by typing directly into the control or by typing an expression in the formula bar.

  • 如果您直接輸入到控制項,它會一字不漏地顯示您輸入的內容。If you type directly into the control, it shows exactly what you type.
  • 如果您在公式列中輸入運算式,控制項則會顯示運算式的結果。If you type an expression in the formula bar, the control shows the result of the expression.

以下是一些範例。Here are some examples.

  1. 新增名為 ShowText標籤控制項,並將其 Text 屬性設為以下公式:Add a Label control named ShowText, and set its Text property to this formula:
    Now()Now()

    如果您的電腦設定為「en-us」地區設定,則目前的日期和時間會以這種格式顯示︰ If your computer is set to the "en-us" locale, the current date and time appears in this format:
    mm/dd/yyyy hh:mm AM/PMmm/dd/yyyy hh:mm AM/PM

    如果您的電腦設定為「fr-fr」之類的地區設定,則目前的日期和時間會以這種格式顯示︰ If your computer is set to a locale such as "fr-fr", the current date and time appears in this format:
    dd/mm/yyyy hh:mm AM/PMdd/mm/yyyy hh:mm AM/PM

  2. ShowTextText 屬性設為以下公式:Set the Text property of ShowText to this formula:
    DateDiff(Today(), DateValue("01/01/2020"))DateDiff(Today(), DateValue("01/01/2020"))

    今天和 2020 年 1 月 1 日之間相隔的天數

    此控制項會使用下列函式顯示今天和 2020 年 1 月 1 日之間相隔的天數︰The control shows the number of days between today and January 1, 2020, by using these functions:

    • DateDiff,計算兩個日期之間相隔的天數、季數或年數。DateDiff, which calculates the number of days, quarters, or years between two dates.
    • Today,計算當天的值。Today, which calculates the current day as a value.
    • DateValue,將常值字串 (雙引號之間顯示的日期) 轉換為可供執行計算的值。DateValue, which converts a literal string, as shown between double quotation marks, to a value on which calculations can be performed.
  3. 新增名為 BirthDate文字輸入控制項,並將它移到 ShowText 底下。Add a Text input control named BirthDate, and move it under ShowText.
  4. BirthDate 中,輸入您的出生月日 (例如,05/18)。In BirthDate, type the month and the day of your birth (for example, 05/18).
  5. ShowTextText 屬性設為以下公式:Set the Text property of ShowText to this formula:
    DateDiff(Today(), DateValue(BirthDate.Text))DateDiff(Today(), DateValue(BirthDate.Text))

    今天和您的生日之間相隔的天數

    ShowText 會顯示今天和您在 BirthDate 中所輸入之日期之間相隔的天數。ShowText shows the number of days between today and whatever date you type into BirthDate. 如果您今年的生日已過,ShowText 會顯示負數值。If your birthday has already occurred this year, ShowText displays a negative value.

使用 DateTimeValue 設定日期和時間的格式Format dates and times by using DateTimeValue

將日期和時間從文字字串轉換為值,而您可將其設定為各種格式並用於計算。Convert dates and times from strings of text to values, which you can format in a variety of ways and use in calculations. 使用內建和自訂選項來指定格式。Specify the format by using built-in and custom options.

注意

DateTimeValueDateValue 函式可將下列任何一種格式的日期轉換為值︰The DateTimeValue and DateValue functions can convert dates in any of these formats into values:

  • MM/DD/YYYYMM/DD/YYYY
  • DD/MM/YYYYDD/MM/YYYY
  • DD Mon YYYYDD Mon YYYY
  • Month DD, YYYYMonth DD, YYYY
  1. 新增名為 ArrivalDateTime文字輸入控制項,並輸入下列格式的日期和時間︰Add a Text input control named ArrivalDateTime, and type a date and time in this format:
    5/10/85 6:15 AM5/10/85 6:15 AM
  2. 新增名為 ShowDate標籤控制項,並將其 Text 屬性設為以下公式:Add a Label control named ShowDate, and set its Text property to this formula:
    DateTimeValue(ArrivalDateTime.Text)DateTimeValue(ArrivalDateTime.Text)

    將日期/時間從文字轉換為值

    ShowDate 會顯示您所輸入的相同資訊,但該資訊已從文字轉換為值且使用不同格式。ShowDate shows the same information that you typed, but it's been converted from text to a value and formatted differently. 例如,年會顯示為四位數,而非只有兩位數。For example, the year appears as four digits instead of just two.

  3. ShowDateText 屬性變更為下列公式:Change the Text property of ShowDate to this formula:
    DateTimeValue(ArrivalDateTime.Text, "fr")DateTimeValue(ArrivalDateTime.Text, "fr")

    以法文格式顯示日期/時間值

    ShowDate 會以先日後月的方式顯示,正如法文使用者所預期的格式。ShowDate shows the day before the month, as a French user would expect.

    提示

    若要在 Intellisense 中顯示其他地區設定的清單,請從公式中移除右引號和 fr,但保留左引號︰To display a list of other locales in Intellisense, remove the closing quotation mark and fr from the formula, but leave the open quotation mark:

    顯示地區設定清單

  4. 若要使用幾種內建格式的其中一種,請將 ShowDateText 屬性變更為以下公式︰To use one of several built-in formats, change the Text property of ShowDate to this formula:
    Text(DateTimeValue(ArrivalDateTime.Text), DateTimeFormat.LongDateTime)Text(DateTimeValue(ArrivalDateTime.Text), DateTimeFormat.LongDateTime)

    以法文格式顯示日期/時間值

    ShowDate 會顯示星期幾、日期和時間。ShowDate shows the day of the week, the date, and the time.

    提示

    DateTimeFormat 參數支援其他幾種內建格式。The DateTimeFormat parameter supports several other built-in formats. 若要顯示該清單,請從公式中移除 LongDateTimeTo display that list, remove LongDateTime from the formula.

  5. 若要使用自訂格式,請將 ShowDateText 屬性變更為以下公式:To use a custom format, change the Text property of ShowDate to this formula:
    Text(DateTimeValue(ArrivalDateTime.Text), "mm/dd/yyyy hh:mm:ss.fff AM/PM")Text(DateTimeValue(ArrivalDateTime.Text), "mm/dd/yyyy hh:mm:ss.fff AM/PM")

    以法文格式顯示日期/時間值

    ShowDate 會以您指定的格式顯示日期/時間值 (包含毫秒)。ShowDate shows the date/time value in the format that you specified, including milliseconds.

    提示

    若要將時間四捨五入至最接近的十分之一或百分之一秒,請在公式中指定 hh:mm:ss.fhh:mm:ss.ffTo round the time to the nearest tenth or hundredth of a second, specify hh:mm:ss.f or hh:mm:ss.ff in the formula.

使用 DateValue 設定日期的格式Format a date by using DateValue

  1. 新增名為 ArrivalDate文字輸入控制項,然後在其中輸入日期 (例如,5/10/85)。Add a Text input control named ArrivalDate, and then type a date in it (for example, 5/10/85).
  2. 新增名為 FormatDate標籤控制項,並將其 Text 屬性設為以下公式:Add a Label control named FormatDate, and set its Text property to this formula:
    DateValue(ArrivalDate.Text)DateValue(ArrivalDate.Text)

    FormatDate 會顯示您所輸入的日期,但年會顯示為四位數。FormatDate shows the date that you typed, except the year appears as four digits.

  3. FormatDateText 屬性設為以下公式:Set the Text property of FormatDate to this formula:
    DateValue(ArrivalDate.Text, "fr")DateValue(ArrivalDate.Text, "fr")

    FormatDate 會以先日後月的方式顯示,正如法文使用者所預期的格式。FormatDate shows the day before the month, just as a French user would expect.

  4. 若要使用幾種內建格式的其中一種,請將 FormatDateText 屬性設為以下公式︰To use one of several built-in formats, set the Text property of FormatDate to this formula:
    Text(DateValue(ArrivalDate.Text), DateTimeFormat.LongDate)Text(DateValue(ArrivalDate.Text), DateTimeFormat.LongDate)

    FormatDate 會顯示星期幾、月、日和年。FormatDate shows the day of the week, the month, the day, and the year.

  5. 若要使用自訂格式,請將 FormatDateText 屬性設為以下公式:To use a custom format, set the Text property of FormatDate to this formula:
    Text(DateValue(ArrivalDate.Text), "yy/mm/dd")Text(DateValue(ArrivalDate.Text), "yy/mm/dd")

    FormatDate 會以您指定的格式顯示日期。FormatDate shows the date in the format that you specified.

使用 DateTimeValue 設定時間的格式Format a time using DateTimeValue

  1. 新增名為 ArrivalTime文字輸入控制項,然後在其中輸入 6:15 AMAdd a Text input control named ArrivalTime, and then type 6:15 AM in it.
  2. 新增名為 ShowTime標籤控制項。Add a Label control named ShowTime.
  3. 若要使用幾種內建格式的其中一種,請將 ShowTimeText 屬性設為以下公式︰To use one of several built-in formats, set the Text property of ShowTime to this formula:
    Text(DateTimeValue(ArrivalTime.Text), DateTimeFormat.LongTime)Text(DateTimeValue(ArrivalTime.Text), DateTimeFormat.LongTime)

    ShowTime 會顯示您所指定的時間 (包括秒)。ShowTime shows the time that you specified, including seconds.

  4. 若要使用自訂格式,請將 ShowTimeText 屬性設為以下公式:To use a custom format, set the Text property of ShowTime to this formula:
    Text(DateTimeValue(ArrivalTime.Text), "hh:mm:ss.fff AM/PM")Text(DateTimeValue(ArrivalTime.Text), "hh:mm:ss.fff AM/PM")

    ShowTime 會顯示您所指定的時間 (包括秒和毫秒)。ShowTime shows the time that you specified, including seconds and milliseconds.

    提示

    若要將時間四捨五入至最接近的十分之一或百分之一秒,請在公式中輸入 hh:mm:ss.fhh:mm:ss.ffTo round the time to the nearest tenth or hundredth of a second, enter hh:mm:ss.f or hh:mm:ss.ff in the formula.

顯示日期之間相隔的時間Show the time between dates

  1. 新增兩個分別名為 StartEnd文字輸入控制項。Add two Text input controls named Start and End.
  2. Start 中輸入 4/1/2015,並在 End 中輸入 1/1/2016Type 4/1/2015 in Start, and type 1/1/2016 in End.
  3. 新增名為 DateDiff標籤控制項,並將其 Text 屬性設為以下公式:Add a Label control named DateDiff, and set its Text property to this formula:
    DateDiff(DateValue(Start.Text), DateValue(End.Text))DateDiff(DateValue(Start.Text), DateValue(End.Text))

    比較兩個日期

    DateDiff 會顯示 275,這是 2015 年 4 月 1 日和 2016 年 1 月 1 日之間相隔的天數。DateDiff shows 275, which is the number of days between April 1, 2015, and January 1, 2016.

  4. DateDiffText 屬性設為以下公式:Set the Text property of DateDiff to this formula:
    DateDiff(DateValue(Start.Text), DateValue(End.Text), Months)DateDiff(DateValue(Start.Text), DateValue(End.Text), Months)

    DateDiff 會顯示 9,這是 2015 年 4 月 1 日和 2016 年 1 月 1 日之間相隔的月份數。DateDiff shows 9, which is the number of months between April 1, 2015, and January 1, 2016. Months 替換為 QuartersYears 可顯示這些單位的時間。Replace Months with Quarters or Years to show the time in those units.

找出某個日期之前或之後的另一個日期Identify a date before or after another date

  1. 新增名為 Start文字輸入控制項,並在其中輸入 5/10/1985Add a Text input control named Start, and type 5/10/1985 in it.
  2. 新增名為 DateAdd標籤控制項,並將其 Text 屬性設為以下公式:Add a Label control named DateAdd, and set its Text property to this formula:
    DateAdd(DateValue(Start.Text), 3)DateAdd(DateValue(Start.Text), 3)

    加 3 天

    DateAdd 會顯示 5/13/1985,這是 Start 中日期的三天後。DateAdd shows 5/13/1985, which is three days after the date in Start.

  3. DateAddText 屬性設為以下公式:Set the Text property of DateAdd to this formula:
    DateAdd(DateValue(Start.Text), -3)DateAdd(DateValue(Start.Text), -3)

    減 3 天

    DateAdd 會顯示 5/7/1985,這是 Start 中日期的三天前。DateAdd shows 5/7/1985, which is three days before the date in Start.

  4. DateAddText 屬性變更為下列公式:Change the Text property of DateAdd to this formula:
    DateAdd(DateValue(Start.Text), 3, Months)DateAdd(DateValue(Start.Text), 3, Months)

    加 3 個月

    此標籤會顯示 8/10/1985,這是 Start 中日期的三個月後。The label shows 8/10/1985, which is three months after the date in Start. Months 替換為 QuartersYears 可找出 Start 中日期之前或之後指定季數或年數的日期。Replace Months with Quarters or Years to identify a date that's the specified number of quarters or years before or after the date in Start.

依據年、月和日來計算日期Calculate dates based on years, months, and days

  1. 新增三個分別名為 YearMonthDay下拉式清單控制項。Add three Drop down controls named Year, Month, and Day.
  2. YearItems 屬性設為以下公式:Set the Items property of Year to this formula:
    Table({Year:"2014"}, {Year:"2015"}, {Year:"2016"})Table({Year:"2014"}, {Year:"2015"}, {Year:"2016"})
  3. MonthItems 屬性設為以下公式:Set the Items property of Month to this formula:
    Table({Month:"1"}, {Month:"2"}, {Month:"3"}, {Month:"4"}, {Month:"5"}, {Month:"6"}, {Month:"7"}, {Month:"8"}, {Month:"9"}, {Month:"10"}, {Month:"11"}, {Month:"12"})Table({Month:"1"}, {Month:"2"}, {Month:"3"}, {Month:"4"}, {Month:"5"}, {Month:"6"}, {Month:"7"}, {Month:"8"}, {Month:"9"}, {Month:"10"}, {Month:"11"}, {Month:"12"})
  4. DayItems 屬性設為以下公式:Set the Items property of Day to this formula:
    Table({Day:"1"}, {Day:"2"}, {Day:"3"}, {Day:"4"}, {Day:"5"}, {Day:"6"}, {Day:"7"}, {Day:"8"}, {Day:"9"}, {Day:"10"}, {Day:"11"}, {Day:"12"}, {Day:"13"}, {Day:"14"}, {Day:"15"}, {Day:"16"}, {Day:"17"}, {Day:"18"}, {Day:"19"}, {Day:"20"}, {Day:"21"}, {Day:"22"}, {Day:"23"}, {Day:"24"}, {Day:"25"}, {Day:"26"}, {Day:"27"}, {Day:"28"}, {Day:"29"}, {Day:"30"}, {Day:"31"})Table({Day:"1"}, {Day:"2"}, {Day:"3"}, {Day:"4"}, {Day:"5"}, {Day:"6"}, {Day:"7"}, {Day:"8"}, {Day:"9"}, {Day:"10"}, {Day:"11"}, {Day:"12"}, {Day:"13"}, {Day:"14"}, {Day:"15"}, {Day:"16"}, {Day:"17"}, {Day:"18"}, {Day:"19"}, {Day:"20"}, {Day:"21"}, {Day:"22"}, {Day:"23"}, {Day:"24"}, {Day:"25"}, {Day:"26"}, {Day:"27"}, {Day:"28"}, {Day:"29"}, {Day:"30"}, {Day:"31"})
  5. 新增標籤控制項,並將其 Text 屬性設為以下公式:Add a Label control, and set its Text property to this formula:
    Text(Date(Value(Year.Selected.Value), Value(Month.Selected.Value), Value(Day.Selected.Value)), DateTimeFormat.LongDate)Text(Date(Value(Year.Selected.Value), Value(Month.Selected.Value), Value(Day.Selected.Value)), DateTimeFormat.LongDate)

    預設會列出 Wednesday, January 1, 2014Wednesday, January 1, 2014 is listed by default. 下拉式清單控制項中選取不同值,可變更標籤控制項中的日期。Select different values in the Drop down controls to change the date in the Label control.

您可能需要將非預期的資料進行轉換。You may need to convert data that you didn't expect. 如果您新增文字輸入控制項而非下拉式清單控制項,使用者可能會輸入錯誤日期,例如 May 45。If you add Text input controls instead of Drop down controls, a user may enter an incorrect date, such as May 45. Date 函式可使用下列方式處理格式不符的資料︰The Date function handles atypical data in the following ways:

  • 如果年份值介於 0 和 1899 (含) 之間,此函式會將該值加到 1900 來計算年份。If a year value is between 0 and 1899 (inclusive), the function adds that value to 1900 to calculate the year.
  • 如果年份值介於 1900 和 9999 (含) 之間,此函式會使用該值作為年份。If a year value is between 1900 and 9999 (inclusive), the function uses that value as the year.
  • 如果年份值小於 0 或是大於或等於 10000,此函式會傳回錯誤值。If a year value is less than 0 or is 10000 or greater, the function returns an error value.
  • 如果月份值大於 12,此函式會將該月份數加到指定年份的第一個月。If a month value is greater than 12, the function adds that number of months to the first month of the specified year.
  • 如果月份值小於 1,此函式會從指定年份的第一個月減去該月份數,再加上 1。If a month value is less than 1, the function subtracts that many months, plus 1, from the first month of the specified year.
  • 如果日期值大於指定月份的天數,此函式會將該天數加到該月份的第一天,並傳回對應的後續月份日期。If a day value is greater than the number of days in the specified month, the function adds that many days to the first day of the month and returns the corresponding date from a subsequent month.
  • 如果日期值小於 1,此函式會從指定月份的第一天減去該天數,再加上 1。If a day value is less than 1, the function subtracts that many days, plus 1, from the first day of the specified month.

依據小時、分和秒來計算時間Calculate times based on hours, minutes, and seconds

  1. 新增兩個分別名為 HourMinute下拉式清單。Add two Drop-down lists named Hour and Minute.
  2. HourItems 屬性設為以下公式:Set the Items property of Hour to this formula:
    Table({Hour:"9"}, {Hour:"10"}, {Hour:"11"}, {Hour:"12"}, {Hour:"13"}, {Hour:"14"}, {Hour:"15"}, {Hour:"16"}, {Hour:"17"})Table({Hour:"9"}, {Hour:"10"}, {Hour:"11"}, {Hour:"12"}, {Hour:"13"}, {Hour:"14"}, {Hour:"15"}, {Hour:"16"}, {Hour:"17"})
  3. MinuteItems 屬性設為以下公式:Set the Items property of Minute to this formula:
    Table({Minute:"0"}, {Minute:"15"}, {Minute:"30"}, {Minute:"45"})Table({Minute:"0"}, {Minute:"15"}, {Minute:"30"}, {Minute:"45"})
  4. 新增標籤控制項,並將其 Text 屬性設為以下公式:Add a Label control, and set its Text property to this formula:

    Text(Time(Value(Hour.Selected.Value), Value(Minute.Selected.Value), 0), DateTimeFormat.ShortTime)Text(Time(Value(Hour.Selected.Value), Value(Minute.Selected.Value), 0), DateTimeFormat.ShortTime)
  5. Hour 中選取 15,並在 Minute 中選取 45Select 15 in Hour and 45 in Minute.

    適用於標籤控制項會顯示 3:45 PMThe Label control shows 3:45 PM.

    您可以在 HourMinute 中新增項目,讓使用者可從更大範圍的小時和更精確的分鐘數中進行選取。You can add entries to Hour and Minute so that users can select from a bigger range of hours and a more precise number of minutes. 您也可以新增第三個下拉式清單控制項,讓使用者可以指定秒數。You can also add a third Drop down control so that users can specify seconds. 如果您新增第三個清單,請將標籤控制項的 Text 屬性設定為下列運算式︰If you add a third list, set the Text property of the Label control to the following expression:
    Text(Time(Value(Hour.Selected.Value), Value(Minute.Selected.Value), Value(Second.Selected.Value)), DateTimeFormat.LongTime)Text(Time(Value(Hour.Selected.Value), Value(Minute.Selected.Value), Value(Second.Selected.Value)), DateTimeFormat.LongTime)