Hàm dấu thời gian

Hàm dấu thời gian

Tên Mô tả
CURRENT_TIMESTAMP Tạo một TIMESTAMP tương ứng với thời gian yêu cầu.
TIMESTAMP_TRUNC Cắt bớt một TIMESTAMP theo độ chi tiết nhất định.
UNIX_MICROS_TO_TIMESTAMP Chuyển đổi số lượng micro giây kể từ 1970-01-01 00:00:00 UTC thành TIMESTAMP
UNIX_MILLIS_TO_TIMESTAMP Chuyển đổi số mili giây kể từ 1970-01-01 00:00:00 UTC thành TIMESTAMP
UNIX_SECONDS_TO_TIMESTAMP Chuyển đổi số giây kể từ 1970-01-01 00:00:00 UTC thành TIMESTAMP
TIMESTAMP_ADD Thêm một khoảng thời gian vào TIMESTAMP
TIMESTAMP_SUB Trừ một khoảng thời gian vào TIMESTAMP
TIMESTAMP_TO_UNIX_MICROS Chuyển đổi TIMESTAMP thành số vi giây kể từ 1970-01-01 00:00:00 UTC
TIMESTAMP_TO_UNIX_MILLIS Chuyển đổi TIMESTAMP thành số mili giây kể từ 1970-01-01 00:00:00 UTC
TIMESTAMP_TO_UNIX_SECONDS Chuyển đổi TIMESTAMP thành số giây kể từ 1970-01-01 00:00:00 UTC
TIMESTAMP_DIFF Trả về số nguyên của khoảng thời gian unit được chỉ định giữa hai TIMESTAMP.
TIMESTAMP_EXTRACT Trích xuất một part cụ thể (ví dụ: năm, tháng, ngày) từ một TIMESTAMP.

CURRENT_TIMESTAMP

Cú pháp:

current_timestamp() -> TIMESTAMP

Nội dung mô tả:

Lấy dấu thời gian ở đầu thời gian yêu cầu input (được diễn giải là số lượng vi giây kể từ 1970-01-01 00:00:00 UTC).

Giá trị này ổn định trong một truy vấn và sẽ luôn phân giải thành cùng một giá trị nếu được gọi nhiều lần.

TIMESTAMP_TRUNC

Cú pháp:

timestamp_trunc(timestamp: TIMESTAMP, granularity: STRING[, timezone: STRING]) -> TIMESTAMP

Nội dung mô tả:

Cắt bớt dấu thời gian xuống một mức độ chi tiết nhất định.

Đối số granularity phải là một chuỗi và là một trong những đối số sau:

  • microsecond
  • millisecond
  • second
  • minute
  • hour
  • day
  • week
  • week([weekday])
  • month
  • quarter
  • year
  • isoyear

Nếu bạn cung cấp đối số timezone, thì việc cắt bớt sẽ dựa trên ranh giới lịch của múi giờ đã cho (ví dụ: việc cắt bớt theo ngày sẽ cắt bớt đến nửa đêm theo múi giờ đã cho). Việc cắt bớt sẽ tuân theo giờ mùa hè.

Nếu bạn không cung cấp timezone, thì việc cắt bớt sẽ dựa trên ranh giới lịch UTC.

Đối số timezone phải là một chuỗi thể hiện múi giờ trong cơ sở dữ liệu tz, ví dụ: America/New_York. Bạn cũng có thể sử dụng mức chênh lệch thời gian tuỳ chỉnh bằng cách chỉ định mức chênh lệch so với GMT.

Ví dụ:

timestamp granularity timezone timestamp_trunc(timestamp, granularity, timezone)
2000-01-01 10:20:30:123456 UTC "giây" Không được cung cấp 2001-01-01 10:20:30 UTC
1997-05-31 04:30:30 UTC "ngày" Không được cung cấp 1997-05-31 00:00:00 UTC
1997-05-31 04:30:30 UTC "ngày" "America/Los_Angeles" 1997-05-30 07:00:00 UTC
2001-03-16 04:00:00 UTC "week(friday) Không được cung cấp 2001-03-16 00:00:00 UTC
2001-03-23 04:00:00 UTC "week(friday) "America/Los_Angeles" 2001-03-23 17:00:00 UTC
20:00:00 ngày 24 tháng 1 năm 2026 (giờ UTC) "tháng" "GMT+06:32:43" 2026-01-01T06:32:43 UTC

UNIX_MICROS_TO_TIMESTAMP

Cú pháp:

unix_micros_to_timestamp(input: INT64) -> TIMESTAMP

Nội dung mô tả:

Chuyển đổi input (được diễn giải là số vi giây kể từ 1970-01-01 00:00:00 UTC) thành TIMESTAMP. Trả về một error nếu input không thể chuyển đổi thành TIMESTAMP hợp lệ.

Ví dụ:

input unix_micros_to_timestamp(input)
0L 00:00:00 ngày 1 tháng 1 năm 1970 (giờ UTC)
400123456L 1970-01-01 00:06:40.123456 UTC
-1000000L 1969-12-31 23:59:59 UTC
Node.js
const result = await db.pipeline()
  .collection("documents")
  .select(
    field("createdAtMicros").unixMicrosToTimestamp().as("createdAtString")
  )
  .execute();

Web

const result = await execute(db.pipeline()
  .collection("documents")
  .select(
    field("createdAtMicros").unixMicrosToTimestamp().as("createdAtString")
  )
);
Swift
let result = try await db.pipeline()
  .collection("documents")
  .select([
    Field("createdAtMicros").unixMicrosToTimestamp().as("createdAtString")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("documents")
    .select(
        field("createdAtMicros").unixMicrosToTimestamp().alias("createdAtString")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("documents")
    .select(
        field("createdAtMicros").unixMicrosToTimestamp().alias("createdAtString")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("documents")
    .select(
        Field.of("createdAtMicros")
        .unix_micros_to_timestamp()
        .as_("createdAtString")
    )
    .execute()
)
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("documents")
        .select(unixMicrosToTimestamp(field("createdAtMicros")).as("createdAtString"))
        .execute()
        .get();

UNIX_MILLIS_TO_TIMESTAMP

Cú pháp:

unix_millis_to_timestamp(input: INT64) -> TIMESTAMP

Nội dung mô tả:

Chuyển đổi input (được diễn giải là số mili giây kể từ 1970-01-01 00:00:00 UTC) thành TIMESTAMP. Trả về một error nếu input không thể chuyển đổi thành TIMESTAMP hợp lệ.

Ví dụ:

input unix_millis_to_timestamp(input)
0L 00:00:00 ngày 1 tháng 1 năm 1970 (giờ UTC)
4000123L 1970-01-01 01:06:40.123 UTC
-1000000L 1969-12-31 23:43:20 UTC
Node.js
const result = await db.pipeline()
  .collection("documents")
  .select(
    field("createdAtMillis").unixMillisToTimestamp().as("createdAtString")
  )
  .execute();

Web

const result = await execute(db.pipeline()
  .collection("documents")
  .select(
    field("createdAtMillis").unixMillisToTimestamp().as("createdAtString")
  )
);
Swift
let result = try await db.pipeline()
  .collection("documents")
  .select([
    Field("createdAtMillis").unixMillisToTimestamp().as("createdAtString")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("documents")
    .select(
        field("createdAtMillis").unixMillisToTimestamp().alias("createdAtString")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("documents")
    .select(
        field("createdAtMillis").unixMillisToTimestamp().alias("createdAtString")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("documents")
    .select(
        Field.of("createdAtMillis")
        .unix_millis_to_timestamp()
        .as_("createdAtString")
    )
    .execute()
)
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("documents")
        .select(unixMillisToTimestamp(field("createdAtMillis")).as("createdAtString"))
        .execute()
        .get();

UNIX_SECONDS_TO_TIMESTAMP

Cú pháp:

unix_seconds_to_timestamp(input: INT64) -> TIMESTAMP

Nội dung mô tả:

Chuyển đổi input (được diễn giải là số giây kể từ 1970-01-01 00:00:00 UTC) thành TIMESTAMP. Trả về một error nếu input không thể chuyển đổi thành TIMESTAMP hợp lệ.

Ví dụ:

input unix_seconds_to_timestamp(input)
0L 00:00:00 ngày 1 tháng 1 năm 1970 (giờ UTC)
60L 1970-01-01 00:01:00 UTC
-300L 1969-12-31 23:55:00 UTC
Node.js
const result = await db.pipeline()
  .collection("documents")
  .select(
    field("createdAtSeconds").unixSecondsToTimestamp().as("createdAtString")
  )
  .execute();

Web

const result = await execute(db.pipeline()
  .collection("documents")
  .select(
    field("createdAtSeconds").unixSecondsToTimestamp().as("createdAtString")
  )
);
Swift
let result = try await db.pipeline()
  .collection("documents")
  .select([
    Field("createdAtSeconds").unixSecondsToTimestamp().as("createdAtString")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("documents")
    .select(
        field("createdAtSeconds").unixSecondsToTimestamp().alias("createdAtString")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("documents")
    .select(
        field("createdAtSeconds").unixSecondsToTimestamp().alias("createdAtString")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("documents")
    .select(
        Field.of("createdAtSeconds")
        .unix_seconds_to_timestamp()
        .as_("createdAtString")
    )
    .execute()
)
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("documents")
        .select(unixSecondsToTimestamp(field("createdAtSeconds")).as("createdAtString"))
        .execute()
        .get();

TIMESTAMP_ADD

Cú pháp:

timestamp_add(timestamp: TIMESTAMP, unit: STRING, amount: INT64) -> TIMESTAMP

Nội dung mô tả:

Thêm amount bằng unit từ timestamp. Đối số amount có thể là số âm, trong trường hợp đó, đối số này tương đương với TIMESTAMP_SUB.

Đối số unit phải là một chuỗi và là một trong những đối số sau:

  • microsecond
  • millisecond
  • second
  • minute
  • hour
  • day

Đưa ra lỗi nếu dấu thời gian kết quả không nằm trong phạm vi TIMESTAMP.

Ví dụ:

timestamp unit amount timestamp_add(timestamp, unit, amount)
2025-02-20 00:00:00 UTC "phút" 2L 2025-02-20 00:02:00 UTC
2025-02-20 00:00:00 UTC "giờ" -4L 20:00:00 ngày 19 tháng 2 năm 2025 (giờ UTC)
2025-02-20 00:00:00 UTC "ngày" 5L 00:00:00 ngày 25 tháng 2 năm 2025 (giờ UTC)
Node.js
const result = await db.pipeline()
  .collection("documents")
  .select(
    field("createdAt").timestampAdd("day", 3653).as("expiresAt")
  )
  .execute();

Web

const result = await execute(db.pipeline()
  .collection("documents")
  .select(
    field("createdAt").timestampAdd("day", 3653).as("expiresAt")
  )
);
Swift
let result = try await db.pipeline()
  .collection("documents")
  .select([
    Field("createdAt").timestampAdd(3653, .day).as("expiresAt")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("documents")
    .select(
        field("createdAt")
          .timestampAdd("day", 3653)
          .alias("expiresAt")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("documents")
    .select(
        field("createdAt").timestampAdd("day", 3653).alias("expiresAt")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("documents")
    .select(Field.of("createdAt").timestamp_add("day", 3653).as_("expiresAt"))
    .execute()
)
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("documents")
        .select(timestampAdd(field("createdAt"), "day", 3653).as("expiresAt"))
        .execute()
        .get();

TIMESTAMP_SUB

Cú pháp:

timestamp_sub(timestamp: TIMESTAMP, unit: STRING, amount: INT64) -> TIMESTAMP

Nội dung mô tả:

Trừ amount của unit khỏi timestamp. Đối số amount có thể là số âm, trong trường hợp đó, đối số này tương đương với TIMESTAMP_ADD.

Đối số unit phải là một chuỗi và là một trong những đối số sau:

  • microsecond
  • millisecond
  • second
  • minute
  • hour
  • day

Đưa ra lỗi nếu dấu thời gian kết quả không nằm trong phạm vi TIMESTAMP.

Ví dụ:

timestamp unit amount timestamp_sub(timestamp, unit, amount)
00:00, ngày 4 tháng 7 năm 2026, giờ UTC "phút" 40L 23:20:00 ngày 3 tháng 7 năm 2026 (giờ UTC)
00:00, ngày 4 tháng 7 năm 2026, giờ UTC "giờ" -24L 00:00, ngày 5 tháng 7 năm 2026, giờ UTC
00:00, ngày 4 tháng 7 năm 2026, giờ UTC "ngày" 3L 00:00:00 ngày 1 tháng 7 năm 2026 theo giờ UTC
Node.js
const result = await db.pipeline()
  .collection("documents")
  .select(
    field("expiresAt").timestampSubtract("day", 14).as("sendWarningTimestamp")
  )
  .execute();

Web

const result = await execute(db.pipeline()
  .collection("documents")
  .select(
    field("expiresAt").timestampSubtract("day", 14).as("sendWarningTimestamp")
  )
);
Swift
let result = try await db.pipeline()
  .collection("documents")
  .select([
    Field("expiresAt").timestampSubtract(14, .day).as("sendWarningTimestamp")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("documents")
    .select(
        field("expiresAt")
          .timestampSubtract("day", 14)
          .alias("sendWarningTimestamp")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("documents")
    .select(
        field("expiresAt").timestampSubtract("day", 14).alias("sendWarningTimestamp")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("documents")
    .select(
        Field.of("expiresAt")
        .timestamp_subtract("day", 14)
        .as_("sendWarningTimestamp")
    )
    .execute()
)
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("documents")
        .select(timestampSubtract(field("expiresAt"), "day", 14).as("sendWarningTimestamp"))
        .execute()
        .get();

TIMESTAMP_TO_UNIX_MICROS

Cú pháp:

timestamp_to_unix_micros(input: TIMESTAMP) -> INT64

Nội dung mô tả:

Chuyển đổi input thành số lượng micrô giây kể từ 1970-01-01 00:00:00 UTC. Cắt bớt các mức độ chính xác cao hơn bằng cách làm tròn xuống đầu của vi giây.

Ví dụ:

input timestamp_to_unix_micros(input)
00:00:00 ngày 1 tháng 1 năm 1970 (giờ UTC) 0L
1970-01-01 00:06:40.123456 UTC 400123456L
1969-12-31 23:59:59 UTC -1000000L
Node.js
const result = await db.pipeline()
  .collection("documents")
  .select(
    field("dateString").timestampToUnixMicros().as("unixMicros")
  )
  .execute();

Web

const result = await execute(db.pipeline()
  .collection("documents")
  .select(
    field("dateString").timestampToUnixMicros().as("unixMicros")
  )
);
Swift
let result = try await db.pipeline()
  .collection("documents")
  .select([
    Field("dateString").timestampToUnixMicros().as("unixMicros")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("documents")
    .select(
        field("dateString").timestampToUnixMicros().alias("unixMicros")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("documents")
    .select(
        field("dateString").timestampToUnixMicros().alias("unixMicros")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("documents")
    .select(Field.of("dateString").timestamp_to_unix_micros().as_("unixMicros"))
    .execute()
)
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("documents")
        .select(timestampToUnixMicros(field("dateString")).as("unixMicros"))
        .execute()
        .get();

TIMESTAMP_TO_UNIX_MILLIS

Cú pháp:

timestamp_to_unix_millis(input: TIMESTAMP) -> INT64

Nội dung mô tả:

Chuyển đổi input thành số mili giây kể từ 1970-01-01 00:00:00 UTC. Cắt bớt các mức độ chính xác cao hơn bằng cách làm tròn xuống đầu của mili giây.

Ví dụ:

input timestamp_to_unix_millis(input)
00:00:00 ngày 1 tháng 1 năm 1970 (giờ UTC) 0L
1970-01-01 01:06:40.123 UTC 4000123L
1969-12-31 23:43:20 -1000000L
Node.js
const result = await db.pipeline()
  .collection("documents")
  .select(
    field("dateString").timestampToUnixMillis().as("unixMillis")
  )
  .execute();

Web

const result = await execute(db.pipeline()
  .collection("documents")
  .select(
    field("dateString").timestampToUnixMillis().as("unixMillis")
  )
);
Swift
let result = try await db.pipeline()
  .collection("documents")
  .select([
    Field("dateString").timestampToUnixMillis().as("unixMillis")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("documents")
    .select(
        field("dateString").timestampToUnixMillis().alias("unixMillis")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("documents")
    .select(
        field("dateString").timestampToUnixMillis().alias("unixMillis")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("documents")
    .select(Field.of("dateString").timestamp_to_unix_millis().as_("unixMillis"))
    .execute()
)
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("documents")
        .select(timestampToUnixMillis(field("dateString")).as("unixMillis"))
        .execute()
        .get();

TIMESTAMP_TO_UNIX_SECONDS

Cú pháp:

timestamp_to_unix_seconds(input: TIMESTAMP) -> INT64

Nội dung mô tả:

Chuyển đổi input thành số giây kể từ 1970-01-01 00:00:00 UTC. Cắt bớt các mức độ chính xác cao hơn bằng cách làm tròn xuống đầu giây.

Ví dụ:

input timestamp_to_unix_seconds(input)
00:00:00 ngày 1 tháng 1 năm 1970 (giờ UTC) 0L
1970-01-01 00:01:00 UTC 60L
1969-12-31 23:55:00 UTC -300L
Node.js
const result = await db.pipeline()
  .collection("documents")
  .select(
    field("dateString").timestampToUnixSeconds().as("unixSeconds")
  )
  .execute();

Web

const result = await execute(db.pipeline()
  .collection("documents")
  .select(
    field("dateString").timestampToUnixSeconds().as("unixSeconds")
  )
);
Swift
let result = try await db.pipeline()
  .collection("documents")
  .select([
    Field("dateString").timestampToUnixSeconds().as("unixSeconds")
  ])
  .execute()

Kotlin

val result = db.pipeline()
    .collection("documents")
    .select(
        field("dateString").timestampToUnixSeconds().alias("unixSeconds")
    )
    .execute()

Java

Task<Pipeline.Snapshot> result = db.pipeline()
    .collection("documents")
    .select(
        field("dateString").timestampToUnixSeconds().alias("unixSeconds")
    )
    .execute();
Python
from google.cloud.firestore_v1.pipeline_expressions import Field

result = (
    client.pipeline()
    .collection("documents")
    .select(Field.of("dateString").timestamp_to_unix_seconds().as_("unixSeconds"))
    .execute()
)
Java
Pipeline.Snapshot result =
    firestore
        .pipeline()
        .collection("documents")
        .select(timestampToUnixSeconds(field("dateString")).as("unixSeconds"))
        .execute()
        .get();

TIMESTAMP_DIFF

Cú pháp:

timestamp_diff(end: TIMESTAMP, start: TIMESTAMP, unit: STRING) -> INT64

Nội dung mô tả:

Trả về số nguyên của khoảng thời gian unit được chỉ định giữa hai TIMESTAMP.

  • Trả về giá trị âm nếu end đứng trước start.
  • Cắt bớt mọi đơn vị phân số. Ví dụ: timestamp_diff("2021-01-01 00:00:01", "2021-01-01 00:00:00", "minute") trả về 0.

Đối số unit phải là một chuỗi và là một trong những đối số sau:

  • microsecond
  • millisecond
  • second
  • minute
  • hour
  • day

Ví dụ:

end start unit timestamp_diff(end, start, unit)
00:01:00 ngày 4 tháng 7 năm 2026 theo giờ UTC 00:00, ngày 4 tháng 7 năm 2026, giờ UTC "giây" 60L
00:00, ngày 4 tháng 7 năm 2026, giờ UTC 00:00, ngày 5 tháng 7 năm 2026, giờ UTC "ngày" -1L
2026-07-04 00:00:59 UTC 00:00, ngày 4 tháng 7 năm 2026, giờ UTC "phút" 0L

TIMESTAMP_EXTRACT

Cú pháp:

timestamp_extract(timestamp: TIMESTAMP, part: STRING[, timezone: STRING]) -> INT64

Nội dung mô tả:

Trích xuất một part cụ thể (ví dụ: năm, tháng, ngày) từ timestamp.

Đối số part phải là một chuỗi và là một trong những đối số sau:

  • microsecond
  • millisecond
  • second
  • minute
  • hour
  • day
  • dayofweek: Trả về một giá trị từ 1 (Chủ Nhật) đến 7 (Thứ Bảy).
  • dayofyear
  • week: Trả về số tuần của năm, bắt đầu từ 1 cho ngày Chủ Nhật đầu tiên của năm.
  • week([weekday]): Trả về số tuần trong năm, bắt đầu từ weekday được chỉ định.
  • month
  • quarter
  • year
  • isoweek: Trả về số tuần theo tiêu chuẩn ISO 8601.
  • isoyear: Trả về năm đánh số tuần theo tiêu chuẩn ISO 8601.

Nếu bạn cung cấp đối số timezone, thì quá trình trích xuất sẽ dựa trên lịch của múi giờ đã cho. Hoạt động trích xuất sẽ tuân thủ giờ mùa hè.

Nếu bạn không cung cấp timezone, thì quá trình trích xuất sẽ dựa trên UTC.

Đối số timezone phải là một chuỗi thể hiện múi giờ trong cơ sở dữ liệu múi giờ, ví dụ: America/New_York. Bạn cũng có thể sử dụng độ lệch thời gian tuỳ chỉnh bằng cách chỉ định độ lệch so với GMT.

Ví dụ:

timestamp part timezone timestamp_extract(timestamp, part, timezone)
2025-02-20 10:20:30 UTC "year" Không được cung cấp 2025
2025-02-20 10:20:30 UTC "ngày" Không được cung cấp 20
23:59:59 ngày 31 tháng 12 năm 2025 (giờ UTC) "year" "Asia/Tokyo" 2026