PowerApps 中的 DateAdd、DateDiff 和 TimeZoneOffset 函数DateAdd, DateDiff, and TimeZoneOffset functions in PowerApps

加上或发现日期/时间值的差值,并转换本地时间和 UTC。Adds to or finds the difference in date/time values and converts between local time and UTC.

说明Description

DateAdd 函数用于在日期/时间值中加上时间单位值。The DateAdd function adds a number of units to a date/time value. 结果是一个新的日期/时间值。The result is a new date/time value. 还可以指定负值,从日期/时间值中减去时间单位值。You can also subtract a number of units from a date/time value by specifying a negative value.

DateDiff 函数返回两个日期/时间值的差值。The DateDiff function returns the difference between two date/time values. 结果是时间单位值。The result is a number of units.

对于这两个函数,时间单位可以是毫秒、秒、分钟、小时、天、月、季度或年。For both functions, units can be Milliseconds, Seconds, Minutes, Hours, Days, Months, Quarters, or Years. 默认情况下,这两个函数都以为单位。By default, both functions use Days as units.

TimeZoneOffset 函数返回用户本地时间和 UTC(协调世界时)的时间差(以分钟为单位)。The TimeZoneOffset function returns the number of minutes between the user's local time and UTC (Coordinated Universal Time).

可以结合使用 DateAdd 和 TimeZoneOffset,转换用户本地时间和 UTC(协调世界时)。You can use DateAdd with the TimeZoneOffset to convert between the user's local time and UTC (Coordinated Universal Time). 加上 TimeZoneOffset 会将本地时间转换为 UTC,减去 TimeZoneOffset(即添加负值)会将 UTC 转换为本地时间。Adding TimeZoneOffset will convert a local time to UTC, and subtracting it (adding the negative) will convert from UTC to local time.

有关详细信息,请参阅处理日期和时间Also see working with dates and times for more information.

语法Syntax

DateAdd( DateTime, Addition [, Units ] )DateAdd( DateTime, Addition [, Units ] )

  • DateTime - 必需。DateTime - Required. 要进行运算的日期/时间值。Date/time value to operate on.
  • Addition - 必需。Addition - Required. 要在 DateTime 中加上的数值,以时间单位为单位。Number, in Units, to add to the DateTime.
  • 单位 - 可选。Units - Optional. 要加上的数值的时间单位类型:毫秒、秒、分钟、小时、天、月、季度或年。The type of Units to add: Milliseconds, Seconds, Minutes, Hours, Days, Months, Quarters, or Years. 如果未指定则使用If not specified, Days are used.

DateDiff( StartDateTime, EndDateTime [, Units ] )DateDiff( StartDateTime, EndDateTime [, Units ] )

  • StartDateTime - 必需。StartDateTime - Required. 开始日期/时间值。Starting date/time value.
  • EndDateTime - 必需。EndDateTime - Required. 结束日期/时间值。Ending date/time value.
  • 单位 - 可选。Units - Optional. 要加上的数值的时间单位类型:毫秒、秒、分钟、小时、天、月、季度或年。The type of Units to add: Milliseconds, Seconds, Minutes, Hours, Days, Months, Quarters, or Years. 如果未指定则使用If not specified, Days are used.

TimeZoneOffset( [ DateTime ] )TimeZoneOffset( [ DateTime ] )

  • DateTime - 可选。DateTime - Optional. 要为其返回时间差的日期/时间值。Date/time value for which to return the offset. 默认情况下,使用当前日期/时间。By default, the current date/time is used.

示例Examples

在下面所有示例中,假定当前日期和时间为“July 15, 2013, 1:02 PM”。In all of these examples, assume that the current date and time is July 15, 2013, 1:02 PM.

简单 DateAddSimple DateAdd

公式Formula 说明Description 结果Result
Text( DateAdd( Now(), 3 ),
"dd-mm-yyyy hh:mm" )
Text( DateAdd( Now(), 3 ),
"dd-mm-yyyy hh:mm" )
在当前日期和时间中加上三天(默认时间单位)。Adds three days (default units) to the current date and time. "18-07-2013 13:02""18-07-2013 13:02"
Text( DateAdd( Now(), 4, Hours ),
"dd-mm-yyyy hh:mm" )
Text( DateAdd( Now(), 4, Hours ),
"dd-mm-yyyy hh:mm" )
在当前日期和时间中加上四小时。Add four hours to the current date and time. "15-07-2013 17:02""15-07-2013 17:02"
Text( DateAdd( Today(), 1, Months ),
"dd-mm-yyyy hh:mm" )
Text( DateAdd( Today(), 1, Months ),
"dd-mm-yyyy hh:mm" )
在当前日期(无时间)中加上一个月,因为“Today”不返回时间组件。Adds one month to the current date, without time as Today doesn't return a time component. "15-08-2013 00:00""15-08-2013 00:00"
Text( DateAdd( Now(), ‑30, Minutes ),
"dd-mm-yyyy hh:mm" )
Text( DateAdd( Now(), ‑30, Minutes ),
"dd-mm-yyyy hh:mm" )
从当前日期和时间中减去 30 分钟。Subtracts 30 minutes from the current date and time. "15-07-2013 12:32""15-07-2013 12:32"

简单 DateDiffSimple DateDiff

公式Formula 说明Description 结果Result
DateDiff( Now(), DateValue("1/1/2014") )DateDiff( Now(), DateValue("1/1/2014") ) 返回两个时间单位(默认时间单位为“天”)差值Returns the difference between the two units in the default units of Days 170170
DateDiff( Now(), DateValue("1/1/2014"), Months )DateDiff( Now(), DateValue("1/1/2014"), Months ) 返回两个时间单位(月)差值Returns the difference between the two values in Months 66
DateDiff( Now(), Today(), Minutes )DateDiff( Now(), Today(), Minutes ) 返回当前日期/时间和仅当前日期(没有时间)的差值(以分钟为单位)。Returns the difference between the current date/time and the current date only (no time) in minutes. 由于“Now”晚于“Today”,因此结果将为负数。Since the Now is later than Today the result will be negative. -782-782

转换为 UTCConverting to UTC

若要转换为 UTC(协调世界时),请为给定时间加上 TimeZoneOffset。To convert to UTC (Coordinated Universal Time), add the TimeZoneOffset for the given time.

例如,假设当前日期和时间为“July 15, 2013, 1:02 PM”,使用太平洋夏令时(PDT、UTC-7)。For example, imagine the current date and time is July 15, 2013, 1:02 PM in Pacific Daylight Time (PDT, UTC-7). 若要确定当前 UTC,请使用:To determine the current time in UTC, use:

  • DateAdd( Now(), TimeZoneOffset(), Minutes )DateAdd( Now(), TimeZoneOffset(), Minutes )

由于 TimeZoneOffset 默认为当前时间,因此无需向其传递参数。TimeZoneOffset defaults to the current time, so you don't need to pass it an argument.

若要查看结果,请使用格式为“dd-mm-yyyy hh:mm”的 Text 函数,这将返回“15-07-2013 20:02”。To see the result, use the Text function with the format dd-mm-yyyy hh:mm, which will return 15-07-2013 20:02.

从 UTC 转换Converting from UTC

若要从 UTC 转换,请为给定时间减去 TimeZoneOffset(通过添加负号)。To convert from UTC, subtract the TimeZoneOffset (by adding the negative) for the given time.

例如,假设将 UTC 日期和时间“July 15, 2013, 8:02 PM”存储到名为“StartTime”的变量中。For example, imagine the UTC date and time July 15, 2013, 8:02 PM is stored in a variable named StartTime. 若要调整用户时区时间,请使用:To adjust the time for the user's time zone, use:

  • DateAdd( StartTime, -TimeZoneOffset( StartTime ), Minutes )DateAdd( StartTime, -TimeZoneOffset( StartTime ), Minutes )

请注意,TimeZoneOffset 之前是负号,以减去(而不是加上)时间差。Note the negative sign before TimeZoneOffset to subtract the offset rather than add it.

若要查看结果,请使用格式为“dd-mm-yyyy hh:mm”的 Text 函数,这将返回“15-07-2013 13:02”(如果使用太平洋夏令时)。To see the result, use the Text function with the format dd-mm-yyyy hh:mm, which will result in 15-07-2013 13:02 if you're in Pacific Daylight Time.