Skip to contents

We register S3 methods to convert datetimeoffset() objects to other R datetime objects:

Usage

# S3 method for class 'datetimeoffset'
as.Date(x, ...)

# S3 method for class 'datetimeoffset'
as_date(x, ...)

# S3 method for class 'datetimeoffset'
as.POSIXct(x, tz = mode_tz(x), ..., fill = "")

# S3 method for class 'datetimeoffset'
as_date_time(x, zone = mode_tz(x), ..., fill = NA_character_)

# S3 method for class 'datetimeoffset'
as.POSIXlt(x, tz = mode_tz(x), ..., fill = "")

# S3 method for class 'datetimeoffset'
as_year_month_day(x, ...)

# S3 method for class 'datetimeoffset'
as_year_month_weekday(x, ...)

# S3 method for class 'datetimeoffset'
as_iso_year_week_day(x, ...)

# S3 method for class 'datetimeoffset'
as_year_quarter_day(x, ..., start = NULL)

# S3 method for class 'datetimeoffset'
as_year_day(x, ...)

# S3 method for class 'datetimeoffset'
as_naive_time(x, ...)

# S3 method for class 'datetimeoffset'
as_sys_time(
  x,
  ...,
  ambiguous = "error",
  nonexistent = "error",
  fill = NA_character_
)

# S3 method for class 'datetimeoffset'
as_zoned_time(
  x,
  zone = mode_tz(x),
  ...,
  ambiguous = "error",
  nonexistent = "error",
  fill = NA_character_
)

# S3 method for class 'datetimeoffset'
as_weekday(x, ...)

Arguments

x

A datetimeoffset() object

...

Ignored

tz, zone

What time zone to assume

fill

If timezone and UTC offset info is missing what timezone to assume. See fill_tz().

start

The month to start the fiscal year in. See clock::as_year_quarter_day().

ambiguous

What to do when the "clock time" in the new time zone is ambiguous. See clock::as_zoned_time.clock_naive_time().

nonexistent

What to do when the "clock time" in the new time zone doesn't exist. See clock::as_zoned_time.clock_naive_time().

Value

A datetime object vector

Details

We register S3 methods for the following:

Examples

  # {base}
  today <- as_datetimeoffset(Sys.Date())
  now <- as_datetimeoffset(Sys.time())

  as.Date(today)
#> [1] "2025-03-24"
  as.Date(now)
#> [1] "2025-03-24"
  as.POSIXct(now)
#> [1] "2025-03-24 09:52:32 PDT"
  as.POSIXlt(now)
#> [1] "2025-03-24 09:52:32"

  # {clock}
  clock::as_date(today)
#> [1] "2025-03-24"
  clock::as_date_time(now)
#> [1] "2025-03-24 09:52:32 PDT"

  clock::as_year_month_day(now)
#> <year_month_day<microsecond>[1]>
#> [1] "2025-03-24T09:52:32.076870"
  clock::as_year_month_weekday(now)
#> <year_month_weekday<microsecond>[1]>
#> [1] "2025-03-Mon[4]T09:52:32.076870"
  clock::as_iso_year_week_day(now)
#> <iso_year_week_day<microsecond>[1]>
#> [1] "2025-W13-1T09:52:32.076870"
  clock::as_year_quarter_day(now)
#> <year_quarter_day<January><microsecond>[1]>
#> [1] "2025-Q1-83T09:52:32.076870"
  clock::as_year_day(now)
#> <year_day<microsecond>[1]>
#> [1] "2025-083T09:52:32.076870"

  clock::as_naive_time(now)
#> <naive_time<microsecond>[1]>
#> [1] "2025-03-24T09:52:32.076870"
  clock::as_sys_time(now)
#> <sys_time<microsecond>[1]>
#> [1] "2025-03-24T16:52:32.076870"
  clock::as_zoned_time(now)
#> <zoned_time<microsecond><America/Los_Angeles>[1]>
#> [1] "2025-03-24T09:52:32.076870-07:00"

  clock::as_weekday(now)
#> <weekday[1]>
#> [1] Mon

  if (requireNamespace("nanotime")) {
    nanotime::as.nanotime(now)
  }
#> integer64
#> [1] 2025-03-24T16:52:32.076870+00:00
#> attr(,"class")
#> [1] nanotime
#> attr(,"class")attr(,"package")
#> [1] nanotime
#> attr(,".S3Class")
#> [1] integer64

  if (requireNamespace("parttime")) {
    parttime::as.parttime(now)
  }
#> <partial_time<YMDhms+tz>[1]> 
#> [1] "2025-03-24 09:52:32.077+-7:00"